Merge pull request #12418 from dianbanjiu/translating-new

translated
This commit is contained in:
Xingyu.Wang 2019-02-17 15:26:16 +08:00 committed by GitHub
commit 3598f3b652
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 323 additions and 325 deletions

View File

@ -1,325 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (dianbanjiu )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (10 Methods To Create A File In Linux)
[#]: via: (https://www.2daygeek.com/linux-command-to-create-a-file/)
[#]: author: (Vinoth Kumar https://www.2daygeek.com/author/vinoth/)
10 Methods To Create A File In Linux
======
As we already know that everything is a file in Linux, that includes device as well.
Linux admin should be performing the file creation activity multiple times (It may 20 times or 50 times or more than that, its depends upon their environment) in a day.
Navigate to the following URL, if you would like to **[create a file in a specific size in Linux][1]**.
Its very important. how efficiently are we creating a file. Why im saying efficient? there is a lot of benefit if you know the efficient way to perform an activity.
It will save you a lot of time. You can spend those valuable time on other important or major tasks, where you want to spend some more time instead of doing that in hurry.
Here im including multiple ways to create a file in Linux. I advise you to choose few which is easy and efficient for you to perform your activity.
You no need to install any of the following commands because all these commands has been installed as part of Linux core utilities except nano command.
It can be done using the following 6 methods.
* **`Redirect Symbol (>):`** Standard redirect symbol allow us to create a 0KB empty file in Linux.
* **`touch:`** touch command can create a 0KB empty file if does not exist.
* **`echo:`** echo command is used to display line of text that are passed as an argument.
* **`printf:`** printf command is used to display the given text on the terminal window.
* **`cat:`** It concatenate files and print on the standard output.
* **`vi/vim:`** Vim is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain text.
* **`nano:`** nano is a small and friendly editor. It copies the look and feel of Pico, but is free software.
* **`head:`** head is used to print the first part of files..
* **`tail:`** tail is used to print the last part of files..
* **`truncate:`** truncate is used to shrink or extend the size of a file to the specified size.
### How To Create A File In Linux Using Redirect Symbol (>)?
Standard redirect symbol allow us to create a 0KB empty file in Linux. Basically it used to redirect the output of a command to a new file. When you use redirect symbol without a command then its create a file.
But it wont allow you to input any text while creating a file. However, its very simple and will be useful for lazy admins. To do so, simple enter the redirect symbol followed by the filename which you want.
```
$ > daygeek.txt
```
Use the ls command to check the created file.
```
$ ls -lh daygeek.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:00 daygeek.txt
```
### How To Create A File In Linux Using touch Command?
touch command is used to update the access and modification times of each FILE to the current time.
Its create a new file if does not exist. Also, touch command doesnt allow us to enter any text while creating a file. By default it creates a 0KB empty file.
```
$ touch daygeek1.txt
```
Use the ls command to check the created file.
```
$ ls -lh daygeek1.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:02 daygeek1.txt
```
### How To Create A File In Linux Using echo Command?
echo is a built-in command found in most operating systems. It is frequently used in scripts, batch files, and as part of individual commands to insert a text.
This is nice command that allow users to input a text while creating a file. Also, it allow us to append the text in the next time.
```
$ echo "2daygeek.com is a best Linux blog to learn Linux" > daygeek2.txt
```
Use the ls command to check the created file.
```
$ ls -lh daygeek2.txt
-rw-rw-r-- 1 daygeek daygeek 49 Feb 4 02:04 daygeek2.txt
```
To view the content from the file, use the cat command.
```
$ cat daygeek2.txt
2daygeek.com is a best Linux blog to learn Linux
```
If you would like to append the content in the same file, use the double redirect Symbol (>>).
```
$ echo "It's FIVE years old blog" >> daygeek2.txt
```
You can view the appended content from the file using cat command.
```
$ cat daygeek2.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
### How To Create A File In Linux Using printf Command?
printf command also works in the same way like how echo command works.
printf command in Linux is used to display the given string on the terminal window. printf can have format specifiers, escape sequences or ordinary characters.
```
$ printf "2daygeek.com is a best Linux blog to learn Linux\n" > daygeek3.txt
```
Use the ls command to check the created file.
```
$ ls -lh daygeek3.txt
-rw-rw-r-- 1 daygeek daygeek 48 Feb 4 02:12 daygeek3.txt
```
To view the content from the file, use the cat command.
```
$ cat daygeek3.txt
2daygeek.com is a best Linux blog to learn Linux
```
If you would like to append the content in the same file, use the double redirect Symbol (>>).
```
$ printf "It's FIVE years old blog\n" >> daygeek3.txt
```
You can view the appended content from the file using cat command.
```
$ cat daygeek3.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
### How To Create A File In Linux Using cat Command?
cat stands for concatenate. It is very frequently used in Linux to reads data from a file.
cat is one of the most frequently used commands on Unix-like operating systems. Its offer three functions which is related to text file such as display content of a file, combine multiple files into the single output and create a new file.
```
$ cat > daygeek4.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
Use the ls command to check the created file.
```
$ ls -lh daygeek4.txt
-rw-rw-r-- 1 daygeek daygeek 74 Feb 4 02:18 daygeek4.txt
```
To view the content from the file, use the cat command.
```
$ cat daygeek4.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
If you would like to append the content in the same file, use the double redirect Symbol (>>).
```
$ cat >> daygeek4.txt
This website is maintained by Magesh M, It's licensed under CC BY-NC 4.0.
```
You can view the appended content from the file using cat command.
```
$ cat daygeek4.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
This website is maintained by Magesh M, It's licensed under CC BY-NC 4.0.
```
### How To Create A File In Linux Using vi/vim Command?
Vim is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain text. It is especially useful for editing programs.
There are a lot of features are available in vim to edit a single file with the command.
```
$ vi daygeek5.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
Use the ls command to check the created file.
```
$ ls -lh daygeek5.txt
-rw-rw-r-- 1 daygeek daygeek 75 Feb 4 02:23 daygeek5.txt
```
To view the content from the file, use the cat command.
```
$ cat daygeek5.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
### How To Create A File In Linux Using nano Command?
Nanos is a another editor, an enhanced free Pico clone. nano is a small and friendly editor. It copies the look and feel of Pico, but is free software, and implements several features that Pico lacks, such as: opening multiple files, scrolling per line, undo/redo, syntax coloring, line numbering, and soft-wrapping overlong lines.
```
$ nano daygeek6.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
This website is maintained by Magesh M, It's licensed under CC BY-NC 4.0.
```
Use the ls command to check the created file.
```
$ ls -lh daygeek6.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:26 daygeek6.txt
```
To view the content from the file, use the cat command.
```
$ cat daygeek6.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
This website is maintained by Magesh M, It's licensed under CC BY-NC 4.0.
```
### How To Create A File In Linux Using head Command?
head command is used to output the first part of files. By default it prints the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name.
```
$ head -c 0K /dev/zero > daygeek7.txt
```
Use the ls command to check the created file.
```
$ ls -lh daygeek7.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:30 daygeek7.txt
```
### How To Create A File In Linux Using tail Command?
tail command is used to output the last part of files. By default it prints the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name.
```
$ tail -c 0K /dev/zero > daygeek8.txt
```
Use the ls command to check the created file.
```
$ ls -lh daygeek8.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:31 daygeek8.txt
```
### How To Create A File In Linux Using truncate Command?
truncate command is used to shrink or extend the size of a file to the specified size.
```
$ truncate -s 0K daygeek9.txt
```
Use the ls command to check the created file.
```
$ ls -lh daygeek9.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:37 daygeek9.txt
```
I have performed totally 10 commands in this article to test this. All together in the single output.
```
$ ls -lh daygeek*
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:02 daygeek1.txt
-rw-rw-r-- 1 daygeek daygeek 74 Feb 4 02:07 daygeek2.txt
-rw-rw-r-- 1 daygeek daygeek 74 Feb 4 02:15 daygeek3.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:20 daygeek4.txt
-rw-rw-r-- 1 daygeek daygeek 75 Feb 4 02:23 daygeek5.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:26 daygeek6.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:32 daygeek7.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:32 daygeek8.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:38 daygeek9.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:00 daygeek.txt
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/linux-command-to-create-a-file/
作者:[Vinoth Kumar][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.2daygeek.com/author/vinoth/
[b]: https://github.com/lujun9972
[1]: https://www.2daygeek.com/create-a-file-in-specific-certain-size-linux/

View File

@ -0,0 +1,323 @@
[#]: collector: (lujun9972)
[#]: translator: (dianbanjiu )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (10 Methods To Create A File In Linux)
[#]: via: (https://www.2daygeek.com/linux-command-to-create-a-file/)
[#]: author: (Vinoth Kumar https://www.2daygeek.com/author/vinoth/)
在 Linux 上创建文件的 10 个方法
======
我们都知道,在 Linux 上,包括设备在内的一切都是文件。
Linux 管理员每天应该会多次执行文件创建活动(可能是 20 次50 次,甚至是更多,这依赖于他们的环境)。
如果你想 **[在Linux上创建一个特定大小的文件][1]**,查看前面的这个链接。
高效创建一个文件是非常重要的。为什么我说高效?如果你了解一些高效进行你当前活动的方式,你就可以事半功倍。
这将会节省你很多的时间。你可以把这些有用的时间用到到其他重要的事情上。
我下面将会介绍多个在 Linux 上创建文件的方法。我建议你选择几个简单高效的来辅助你的工作。
你不必安装下列的任何一个命令,因为它们已经作为 Linux 核心工具的一部分安装到你的系统上了。
创建文件可以通过以下六个方式来完成。
* **`重定向符号 (>):`** 标准重定向符允许我们创建一个 0KB 的空文件。
* **`touch:`** 如果文件不存在的话touch 命令将会创建一个 0KB 的空文件。
* **`echo:`** echo 命令通过一个参数显示文本的某行。
* **`printf:`** printf 命令用于显示在终端给定的文本。
* **`cat:`** 它串联并打印文件到标准输出。
* **`vi/vim:`** Vim 是一个向上兼容 Vi 的文本编辑器。它常用于编辑各种类型的纯文本。
* **`nano:`** nano 是一个简小且用户友好的编辑器。它复制了 Pico 的外观和优点,而且还是免费的。
* **`head:`** head 用于打印一个文件开头的一部分。
* **`tail:`** tail 用于打印一个文件的最后一部分。
* **`truncate:`** truncate 用于缩小或者扩展文件的尺寸到指定大小。
### 在 Linux 上使用重定向符(>>)创建一个文件
标准重定向符允许我们创建一个 0KB 的空文件。它通常用于重定向一个命令的输出到一个新文件中。在没有命令的情况下使用重定向符号时,它会创建一个文件。
但是它不允许你在创建文件时向其中输入任何文本。然而它对于不是很勤劳的管理员是非常简单有用的。只需要输入重定向符后面跟着你想要的文件名。
(译者注:在输入下面的命令回车后,该命令并不会立刻中断,回车后你其实还可以继续输入一些文本,而这些文本都会被记录到你所创建的文章中去。在输入完成后,你可以使用 Ctrl+C 或者 Ctrl+D 来结束输入)
```
$ > daygeek.txt
```
使用 ls 命令查看刚刚创建的文件。
```
$ ls -lh daygeek.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:00 daygeek.txt
```
### 在 Linux 上使用 touch 命令创建一个文件
touch 命令常用于将每个文件的访问和修改时间更新为当前时间。
如果指定的文件名不存在将会创建一个新的文件。touch 不允许我们在创建文件的同时向其中输入一些文本。它默认创建一个 0KB 的空文件。
```
$ touch daygeek1.txt
```
使用 ls 命令查看刚刚创建的文件。
```
$ ls -lh daygeek1.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:02 daygeek1.txt
```
### 在 Linux 上使用 echo 命令创建一个文件
echo 内置于大多数的操作系统中。它常用于脚本,批处理文件,以及作为插入文本的单个命令的一部分。
它允许你在创建一个文件时就向其中输入一些文本。当然也允许你在之后向其中输入一些文本。
```
$ echo "2daygeek.com is a best Linux blog to learn Linux" > daygeek2.txt
```
使用 ls 命令查看刚刚创建的文件。
```
$ ls -lh daygeek2.txt
-rw-rw-r-- 1 daygeek daygeek 49 Feb 4 02:04 daygeek2.txt
```
可以使用 cat 命令查看文件的内容。
```
$ cat daygeek2.txt
2daygeek.com is a best Linux blog to learn Linux
```
你可以使用两个重定向符 (>>) 添加其他内容到同一个文件。
```
$ echo "It's FIVE years old blog" >> daygeek2.txt
```
你可以使用 cat 命令查看添加的内容。
```
$ cat daygeek2.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
### 在 Linux 上使用 printf 命令创建一个新的文件
printf 命令也可以以类似 echo 的方式执行。
printf 命令常用来显示在终端窗口给出的字符串。printf 可以有格式说明符,转义序列或普通字符。
```
$ printf "2daygeek.com is a best Linux blog to learn Linux\n" > daygeek3.txt
```
使用 ls 命令查看刚刚创建的文件。
```
$ ls -lh daygeek3.txt
-rw-rw-r-- 1 daygeek daygeek 48 Feb 4 02:12 daygeek3.txt
```
使用 cat 命令查看文件的内容。
```
$ cat daygeek3.txt
2daygeek.com is a best Linux blog to learn Linux
```
你可以使用两个重定向符 (>>) 添加其他的内容到同一个文件中去。
```
$ printf "It's FIVE years old blog\n" >> daygeek3.txt
```
你可以使用 cat 命令查看这个文件中添加的内容。
```
$ cat daygeek3.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
### 在 Linux 中使用 cat 创建一个文件
cat 表示串联concatenate。在 Linux 经常用于读取一个文件中的数据。
cat 是在类 Unix 系统中最常使用的命令之一。 它提供了三个与文本文件相关的功能:显示一个文件的内容,组合多个文件的内容到一个输出以及创建一个新的文件。
(译者注:如果 cat 命令后如果不带任何文件的话,下面的命令在回车后也不会立刻结束,回车后的操作同上面的重定向符中的注解)
```
$ cat > daygeek4.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
使用 ls 命令查看创建的文件。
```
$ ls -lh daygeek4.txt
-rw-rw-r-- 1 daygeek daygeek 74 Feb 4 02:18 daygeek4.txt
```
使用 cat 命令查看文件的内容。
```
$ cat daygeek4.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
如果你想向同一个文件中添加其他内容,使用两个连接的重定向符(>>)。
```
$ cat >> daygeek4.txt
This website is maintained by Magesh M, It's licensed under CC BY-NC 4.0.
```
你可以使用 cat 命令查看添加的内容。
```
$ cat daygeek4.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
This website is maintained by Magesh M, It's licensed under CC BY-NC 4.0.
```
### 在 Linux 上使用 vi/vim 命令创建一个文件
Vim 是一个向上兼容 Vi 的文本编辑器。它通常用来编辑所有种类的纯文本。在编辑程序时特别有用。
vim 中有很多功能可以用于编辑单个文件。
```
$ vi daygeek5.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
使用 ls 查看刚才创建的文件。
```
$ ls -lh daygeek5.txt
-rw-rw-r-- 1 daygeek daygeek 75 Feb 4 02:23 daygeek5.txt
```
使用 cat 命令查看文件的内容。
```
$ cat daygeek5.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
```
### 在 Linux 上使用 nano 命令创建一个文件
Nano 是一个编辑器,它是一个免费的 Pico 的克隆。nano 是一个小且用户友好的编辑器。它复制了 Pico 的外观及优点,并且是一个免费的应用,它添加了 Pico 缺乏的一系列特性,像是打开多个文件,每行滚动,撤销/重做,语法高亮,行号等等。
```
$ nano daygeek6.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
This website is maintained by Magesh M, It's licensed under CC BY-NC 4.0.
```
使用 ls 命令查看创建的文件。
```
$ ls -lh daygeek6.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:26 daygeek6.txt
```
使用 cat 命令来查看一个文件的内容。
```
$ cat daygeek6.txt
2daygeek.com is a best Linux blog to learn Linux
It's FIVE years old blog
This website is maintained by Magesh M, It's licensed under CC BY-NC 4.0.
```
### 在 Linux 上使用 head 命令创建一个文件
head 命令通常用于输出一个文件开头的一部分。它默认会打印一个文件的开头 10 行到标准输出。如果有多个文件,则每个文件前都会有一个标题,用来表示文件名。
```
$ head -c 0K /dev/zero > daygeek7.txt
```
使用 ls 命令查看创建的文件。
```
$ ls -lh daygeek7.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:30 daygeek7.txt
```
### 在 Linux 上使用 tail 创建一个文件
tail 命令通常用来输出一个文件最后的一部分。它默认会打印每个文件的最后 10 行到标准输出。如果有多个文件,则每个文件前都会有一个标题,用来表示文件名。
```
$ tail -c 0K /dev/zero > daygeek8.txt
```
使用 ls 命令查看创建的文件。
```
$ ls -lh daygeek8.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:31 daygeek8.txt
```
### 在 Linux 上使用 truncate 命令创建一个文件
truncate 命令通常用作将一个文件的尺寸缩小或者扩展为某个指定的尺寸。
```
$ truncate -s 0K daygeek9.txt
```
使用 ls 命令检查创建的文件。
```
$ ls -lh daygeek9.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:37 daygeek9.txt
```
在这篇文章中,我使用这十个命令分别创建了下面的这十个文件。
```
$ ls -lh daygeek*
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:02 daygeek1.txt
-rw-rw-r-- 1 daygeek daygeek 74 Feb 4 02:07 daygeek2.txt
-rw-rw-r-- 1 daygeek daygeek 74 Feb 4 02:15 daygeek3.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:20 daygeek4.txt
-rw-rw-r-- 1 daygeek daygeek 75 Feb 4 02:23 daygeek5.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:26 daygeek6.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:32 daygeek7.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:32 daygeek8.txt
-rw-rw-r-- 1 daygeek daygeek 148 Feb 4 02:38 daygeek9.txt
-rw-rw-r-- 1 daygeek daygeek 0 Feb 4 02:00 daygeek.txt
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/linux-command-to-create-a-file/
作者:[Vinoth Kumar][a]
选题:[lujun9972][b]
译者:[dianbanjiu](https://github.com/dianbanjiu)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.2daygeek.com/author/vinoth/
[b]: https://github.com/lujun9972
[1]: https://www.2daygeek.com/create-a-file-in-specific-certain-size-linux/