在Linux终端上创建文件最简单的方法是什么?


当前回答

这将创建一个带有当前时间戳的空文件

touch filename

其他回答

你可以使用触摸命令,正如其他人所说:

touch filename

要在命令行上写入文件,你可以使用echo或printf:

echo "Foo" > filename
printf "Foo" > filename

也许您在权限方面会遇到问题。如果你得到如下错误:bash: filename: Permission denied,你需要使用sudo bash -c 'echo "Foo" > filename',如下所述: https://askubuntu.com/questions/103643/cannot-echo-hello-x-txt-even-with-sudo

就这么简单:

>文件名

使用cat创建文件

$ cat > myfile.txt

现在,只要在文件中输入你想要的东西:

你好世界!

CTRL-D保存并退出

同时,创建一个空文件:

touch myfile.txt

根据你想要文件包含的内容:

触摸/path/to/file为空文件 somecommand > /path/to/file用于指定包含某命令输出的文件。 示例:grep—help > randomtext.txt echo "This is some text" > randomtext.txt Nano /path/to/file或vi /path/to/file(或任何其他编辑器emacs,gedit等) 它要么打开现有文件进行编辑,要么创建并打开空文件进入,如果它不存在的话