如何在终端窗口中在Linux中创建文件?

本文翻译自: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 : 要在命令行上写入文件,可以使用echoprintf

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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值