What's the easiest way to create a file in Linux?
在Linux中创建文件最简单的方法是什么?
15 个解决方案
#1
391
Depending on what you want the file to contain:
取决于您希望文件包含的内容:
-
touch /path/to/file
for an empty file - 触摸/路径/到/文件的一个空文件。
-
somecommand > /path/to/file
for a file containing the output of some command.为包含某些命令的输出的文件,somecommand > /path/to/文件。
eg: grep --help > randomtext.txt echo "This is some text" > randomtext.txt
-
nano /path/to/file
orvi /path/to/file
(orany other editor emacs,gedit etc
)
It either opens the existing one for editing or creates & opens the empty file to enter, if it doesn't existnano /path/to/file或vi /path/to/file(或任何其他编辑emacs、gedit等)要么打开现有的编辑器,要么创建并打开空白文件,如果它不存在的话。
#3
60
Create the file using cat
使用cat创建文件。
$ cat > myfile.txt
$猫> myfile.txt
Now, just type whatever you want in the file:
现在,在文件中输入你想要的内容:
Hello World!
你好世界!
CTRL-D to save and exit
CTRL-D保存并退出。
#4
31
There are several possible solutions:
有几种可能的解决方案:
Create an empty file
touch file
>file
echo -n > file
printf '' > file
The echo
version will work only if your version of echo
supports the -n
switch to suppress newlines. This is a non-standard addition. The other examples will all work in a POSIX shell.
只有当你的echo版本支持-n开关来抑制换行时,echo版本才会起作用。这是一个非标准加法。其他的例子都在POSIX shell中工作。
Create a file containing a newline and nothing else
echo '' > file
printf '\n' > file
This is a valid "text file" because it ends in a newline.
这是一个有效的“文本文件”,因为它以换行符结尾。
Write text into a file
"$EDITOR" file
echo 'text' > file
cat > file <<END \
text
END
printf 'text\n' > file
These are equivalent. The $EDITOR
command assumes that you have an interactive text editor defined in the EDITOR environment variable and that you interactively enter equivalent text. The cat
version presumes a literal newline after the \
and after each other line. Other than that these will all work in a POSIX shell.
这些是等价的。$EDITOR命令假定您在编辑器环境变量中定义了一个交互式文本编辑器,并且交互式地输入等效文本。猫的版本假设一个字面上的换行后的\和之后的每一条线。除了这些,这些都将在POSIX shell中工作。
Of course there are many other methods of writing and creating files, too.
当然,还有许多其他的方法来编写和创建文件。
#5
17
Also, create an empty file:
另外,创建一个空文件:
touch myfile.txt
#6
11
haha! it's easy! try this:
哈哈!很容易!试试这个:
$ touch filename
#7
8
How to create a text file on Linux:
如何在Linux上创建一个文本文件:
- Using
touch
to create a text file:$ touch NewFile.txt
- 使用触摸创建文本文件:$ touch NewFile.txt。
- Using
cat
to create a new file:$ cat NewFile.txt
The file is created, but it's empty and still waiting for the input from the user. You can type any text into the terminal, and once done CTRL-D will close it, or CTRL-C will escape you out. - 使用cat创建一个新文件:$ cat NewFile。txt文件被创建,但它是空的,并且仍然等待用户的输入。您可以将任何文本输入到终端,并且一旦完成CTRL-D将关闭它,或者CTRL-C将退出您。
- Simply using
>
to create a text file:$ > NewFile.txt
- 简单地使用>创建一个文本文件:$ > NewFile.txt。
- Lastly, we can use any text editor name and then create the file, such as:
nano MyNewFile vi MyNewFile NameOfTheEditor NewFileName
- 最后,我们可以使用任何文本编辑器名称,然后创建文件,例如:nano MyNewFile vi MyNewFile NameOfTheEditor NewFileName。
#8
8
1st method
1号方法
echo -n > filename.txt
2nd method
2方法
> filename.txt
3rd method
第三个方法
touch filename.txt
To view the file contents
查看文件内容。
vi filename.txt
#9
7
You can use touch
command, as the others said:
你可以使用触摸命令,就像其他人说的:
touch filename
To write on file on command line, you can use echo
or printf
:
要在命令行上写入文件,可以使用echo或printf:
echo "Foo" > filename
printf "Foo" > filename
Maybe you can have problems with permissions. If you are getting the following error: bash: filename: Permission denied
, you need to use sudo bash -c 'echo "Foo" > filename'
, as described here: https://askubuntu.com/questions/103643/cannot-echo-hello-x-txt-even-with-sudo
也许你的权限有问题。如果您得到以下错误:bash: filename:权限被拒绝,您需要使用sudo bash -c 'echo "Foo" >文件名",如此处所述:https://askubuntu.com/questions/103643/ cannotecho-hello -x-txt- even-sudo。
#10
6
This will create an empty file with the current timestamp
这将创建一个带有当前时间戳的空文件。
touch filename
#11
5
You can use the touch
command to create a new empty file.
您可以使用touch命令创建一个新的空文件。
http://linux.about.com/library/cmd/blcmdl_touch.htm
http://linux.about.com/library/cmd/blcmdl_touch.htm
#13
3
Simple as that :
简单:
> filename
>文件名
#14
2
In case you guys are trying to create a new file, but it says: 'File does not exist'
, it's simply because you are also accessing a directory, which does not exist yet. You have to create all non existent directories first, using the mkdir /path/to/dir
command.
如果您试图创建一个新文件,但是它说:“文件不存在”,这仅仅是因为您还在访问一个不存在的目录。您必须首先创建所有不存在的目录,使用mkdir /path/to/dir命令。
#15
1
touch filename
for permission denied
error use sudo
command as:
对于允许被拒绝的错误使用sudo命令:
sudo touch filename
#1
391
Depending on what you want the file to contain:
取决于您希望文件包含的内容:
-
touch /path/to/file
for an empty file - 触摸/路径/到/文件的一个空文件。
-
somecommand > /path/to/file
for a file containing the output of some command.为包含某些命令的输出的文件,somecommand > /path/to/文件。
eg: grep --help > randomtext.txt echo "This is some text" > randomtext.txt
-
nano /path/to/file
orvi /path/to/file
(orany other editor emacs,gedit etc
)
It either opens the existing one for editing or creates & opens the empty file to enter, if it doesn't existnano /path/to/file或vi /path/to/file(或任何其他编辑emacs、gedit等)要么打开现有的编辑器,要么创建并打开空白文件,如果它不存在的话。
#2
#3
60
Create the file using cat
使用cat创建文件。
$ cat > myfile.txt
$猫> myfile.txt
Now, just type whatever you want in the file:
现在,在文件中输入你想要的内容:
Hello World!
你好世界!
CTRL-D to save and exit
CTRL-D保存并退出。
#4
31
There are several possible solutions:
有几种可能的解决方案:
Create an empty file
touch file
>file
echo -n > file
printf '' > file
The echo
version will work only if your version of echo
supports the -n
switch to suppress newlines. This is a non-standard addition. The other examples will all work in a POSIX shell.
只有当你的echo版本支持-n开关来抑制换行时,echo版本才会起作用。这是一个非标准加法。其他的例子都在POSIX shell中工作。
Create a file containing a newline and nothing else
echo '' > file
printf '\n' > file
This is a valid "text file" because it ends in a newline.
这是一个有效的“文本文件”,因为它以换行符结尾。
Write text into a file
"$EDITOR" file
echo 'text' > file
cat > file <<END \
text
END
printf 'text\n' > file
These are equivalent. The $EDITOR
command assumes that you have an interactive text editor defined in the EDITOR environment variable and that you interactively enter equivalent text. The cat
version presumes a literal newline after the \
and after each other line. Other than that these will all work in a POSIX shell.
这些是等价的。$EDITOR命令假定您在编辑器环境变量中定义了一个交互式文本编辑器,并且交互式地输入等效文本。猫的版本假设一个字面上的换行后的\和之后的每一条线。除了这些,这些都将在POSIX shell中工作。
Of course there are many other methods of writing and creating files, too.
当然,还有许多其他的方法来编写和创建文件。
#5
17
Also, create an empty file:
另外,创建一个空文件:
touch myfile.txt
#6
11
haha! it's easy! try this:
哈哈!很容易!试试这个:
$ touch filename
#7
8
How to create a text file on Linux:
如何在Linux上创建一个文本文件:
- Using
touch
to create a text file:$ touch NewFile.txt
- 使用触摸创建文本文件:$ touch NewFile.txt。
- Using
cat
to create a new file:$ cat NewFile.txt
The file is created, but it's empty and still waiting for the input from the user. You can type any text into the terminal, and once done CTRL-D will close it, or CTRL-C will escape you out. - 使用cat创建一个新文件:$ cat NewFile。txt文件被创建,但它是空的,并且仍然等待用户的输入。您可以将任何文本输入到终端,并且一旦完成CTRL-D将关闭它,或者CTRL-C将退出您。
- Simply using
>
to create a text file:$ > NewFile.txt
- 简单地使用>创建一个文本文件:$ > NewFile.txt。
- Lastly, we can use any text editor name and then create the file, such as:
nano MyNewFile vi MyNewFile NameOfTheEditor NewFileName
- 最后,我们可以使用任何文本编辑器名称,然后创建文件,例如:nano MyNewFile vi MyNewFile NameOfTheEditor NewFileName。
#8
8
1st method
1号方法
echo -n > filename.txt
2nd method
2方法
> filename.txt
3rd method
第三个方法
touch filename.txt
To view the file contents
查看文件内容。
vi filename.txt
#9
7
You can use touch
command, as the others said:
你可以使用触摸命令,就像其他人说的:
touch filename
To write on file on command line, you can use echo
or printf
:
要在命令行上写入文件,可以使用echo或printf:
echo "Foo" > filename
printf "Foo" > filename
Maybe you can have problems with permissions. If you are getting the following error: bash: filename: Permission denied
, you need to use sudo bash -c 'echo "Foo" > filename'
, as described here: https://askubuntu.com/questions/103643/cannot-echo-hello-x-txt-even-with-sudo
也许你的权限有问题。如果您得到以下错误:bash: filename:权限被拒绝,您需要使用sudo bash -c 'echo "Foo" >文件名",如此处所述:https://askubuntu.com/questions/103643/ cannotecho-hello -x-txt- even-sudo。
#10
6
This will create an empty file with the current timestamp
这将创建一个带有当前时间戳的空文件。
touch filename
#11
5
You can use the touch
command to create a new empty file.
您可以使用touch命令创建一个新的空文件。
http://linux.about.com/library/cmd/blcmdl_touch.htm
http://linux.about.com/library/cmd/blcmdl_touch.htm
#12
#13
3
Simple as that :
简单:
> filename
>文件名
#14
2
In case you guys are trying to create a new file, but it says: 'File does not exist'
, it's simply because you are also accessing a directory, which does not exist yet. You have to create all non existent directories first, using the mkdir /path/to/dir
command.
如果您试图创建一个新文件,但是它说:“文件不存在”,这仅仅是因为您还在访问一个不存在的目录。您必须首先创建所有不存在的目录,使用mkdir /path/to/dir命令。
#15
1
touch filename
for permission denied
error use sudo
command as:
对于允许被拒绝的错误使用sudo命令:
sudo touch filename