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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9381463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:43:38  来源:igfitidea点击:

How to create a file in Linux from terminal window?

linuxbashfilecommand-line-interface

提问by raffian

What's the easiest way to create a file in Linux terminal?

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

采纳答案by Eugen Rieck

Depending on what you want the file to contain:

根据您希望文件包含的内容:

  • touch /path/to/filefor an empty file
  • somecommand > /path/to/filefor a file containing the output of some command.

      eg: grep --help > randomtext.txt
          echo "This is some text" > randomtext.txt
    
  • nano /path/to/fileor vi /path/to/file(or any 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 exist

  • touch /path/to/file对于空文件
  • somecommand > /path/to/file对于包含某些命令输出的文件。

      eg: grep --help > randomtext.txt
          echo "This is some text" > randomtext.txt
    
  • nano /path/to/filevi /path/to/file(或any other editor emacs,gedit etc
    它要么打开现有文件进行编辑,要么创建并打开空文件以输入(如果它不存在)

回答by raffian

Create the file using cat

使用创建文件 cat

$ cat > myfile.txt

$ cat > myfile.txt

Now, just type whatever you want in the file:

现在,只需在文件中输入您想要的任何内容:

Hello World!

Hello World!

CTRL-D to save and exit

CTRL-D 保存并退出

回答by andreapier

Also, create an empty file:

另外,创建一个空文件:

touch myfile.txt

回答by Roman Newaza

haha! it's easy! try this:

哈哈!这很简单!尝试这个:

$ touch filename

回答by Mariusz Jamro

Use touch

使用触摸

touch filename

回答by Andy Arismendi

You can use the touchcommand 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

回答by sorpigal

There are several possible solutions:

有几种可能的解决方案:

Create an empty file

创建一个空文件

touch file

>file

echo -n > file

printf '' > file

The echoversion will work only if your version of echosupports the -nswitch to suppress newlines. This is a non-standard addition. The other examples will all work in a POSIX shell.

echo仅当您的版本echo支持-n禁止换行的开关时,该版本才有效。这是一个非标准的添加。其他示例都将在 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 $EDITORcommand assumes that you have an interactive text editor defined in the EDITOR environment variable and that you interactively enter equivalent text. The catversion presumes a literal newline after the \and after each other line. Other than that these will all work in a POSIX shell.

这些是等价的。该$EDITOR命令假定您在 EDITOR 环境变量中定义了一个交互式文本编辑器,并且您以交互方式输入等效文本。该cat版本假定\在每一行之后和之后都有一个文字换行符。除此之外,这些都可以在 POSIX shell 中工作。

Of course there are many other methods of writing and creating files, too.

当然,还有许多其他的写入和创建文件的方法。

回答by Sebastian

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/dircommand.

如果你们正在尝试创建一个新文件,但它说:'File does not exist',这仅仅是因为您也在访问一个尚不存在的目录。您必须首先使用该mkdir /path/to/dir命令创建所有不存在的目录。

回答by Christy George

This will create an empty file with the current timestamp

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

touch filename

回答by Sam Perry

I like the nanocommand-line editor (text):

我喜欢nano命令行编辑器(文本):

nano filename