本文翻译自:How to create a file in Linux from terminal window?
在Linux终端中创建文件最简单的方法是什么?
#1楼
参考:https://stackoom.com/question/dMXv/如何在终端窗口中在Linux中创建文件
#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. 如果你们正在尝试创建一个新文件,但它说: 'File does not exist'
,这只是因为你也在访问一个目前尚不存在的目录。 You have to create all non existent directories first, using the mkdir /path/to/dir
command. 您必须首先使用mkdir /path/to/dir
命令创建所有不存在的目录。
#3楼
这将创建一个包含当前时间戳的空文件
touch filename
#4楼
我喜欢nano命令行编辑器(文本):
nano filename
#5楼
You can use touch
command, as the others said: 您可以使用touch
命令,其他人说:
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: Permission denied
,您需要使用sudo bash -c 'echo "Foo" > filename'
,如下所述: https : //askubuntu.com/questions/103643/cannot-呼应问候-X-TXT偶数与-须藤
#6楼
How to create a text file on Linux: 如何在Linux上创建文本文件:
- Using
touch
to create a text file:$ touch NewFile.txt
使用touch
创建文本文件:$ touch NewFile.txt
- Using
cat
to create a new file:$ cat NewFile.txt
使用cat
创建一个新文件:$ 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. 您可以在终端中输入任何文本,一旦完成,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