mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
translated 13
This commit is contained in:
parent
d777e88ef6
commit
4f4dc3b1a9
@ -1,212 +0,0 @@
|
||||
hongchuntang wc
|
||||
|
||||
13 Linux Cat Command Examples To Manage (Display,Sort,Create etc) Files
|
||||
================================================================================
|
||||
![](http://linoxide.com/wp-content/uploads/2013/11/linux-cat-command.png)
|
||||
|
||||
In Linux operating system most of configuration files, logs even shell scripts are in text file format. There is why we have numbers of text editors in Linux. When you just need to see the content of those files, you can use a simple command named cat.
|
||||
|
||||
From cat manual page it says
|
||||
|
||||
> cat is a command that concatenate files and print on the standard output
|
||||
|
||||
Cat is built-in command in Linux. I believe that all of Linux distribution has this Cat command by default. Let’s start to use how to use it.
|
||||
|
||||
### 1. View the content of file ###
|
||||
|
||||
The easiest way to use cat is just type ‘cat file_name’.
|
||||
|
||||
# cat /etc/issue
|
||||
|
||||
CentOS release 5.10 (Final)
|
||||
Kernel \r on an \m
|
||||
|
||||
### 2. Put the line number on the fly ###
|
||||
|
||||
When reading a configuration file it may that you have a long configuration file. It will be easier to if you can put line numbers on the fly. Use -n parameter to fulfill this purpose.
|
||||
|
||||
# cat -n /etc/ntp.conf
|
||||
|
||||
1 # Permit time synchronization our time resource but do not
|
||||
2 # permit the source to query or modify the service on this system
|
||||
3 restrict default kod nomodify notrap nopeer noquery
|
||||
4 restrict -6 default kod nomodify notrap nopeer noquery
|
||||
5
|
||||
6 # Permit all access over the loopback interface. This could be
|
||||
7 # tightened as well, but to do so would effect some of the
|
||||
8 # administration functions
|
||||
9 restrict 127.0.0.1
|
||||
10 restrict -6 ::1
|
||||
|
||||
### 3. Number non-blank output lines ###
|
||||
|
||||
Similar with -n parameter, -b parameter will give you numbers **on the fly**. The difference is -b parameter will only number non-blank lines.
|
||||
|
||||
#cat -n /etc/ntp.conf
|
||||
|
||||
1 # Permit time synchronization our time resource but do not
|
||||
2 # permit the source to query or modify the service on this system
|
||||
3 restrict default kod nomodify notrap nopeer noquery
|
||||
4 restrict -6 default kod nomodify notrap nopeer noquery
|
||||
|
||||
5 # Permit all access over the loopback interface. This could be
|
||||
6 # tightened as well, but to do so would effect some of the
|
||||
7 # administration functions
|
||||
8 restrict 127.0.0.1
|
||||
9 restrict -6 ::1
|
||||
|
||||
### 4. Show tabs ###
|
||||
|
||||
You may need to know where are the tabs on your text file. If you do, you can use -T parameter. Tab will be represented by **^I** characters.
|
||||
|
||||
# cat -T /etc/hosts
|
||||
|
||||
# Do not remove the following line, or various programs
|
||||
# that require network functionality will fail.
|
||||
127.0.0.1^I^Ilocalhost.localdomain localhost
|
||||
::1^I^Ilocalhost6.localdomain6 localhost6
|
||||
|
||||
### 5. Show the end of lines ###
|
||||
|
||||
-E parameter will display **$** at end of each line. Here’s the example :
|
||||
|
||||
# cat -E /etc/hosts
|
||||
|
||||
# Do not remove the following line, or various programs$
|
||||
# that require network functionality will fail.$
|
||||
127.0.0.1 localhost.localdomain localhost$
|
||||
::1 localhost6.localdomain6 localhost6$
|
||||
|
||||
### 6. Show All ###
|
||||
|
||||
If you want to combine between -T and -E, you can use -A parameter.
|
||||
|
||||
# cat -A /etc/hosts
|
||||
|
||||
# Do not remove the following line, or various programs$
|
||||
# that require network functionality will fail.$
|
||||
127.0.0.1^I^Ilocalhost.localdomain localhost$
|
||||
::1^I^Ilocalhost6.localdomain6 localhost6$
|
||||
|
||||
### 7. View the content per page ###
|
||||
|
||||
When your file can not fit in your screen, you can **combine** cat with another command to make it displayed per page. Use the pipe ( | ) sign to combine it.
|
||||
|
||||
# cat /proc/meminfo | less
|
||||
|
||||
# cat /proc/meminfo | more
|
||||
|
||||
The difference between less and more is that you can do scroll up **and** scroll down **on less command** using PageUp and PageDown buttons. While you can **only** do scroll down **on more command** using spacebar.
|
||||
|
||||
### 8. View the contents of 2 files ###
|
||||
|
||||
Let’s say we have 2 text files named linux and desktop located in /root folder. Each of files contains :
|
||||
**Linux** : ubuntu, centos, redhat, mint and slackware
|
||||
**Desktop** : gnome kde, xfce, enlightment, and cinnamon
|
||||
When you want to view more than 1 file simultaneously, you do like this :
|
||||
|
||||
# cat /root/linux /root/desktop
|
||||
|
||||
ubuntu
|
||||
centos
|
||||
redhat
|
||||
mint
|
||||
slackware
|
||||
gnome
|
||||
kde
|
||||
xfce
|
||||
enlightment
|
||||
cinnamon
|
||||
|
||||
### 9. Sorting file ###
|
||||
|
||||
Again, you can combine cat using another command to make a custom output. If you want to sort it, you can combine it with **sort** command. Here’s the example :
|
||||
|
||||
# cat /root/linux | sort
|
||||
|
||||
centos
|
||||
mint
|
||||
redhat
|
||||
slackware
|
||||
Ubuntu
|
||||
|
||||
### 10. Redirect standard output ###
|
||||
|
||||
You can also redirect the output to screen or to another file. Just use > sign (greater-than symbol) if you want to redirect the output into a file.
|
||||
|
||||
# cat /root/linux > /root/linuxdistro
|
||||
|
||||
The above command will **create** a linuxdistro file that has the same content with /root/linux file.
|
||||
|
||||
### 11. Creating a new file ###
|
||||
|
||||
There are some ways to create a text file in Linux. One of them is using cat command.
|
||||
|
||||
# cat > operating_system
|
||||
|
||||
Unix
|
||||
Linux
|
||||
Windows
|
||||
MacOS
|
||||
|
||||
When you type cat > operating system, it will create a file which named operating_system. Then you will see a blank line below the cat command. You can type the content that you want. We typed Unix, Linux, Windows and MacOS for example. When you’re done, **press Ctrl-D** to save the content and exit from cat command. You will see **a file named operating_system** is created in the current folder with the content that you add before.
|
||||
|
||||
### 12. Append the content of file ###
|
||||
|
||||
If you use the > sign twice, it means that the content of the first file will be added to the second file. Let’s see the example :
|
||||
|
||||
# cat /root/linux >> /root/desktop
|
||||
|
||||
# cat /root/desktop
|
||||
|
||||
The first cat command will add the content of /root/linux to /root/desktop file
|
||||
The second command will show you this output :
|
||||
|
||||
ubuntu
|
||||
centos
|
||||
redhat
|
||||
mint
|
||||
slackware
|
||||
gnome
|
||||
kde
|
||||
xfce
|
||||
enlightment
|
||||
cinnamon
|
||||
|
||||
### 13. Redirect standard input ###
|
||||
|
||||
You can also redirect standard input using **<** sign (less-than symbol).
|
||||
|
||||
# cat < /root/linux
|
||||
|
||||
The above command means that /root/linux will be an input for cat command. The output will be like this :
|
||||
|
||||
ubuntu
|
||||
centos
|
||||
redhat
|
||||
mint
|
||||
slackware
|
||||
|
||||
To make it clearer, let’s see another example :
|
||||
|
||||
# cat < /root/linux | sort > linux-sort
|
||||
|
||||
That command will be read : **sort content from /root/linux file then print the output to linux-sort file**
|
||||
|
||||
So the output of linux-sort file will be like this :
|
||||
|
||||
centos
|
||||
mint
|
||||
redhat
|
||||
slackware
|
||||
ubuntu
|
||||
|
||||
That’s some examples of cat command on day-to-day operation. Of course you can explore more detail from cat manual page and experiment with Input-Output redirection operator.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://linoxide.com/linux-command/13-cat-command-examples/
|
||||
|
||||
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
@ -0,0 +1,212 @@
|
||||
|
||||
13 Linux Cat命令管理(显示,排序,建立)文件实例
|
||||
================================================================================
|
||||
![](http://linoxide.com/wp-content/uploads/2013/11/linux-cat-command.png)
|
||||
|
||||
Linux系统中,许多配置文件,Logs文件,甚至shell脚本都使用文本文件格式,因此,Linux系统存在着多种文本编辑器,但当你仅仅想要查看一下它们里的内容时,可使用cat命令。
|
||||
|
||||
cat手册里这样描述:
|
||||
|
||||
> cat命令读取文件内容,并输出到标准设备上面
|
||||
|
||||
cat是一条linux内置命令. 几乎所有linux发行版都内置.接下来请跟随我来一起使用它.
|
||||
|
||||
### 1. 显示文件内容 ###
|
||||
|
||||
最简单的方法是直接输入‘cat file_name’.
|
||||
|
||||
# cat /etc/issue
|
||||
|
||||
CentOS release 5.10 (Final)
|
||||
Kernel \r on an \m
|
||||
|
||||
### 2. 在行首显示行号 ###
|
||||
|
||||
当在读取内容很多的配置文件时,加上-n参数可实现在行首显示行号。
|
||||
|
||||
|
||||
# cat -n /etc/ntp.conf
|
||||
|
||||
1 # Permit time synchronization our time resource but do not
|
||||
2 # permit the source to query or modify the service on this system
|
||||
3 restrict default kod nomodify notrap nopeer noquery
|
||||
4 restrict -6 default kod nomodify notrap nopeer noquery
|
||||
5
|
||||
6 # Permit all access over the loopback interface. This could be
|
||||
7 # tightened as well, but to do so would effect some of the
|
||||
8 # administration functions
|
||||
9 restrict 127.0.0.1
|
||||
10 restrict -6 ::1
|
||||
|
||||
### 3. 在行首显示非空行号 ###
|
||||
|
||||
类似于-n参数,-b也在行首显示行号.但它显示的行号为非空行行号
|
||||
|
||||
#cat -b /etc/ntp.conf
|
||||
|
||||
1 # Permit time synchronization our time resource but do not
|
||||
2 # permit the source to query or modify the service on this system
|
||||
3 restrict default kod nomodify notrap nopeer noquery
|
||||
4 restrict -6 default kod nomodify notrap nopeer noquery
|
||||
|
||||
5 # Permit all access over the loopback interface. This could be
|
||||
6 # tightened as well, but to do so would effect some of the
|
||||
7 # administration functions
|
||||
8 restrict 127.0.0.1
|
||||
9 restrict -6 ::1
|
||||
|
||||
### 4. 显示tab制表符 ###
|
||||
|
||||
当你想要显示文本中的tab制表位时. 可使用-T参数. 它会在输入结果中标识为 **^I** .
|
||||
|
||||
# cat -T /etc/hosts
|
||||
|
||||
# Do not remove the following line, or various programs
|
||||
# that require network functionality will fail.
|
||||
127.0.0.1^I^Ilocalhost.localdomain localhost
|
||||
::1^I^Ilocalhost6.localdomain6 localhost6
|
||||
|
||||
### 5. 显示换行符 ###
|
||||
|
||||
-E参数在每行结尾标识 **$** .如下所示 :
|
||||
|
||||
# cat -E /etc/hosts
|
||||
|
||||
# Do not remove the following line, or various programs$
|
||||
# that require network functionality will fail.$
|
||||
127.0.0.1 localhost.localdomain localhost$
|
||||
::1 localhost6.localdomain6 localhost6$
|
||||
|
||||
### 6. 同时显示制表符及换行符 ###
|
||||
|
||||
当你想要同时达到-T及-E的效果, 可使用-A参数.
|
||||
|
||||
# cat -A /etc/hosts
|
||||
|
||||
# Do not remove the following line, or various programs$
|
||||
# that require network functionality will fail.$
|
||||
127.0.0.1^I^Ilocalhost.localdomain localhost$
|
||||
::1^I^Ilocalhost6.localdomain6 localhost6$
|
||||
|
||||
### 7. 每页满屏显示 ###
|
||||
|
||||
当文件内容超过一屏显示范围时,可结合cat命令与其它命令满屏显示.使用管道符 ( | ).
|
||||
|
||||
# cat /proc/meminfo | less
|
||||
|
||||
# cat /proc/meminfo | more
|
||||
|
||||
在less与more显示结果的区别在于less参数可pageup及pagedown上下翻滚.而more仅能使用空格向下翻屏.
|
||||
|
||||
### 8. 同时查看2个文件中的内容 ###
|
||||
|
||||
位于/root文件夹里有两人文件取名linux及desktop,每个文件含有以下内容 :
|
||||
**Linux** : ubuntu, centos, redhat, mint and slackware
|
||||
**Desktop** : gnome kde, xfce, enlightment, and cinnamon
|
||||
当你想同时查看两文件中的内容时,可按如下方法 :
|
||||
|
||||
# cat /root/linux /root/desktop
|
||||
|
||||
ubuntu
|
||||
centos
|
||||
redhat
|
||||
mint
|
||||
slackware
|
||||
gnome
|
||||
kde
|
||||
xfce
|
||||
enlightment
|
||||
cinnamon
|
||||
|
||||
### 9. 排序显示 ###
|
||||
|
||||
类似. 你也可结合 **sort**管道符对内容进行排序显示. 举例 :
|
||||
|
||||
# cat /root/linux | sort
|
||||
|
||||
centos
|
||||
mint
|
||||
redhat
|
||||
slackware
|
||||
Ubuntu
|
||||
|
||||
### 10. 输入重定向 ###
|
||||
|
||||
你也可将显示结果输出重定向到屏幕或另一个文件. 只需要使用 > 符号即可输出生成到另一个文件.
|
||||
|
||||
# cat /root/linux > /root/linuxdistro
|
||||
|
||||
以上命令会生成一个与/root/linux内容一模一样的叫linuxdistro的文件.
|
||||
|
||||
### 11. 新建文件 ###
|
||||
|
||||
Linux下有多种方法新建文件. 其中使用cat就是方法之一.
|
||||
|
||||
# cat > operating_system
|
||||
|
||||
Unix
|
||||
Linux
|
||||
Windows
|
||||
MacOS
|
||||
|
||||
当你打入cat > operating_system,它会生成一个operating_system的文件. 然后下面会显示空行. 此时你可输入内容.比如我们输入Unix, Linux, Windows and MacOS. 输入完成后, **按Ctrl-D**存盘退出cat. 此时你会发现当前文件夹下会生成一个包含你刚才输入内容的叫 **operating_system**的文件.
|
||||
|
||||
### 12.向文件中追加内容 ###
|
||||
|
||||
当你两次使用 >符时, 会将第一个文件中的内容追加到第二个文件的末尾. 举例 :
|
||||
|
||||
# cat /root/linux >> /root/desktop
|
||||
|
||||
# cat /root/desktop
|
||||
|
||||
它会将 /root/linux的内容追加到/root/desktop文件的末尾
|
||||
第二个文件的内容将会变成这样:
|
||||
|
||||
gnome
|
||||
kde
|
||||
xfce
|
||||
enlightment
|
||||
cinnamon
|
||||
ubuntu
|
||||
centos
|
||||
redhat
|
||||
mint
|
||||
slackware
|
||||
|
||||
### 13. 重定向输入 ###
|
||||
|
||||
你可使用 **<**命令将文件输入到cat中 .
|
||||
|
||||
# cat < /root/linux
|
||||
|
||||
上面命令表示 /root/linux中的内容作为cat的输入. 屏幕上显示如下 :
|
||||
|
||||
ubuntu
|
||||
centos
|
||||
redhat
|
||||
mint
|
||||
slackware
|
||||
|
||||
为了更清楚表示它的意义,我们使用以下命令 :
|
||||
|
||||
# cat < /root/linux | sort > linux-sort
|
||||
|
||||
此命令这样理解: **从/root/linux中读取内容,然后排序,将结果输出并生成linux-sort新文件**
|
||||
|
||||
然后我们看看linux-sort中的内容 :
|
||||
|
||||
centos
|
||||
mint
|
||||
redhat
|
||||
slackware
|
||||
ubuntu
|
||||
|
||||
以上是一些cat命令的日常基本应用. 更多相关你可从cat命令手册中学到并记得经常练习它们.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://linoxide.com/linux-command/13-cat-command-examples/
|
||||
|
||||
译者:[hongchuntang](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user