man touch, cat and less sort and grep cut sed tar find diff uniq chmod
接下来让我们逐一来详细介绍。 1、man命令
第一个你需要知道的Linux命令就是man命令,该命令可以显示指定命令的用法和描述。比如你想知道ls命令的用法和选项,可以在终端执行“man ls”:
语法: man man ls
~# man ls LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is speciâ fied. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with .
cat命令用来查看文件的内容,但是使用cat命令并不能编辑文件的内容,它仅仅是可以浏览文件内容。cat命令不支持键盘上下键翻页。
语法: cat cat demo.txt
同样的less命令也可以让你浏览文件,less命令非常快,并且支持上下键查看文件的开头和末尾。然而more命令和它类似,只是more命令只能用enter键实现文件的向前翻页,不支持回退。
语法: less more
1 mike level intermediate jan 10 lucy level beginer mar 45 Dave level expert dec 4 dennis start beginner jul 7 Megan employee trainee feb 58 Mathew Head CEO nov
~# sort -k2 test.txt 45 Dave level expert dec 4 dennis start beginner jul 10 lucy level beginer mar 58 Mathew Head CEO nov 7 Megan employee trainee feb 1 mike level intermediate jan
~# sort -h test.txt 1 mike level intermediate jan 4 dennis start beginner jul 7 Megan employee trainee feb 10 lucy level beginer mar 45 Dave level expert dec 58 Mathew Head CEO nov
最后一列是月份,你可以使用“-M”选项来让文件内容按月份排序:
~# sort -k5 -M test.txt 1 mike level intermediate jan 7 Megan employee trainee feb 10 lucy level beginer mar 4 dennis start beginner jul 58 Mathew Head CEO nov 45 Dave level expert dec
~# sort -h -r test.txt 58 Mathew Head CEO nov 45 Dave level expert dec 10 lucy level beginer mar 7 Megan employee trainee feb 4 dennis start beginner jul 1 mike level intermediate jan
cut -d' ' -f2 test.txt
~# cut -d' ' -f2 test.txt mike lucy Dave dennis Megan Mathew
下面的例子从/etc/passd file中提取users列:
cut -d':' -f1 /etc/passwd
5、sed命令
sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。
如果你想通过搜索替换文件中的指定内容,你可以使用“s”选项来检索到它然后将它替换。
语法: sed 's///' test.txt
例如,在test.txt文件中用“michael”替换“mike”:
sed 's/mike/michael/' test.txt
~# sed 's/mike/michael/' test.txt 1 michael level intermediate jan 10 lucy level beginer mar 45 Dave level expert dec 4 dennis start beginner jul 7 Megan employee trainee feb 58 Mathew Head CEO nov
6、tar命令
tar命令用来压缩和解压缩文件,其中经常会用到“-cf”和“-xf”选项。
语法: tar
让我们将test.txt文件打包:
tar -cf test.tar test.txt
~# tar -cf test.tar test.txt root@devopscube:~# ls test.tar test.txt
用“-C”选项将刚才打包好的test.tar文件解压缩至“demo”目录:
tar -xf test.tar -C /root/demo/
~# tar -xf test.tar -C /root/demo/ root@devopscube:~# cd demo/ root@devopscube:~/demo# ls test.txt
~# uniq test.txt 1 mike level intermediate jan 10 lucy level beginer mar 45 Dave level expert dec 4 dennis start beginner jul 7 Megan employee trainee feb 58 Mathew Head CEO nov