linux基本命令總結(二)

三、基本命令-2
基本命令-2
壓縮和歸檔
打包:即歸檔,相似於旅遊以前收拾行李
壓縮:爲了減小佔用的磁盤空間,能夠作備份,在網絡上傳輸時節省網絡帶寬。
打包壓縮軟件
windows:winrar 360壓縮 好壓 7zip winzip
linux:壓縮格式:gz ,bz2,xz,zip,Z 壓縮算法不一樣,致使壓縮比不一樣
壓縮軟件 gzip bzip2 xz zip 既能打包又能壓縮的軟件:tar * 1、zip:
壓縮後的文件通常以.zip結尾,能夠壓縮目錄
壓縮的語法: zip filename.zip file1 file2 ...
zip 壓縮後的文件名 待壓縮文件
壓縮後不刪除原文件
例如 [root@server150 acltest]# zip com.zip com.txt
adding:com.txt (deflated 99%)
解壓縮:unzip
-d:指定解壓路徑
2、gzip
1)gzip /path/to/somefile 默認會刪除原文件
-d 解壓縮
-#: 指定壓縮比,壓縮比越小,速度越大
2)gunzip /path/to/some_compress_file
3)zcat some_compress_file 不解壓的狀況下查看文本的壓縮文件
例子:#cp /var/log/messages ./
gzip messages 默認後綴名:.gz
gzip壓縮時,原文件消失,生成壓縮文件
解壓:gunzip
gzip的壓縮包,在解壓後,壓縮包消失,解壓後的文件出現。
壓縮實際上是有級別的:1~9 1級別最低,速度最快,效率最低;9級別最高,速度最慢,效率最高。
默認級別是6。
3、bzip2 默認狀況下,壓縮完成,
原文件也是消失的,壓縮包必須以.bz2結尾的
一般可以生成比使用gzip壓縮更小的文件(壓縮比較高)
1)bzip2 /path/to/somefile
-d:解壓
-#:指定壓縮比
-k:保留原文件
2)bunzip2 /path/to/somefile
3)bzcat /path/to/some_compress_file 不解壓查看
解壓縮:bunzip2 四. xz 壓縮比更大
1)壓縮 xz somefile
2)解壓
unxz 或 xzdec
-d:解壓
-k:保留原文件
-c:輸入到屏幕
3)xzcat 不解壓查看
之後常見的壓縮包的格式
.zip .tar.gz .tar.bz2
5、tar
既能夠打包,又能夠壓縮
tar 選項 包名 打包的文件或目錄 //切記:必定要注意語法格式,先是打包後的名字,而後纔是要打包的東西
tar: 歸檔工具, .tar 例如:tar -cf
-c: 建立歸檔文件
-f FILE.tar: 操做的歸檔文件
-x: 展開歸檔
--xattrs: 歸檔時,保留文件的擴展屬性信息
-t: 不展開歸檔,直接查看歸檔了哪些文件
-C:解壓時指定路徑
-r:向包中追加文件
-p:保留權限信息
-v:顯示詳細過程
-zcf: 歸檔並調用gzip壓縮
-zxf: 調用gzip解壓縮並展開歸檔,-z選項可省略
-jcf: bzip2
-jxf:
-Jcf: xz
-Jxf:
1)打包壓縮同時進行
-z:表示使用gzip壓縮方式壓縮或者解壓縮
-j:表示使用bzip2壓縮方式壓縮或者解壓縮
-c:表示建立 --create
-v:顯示詳細過程
-f:指定文件,通常後面跟包名
-zcvf zcvf .tar.gz
-jcvf jcvf .tar.bz2node

tar zcvf com.tar.gz com.txt

com.txtlinux

ll

total 67968
-rw-r--r-- 1 root root 367957 Jul 30 09:24 com.tar.gzweb

tar zcvf /tmp/acltest.tar.gz /acltest/

2)解包 .tar.gz .tar.bz2
-zxvf zxvf
-jxvf jxvf
-C:指定解壓路徑算法

tar zxvf com.tar.gz -C /usr/local/src/

ls /usr/local/src/

com.txt vmware-tools-distrib
3)其餘選項
-t:不解包查看包中的內容shell

tar -tf /tmp/acltest.tar.gz

acltest/
acltest/f1
acltest/com.txt
acltest/f3
acltest/f2
acltest/com.zip
acltest/com.tar.gz
-r:向包中追加文件,不能追加壓縮的文件
tar -rf 包名 追加的文件
三.文件搜索
which:用來查找命令的絕對路徑
-- 顯示shell命令的絕對路徑
-- 僅僅會在PATH變量中搜索要查找的命令
-- 搜索時先查找別名,而後從PATH中查找
一、查看用戶的PATH變量:命令的搜索路徑數據庫

echo $PATH

/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin

command not found可能緣由:
1)敲錯了
2)命令沒有安裝
3)命令所在路徑沒在PATH變量的定義中

cp which vim /tmp/vim2

vim2 /etc/passwd

bash: vim2:command not foundvim

/tmp/vim2 /etc/passwd //絕對路徑執行

二、添加路徑到PATH
1)臨時修改PATH值windows

PATH=$PATH:/tmp //$PATH:保留變量原有值

echo $PATH

/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin:/tmp
2)永久修改PATH值 工做中必定會用的
/etc/profile //全局配置文件,對全部用戶生效
~username/.bash_profile //局部配置文件,只對特定用戶生效bash

vim /root/.bash_profile

PATH=$PATH:$HOME/bin:/tmp 上述文件不是即時生效的,正常狀況下,它是用戶登陸時執行的。網絡

source /root/.bash_profile //從新讀取配置文件,使修改生效

echo $PATH

/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin:/root/bin:/tmp
弊端:每次新開啓一個終端或標籤,都須要執行# source /root/.bash_profile 若是想一勞永逸,那麼須要退出系統,從新登陸,即註銷。
System ——> Log out root ——> Log out

echo $PATH

/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin:/tmp

which ls

alias ls='ls --color=auto'
/bin/ls

which vim

/usr/bin/vim 命令的別名: alias
一、查看當前系統中有哪些別名(root用戶和普通用戶的別名可能不同)

alias

二、設置命令的別名
1)臨時

alias vi='vim'

vi /etc/passwd //執行vi時候,實際上執行的是vim

2)永久,改文件
(1)/root/.bashrc cp rm mv (2) /etc/profile.d
colorls.sh
which2.sh
三、取消別名
[ profile.d]# unalias vi
[ profile.d]# vi /etc/passwd //沒顏色了
locate
-- 經過文件名檢索文件,檢索速度最快
-- 全部可以檢索的東西,都是存放在數據庫中的
-- locate侷限性,有的文件系統、有的文件及有的目錄默認是不會搜索的
一、假設我知道網卡配置文件的名字,可是不知道具體路徑:

locate ifcfg-eth0

/etc/sysconfig/network-scripts/ifcfg-eth0

locate ifcfg

/etc/dbus-1/system.d/nm-ifcfg-rh.conf
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-lo
/sbin/ifcfg
/usr/lib64/NetworkManager/libnm-settings-plugin-ifcfg-rh.so
/usr/share/man/man8/ifcfg.8.gz
/var/log/anaconda.ifcfg.log
二、手動更新數據庫

cp which vim /root/vim3

locate vim3 //未查詢到結果

緣由:由於locate的數據庫是一天一更新,不是實時更新的。

updatedb

locate vim3

/root/vim3 數據庫文件:/var/lib/mlocate/mlocate.db
---------------------------------------------------------- 報錯:
1)數據庫文件不存在
2)手動生成它

updatedb


三、locate數據庫配置文件

vim /etc/updatedb.conf

ls /tmp/vim2

/tmp/vim2

locate vim2 //搜索不到,由於/tmp在排除列表中

find *****
-- 全局性搜索文件
-- 工做方式:沿着文件的層次結構依次向下搜索,找到符合條件的,打印或者是執行相應的操做
1、語法格式
find 要搜索路徑 條件(選項) [動做]
一、基本例子

find /etc/ -name network

/etc/vmware-tools/scripts/vmware/network
/etc/sysconfig/network
/etc/rc.d/init.d/network 通常狀況下,查找範圍越大,目錄層級越深,查找速度就越慢。

mkdir -p /q/w/e/r/t/y/u/i/o/p/a/s/d/f/g/h/j/k/l/z/x/c/v/b/n/m

touch /q/w/e/r/t/y/u/i/o/p/a/s/d/f/g/h/j/k/l/z/x/c/v/b/n/m/test.txt

time find / -name test.txt

/q/w/e/r/t/y/u/i/o/p/a/s/d/f/g/h/j/k/l/z/x/c/v/b/n/m/test.txt
/acl/test.txt
/tmp/test.txt
real 0m3.188s
user 0m0.061s
sys 0m2.953s 練習:
查找/etc目錄下名字爲config的文件

find /etc -name config

/etc/vmware-tools/config
/etc/selinux/config
二、按條件查找
1)按照文件名搜索
-name:按名字查找 *****
-iname:忽略大小寫

find /etc -name networkmanager

find /etc -iname networkmanager

/etc/rc.d/init.d/NetworkManager
/etc/NetworkManager
通配符:* 表明任意字符
? 表明單個字符
查找/etc目錄下,全部以.conf結尾的文件

find /etc/ -name *.conf 查找/etc/目錄下,以.conf結尾,名稱是5個字符的

find /etc/ -name "?????.conf" 練習:
查找/etc目錄下,名字爲ntp.conf的文件

find /etc -name ntp.conf

/etc/ntp.conf 查找 / 目錄下,名字爲passwd的文件

find / -name passwd

2)按照文件類型查找 -type
f:普通文件
b c d l s p

find /var -type l //查找/var目錄下類型是軟連接的文件

ll find /var -type l //驗證是否查找到的都是軟連接

find /tmp/ -type f


-type 後面只能跟一個字母
----------------------- 練習: 查找系統中類型是套接字的文件

find / -type s 查找的時候,可能會遇到No such file or directory的錯誤提示,是正常的,若不想看到能夠將錯誤重定向到「黑洞」(/dev/null)

find / -type s 2> /dev/null

3)按照時間查找 (筆試題)
-atime n 以天爲單位
-ctime n
-mtime n
-amin n 以分鐘爲單位
-cmin n
-mmin n
n爲數字,前面能夠帶+或者-號 -mtime n
+n:n+1天以前
n:n到n+1天之間
-n:n天之內
以n等於7爲例:
搜索最近七天內被訪問過的全部文件
find . -type f -atime -7 搜索剛好在七天前被訪問過的全部文件
find . -type f -atime 7 搜索超過七天內被訪問過的全部文件
find . -type f -atime +7 搜索訪問時間超過10分鐘的全部文件
find . -type f -amin +10 找出比file.log修改時間更長的全部文件
find . -type f -newer file.log
---------------------------------------------------------------------- 插曲:
查看文件的屬性信息? stat 文件名

stat passwd

File: `passwd'
Size: 1030 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 917613 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-08-08 20:38:22.080984537 +0800 Modify: 2015-08-08 20:38:22.080984537 +0800
Change: 2015-08-08 20:38:22.080984537 +0800 一個文件有三個時間:
atime:訪問時間,cat more less ... ...
mtime:文件的內容發生變化的時間 vim ... ... (也是 ll 所顯示的時間)
ctime:文件的屬性發生變化的時間 好比:權限改變、大小改變、全部者、所屬組等改變
例子:

echo hello > file

stat file

File: `file'
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 917619 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-08-13 12:16:19.494018488 +0800 Modify: 2015-08-13 12:16:19.495018470 +0800
Change: 2015-08-13 12:16:19.495018470 +0800

cat file

hello

stat file

File: `file'
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 917619 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-08-13 12:17:32.381018468 +0800 Modify: 2015-08-13 12:16:19.495018470 +0800
Change: 2015-08-13 12:16:19.495018470 +0800

chmod +x file

stat file

File: `file'
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 1197781 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-08-13 12:17:32.381018468 +0800 Modify: 2015-08-13 12:16:19.495018470 +0800
Change: 2015-08-13 12:21:55.563018148 +0800 //ctime發生變化

echo hahaha >> file

stat file

File: `file'
Size: 13 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 1197781 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-08-13 12:17:32.381018468 +0800 Modify: 2015-08-13 12:23:54.286017831 +0800
Change: 2015-08-13 12:23:54.286017831 +0800 筆試題
一、打印文件的數字權限

stat -c %a file

755
二、打印文件的字母權限

stat -c %A file

-rwxr-xr-x
---------------------------------------------------------------------- 按時間查找小例子

mkdir /find

date

Thu Aug 13 14:22:43 CST 2015

cd /find/

[ find]# touch -t 08131425.30 f0813 //-t:指定文件的建立時間 MMDDHHmm.SS
[ find]# touch -t 08121425.50 f0812
[ find]# touch -t 08111426.30 f0811
[ find]# touch -t 08101426.30 f0810
[ find]# ll f*
-rw-r--r-- 1 root root 0 Aug 10 14:26 f0810
-rw-r--r-- 1 root root 0 Aug 11 14:26 f0811
-rw-r--r-- 1 root root 0 Aug 12 14:25 f0812
-rw-r--r-- 1 root root 0 Aug 13 14:25 f0813
-- 查找/find下修改時間在24小時(1天)以內的文件
[ find]# find . -mtime -1
.
./f0813
-- 查找/find下修改時間在2天前的普通文件
[ find]# find . -type f -mtime +1
./f0810
./f0811
4)按照用戶和組查找
-user 用戶名
-group 組名
-uid uid
-gid gid
-nouser:孤兒文件 沒有全部者的文件
-nogroup:沒有所屬組的文件
--查找系統中全部者是quota2的文件
[ home]# find / -user quota2 -type f
[ home]# find / -user quota2 -type f 2>/dev/null
-- 查找系統中的孤兒文件
[ home]# userdel quota2
[ home]# find . -type f -nouser
./quota2/.bash_history
./quota2/.bashrc
./quota2/.bash_profile
./quota2/.bash_logout
[ home]# ll find . -type f -nouser
-rw------- 1 502 quota2 29 Aug 11 10:16 ./quota2/.bash_history
-rw-r--r-- 1 502 quota2 18 Aug 29 2012 ./quota2/.bash_logout
-rw-r--r-- 1 502 quota2 176 Aug 29 2012 ./quota2/.bash_profile
-rw-r--r-- 1 502 quota2 124 Aug 29 2012 ./quota2/.bashrc
-- 查找系統中全部者不是root的普通文件 ! 或者 -not
[ home]# find / ! -user root -type f 或者 -or 或者 -o
-- 查找系統中全部者不是root或者類型是套接字的文件
[ home]# find / ! -user root -o -type s
5)按照權限查找 -perm
+222 或者 (用戶可寫or組可寫or其餘人可寫) 二進制中有1的位置,只要知足其中一個位就能夠
-222 而且 (用戶可寫and組可寫and其餘人可寫) 二進制中有1的位置必須都要有1

rm -f /find/*

cd /find/

[ find]# touch p{r,w,x}_{1,2,3}
[ find]# chmod 400 pr_1
[ find]# chmod 440 pr_2
[ find]# chmod 444 pr_3
[ find]# chmod 200 pw_1
[ find]# chmod 220 pw_2
[ find]# chmod 222 pw_3
[ find]# chmod 100 px_1
[ find]# chmod 110 px_2
[ find]# chmod 111 px_3
[ find]# ll find ./ -perm +020 -type f
--w--w---- 1 root root 0 Aug 13 15:13 ./pw_2
--w--w--w- 1 root root 0 Aug 13 15:13 ./pw_3
[ find]# ll find ./ -perm -020 -type f
--w--w---- 1 root root 0 Aug 13 15:13 ./pw_2
--w--w--w- 1 root root 0 Aug 13 15:13 ./pw_3 當權限位只有一位的時候,+和-是同樣的。
[ find]# ll find ./ -perm -222 -type f
--w--w--w- 1 root root 0 Aug 13 15:13 ./pw_3
[ find]# ll find ./ -perm +222 -type f
--w------- 1 root root 0 Aug 13 15:13 ./pw_1
--w--w---- 1 root root 0 Aug 13 15:13 ./pw_2
--w--w--w- 1 root root 0 Aug 13 15:13 ./pw_3 思考:查找系統中擁有suid權限的文件
6)按照文件大小查找 -size

  • 大於
  • 小於
    直接數字 等於
    ‘b’ for 512-byte blocks (this is the default if no suffix is used) //0.5KB
    ‘c’ for bytes
    ‘w’ for two-byte words
    ‘k’ for Kilobytes (units of 1024 bytes)
    ‘M’ for Megabytes (units of 1048576 bytes)
    ‘G’ for Gigabytes (units of 1073741824 bytes)
    [ find]# rm -f /find/*
    [ find]# dd if=/dev/zero of=f1M bs=1M count=1
    [ find]# dd if=/dev/zero of=f2M bs=1M count=2
    [ find]# dd if=/dev/zero of=f3M bs=1M count=3
    [ find]# dd if=/dev/zero of=f4M bs=1M count=4
    [ find]# find . -type f -size -3M
    ./f2M
    ./f1M
    [ find]# find . -type f -size 3M
    ./f3M
    [ find]# find . -type f -size +3M
    ./f4M
    三、動做
    -exec 動做 -- 找到結果以後直接執行動做
    -ok 動做 -- 執行動做以前先提示,即須要交互
    [ find]# find . -type f -size +3M -exec ls -l {} \;
    -rw-r--r-- 1 root root 4194304 Aug 13 15:51 ./f4M
    {} —— 用來代替找到的結果
    \; —— 表示結束標誌
    [ find]# find . -type f -size +3M -ok ls -l {} \;
    < ls ... ./f4M > ? y
    -rw-r--r-- 1 root root 4194304 Aug 13 15:51 ./f4M
    [ find]# find . -type f -size +3M -ok ls -l {} \;
    < ls ... ./f4M > ? n 練習:
    一、查找/find目錄下,類型是 普通文件的文件將其移動到/test目錄下
    [ find]# find . -type f -exec mv {} /test \;
    [ find]# ls /test/
    f1M f2M f3M f4M 或者
    [ find]# mv find . -type f /test二、查找/test目錄下類型爲普通文件的文件,對其進行備份,備份文件的後綴名爲.bak[ find]# find /test -type f -exec cp {} {}.bak \;[ find]# ls /test/f1M f1M.bak f2M f2M.bak f3M f3M.bak f4M f4M.bak三、刪除/test目錄下修改時間在一天之內的普通文件[ find]# find /test/ -type f -mtime -1 -exec rm {} \;grep文本過濾一.grep:目的是過濾出用戶感興趣的內容 ***語法:grep [選項] 模式或關鍵字 文件列表簡單例子:[ loring ~]# grep root /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin一、--color 帶顏色顯示匹配到的關鍵字[ loring ~]# grep --color root /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin二、-i 忽略大小寫[ loring tmp]# grep --color -i root /find/passwdRoot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin三、-v 取反過濾出不包含nologin的行[ loring tmp]# grep -v nologin /etc/passwd四、^ 以某關鍵字開頭顯示/root/.bashrc文件中的非註釋行[ loring tmp]# grep -v ^# /root/.bashrc五、$ 以某關鍵字結尾顯示passwd文件中以sh結尾的行[ loring tmp]# grep sh$ /etc/passwd六、^$ 空行顯示/root/.bashrc文件中的非註釋行和非空行[ loring tmp]# grep -v ^# /root/.bashrc | grep -v ^$七、-c count,統計匹配到的行數[ loring tmp]# grep -c root /etc/passwd2八、 -l 通常和-r聯用,只顯示包含關鍵字的文件的名字,而不是顯示文件內容九、-r 遞歸檢索顯示test目錄下文件內容中含有root的文件名[ loring tmp]# grep -rl root /test十、-q quiet 靜默輸出 通常在寫腳本時候用[ loring tmp]# grep -q root /etc/passwd[ loring tmp]# echo $? //$?表示上一條命令的執行結果0 返回結果爲0:表示上一條命令的執行時成功的返回結果非0:表示上一條命令執行失敗[ loring tmp]# grep -q jsjdjjdfhfh /etc/passwd[ loring tmp]# echo $?1[ loring tmp]# grep -q root /asdafgrep: /asdaf: No such file or directory[ loring tmp]# echo $? //文件不存在返回22十一、-n 顯示匹配行的行號[ loring tmp]# grep -n root /etc/passwd1:root:x:0:0:root:/root:/bin/bash11:operator:x:11:0:operator:/root:/sbin/nologingrep練習:一、顯示/etc/group文件中含有root的行[ loring test]# grep root /etc/group二、顯示/etc/passwd文件中以rp開頭的行[ loring test]# grep ^rp /etc/passwd三、顯示/etc/group文件中不以:爲結尾的行[ loring test]# grep -v :$ /etc/group四、顯示/etc/rc.local文件中的空行,要求帶行號[ loring test]# grep -n ^$ /etc/rc.local6:五、顯示僅/mnt/cdrom目錄下的文件類型爲目錄的文件(前提是光盤已掛載,且不使用find)[ loring test]# ll /mnt/cdrom/ | grep ^d 2、cut:就是截取的意思,它的處理對象是「一行」文本,能夠從中選取出用戶所須要的部分,不影響原文件能夠指定分隔符,而後打印出以分隔符隔開的具體某一列或某幾列語法:cut -f 指定的列 -d ‘分隔符’-d:指定字段分隔符-f:指定要輸出的區域,多個之間用逗號分隔-c:指定列的字符顯示/etc/passwd文件中的用戶名和uid字段[ loring test]# cut -d: -f1,3 /etc/passwd[ loring test]# head -5 /etc/passwd | cut -d: -f1,3root:0bin:1daemon:2adm:3lp:4[ loring test]# head -5 /etc/passwd > t1[ loring test]# vim t1:%s/:/ /g[ loring test]# cat t1root x 0 0 root /root /bin/bashbin x 1 1 bin /bin /sbin/nologindaemon x 2 2 daemon /sbin /sbin/nologinadm x 3 4 adm /var/adm /sbin/nologinlp x 4 7 lp /var/spool/lpd /sbin/nologin 以空格爲分隔,取出第一個字段[ loring test]# cut -d" " -f1 t1rootbindaemonadmlp取出/etc/group文件中組名的字段cut -d: -f1 /etc/group 打印/etc/passwd 文件中每行的第1-5個字符,以及第7-10個字符的內容[root@web test]# cat /etc/passwd | cut -c 1-5,7-103、sort 排序-t:指定字段分隔符-k:指定第幾個字段-n:按照數字順序排序-r:反向排序 reverse-u:排序後重復行只打印一次 unique[root@web test]# cat sort.txtb:3c:2a:4e:5d:1f:11 對輸出內容直接排序,默認按照每行的第一個字符進行排序[root@web test]# cat sort.txt | sorta:4b:3c:2d:1e:5f:11 對輸出內容進行反向排序[root@web test]# cat sort.txt | sort -rf:11e:5d:1c:2b:3a:4使用「:」作分隔符,對第2個字段進行排序[root@web test]# cat sort.txt | sort -t ":" -k 2d:1f:11c:2b:3a:4e:5使用「:」作分隔符,對第2個字段進行排序,按照數字大小排序[root@web test]# cat sort.txt | sort -t ":" -k 2 -nd:1c:2b:3a:4e:5f:11 對/etc/passwd文件按照uid來排序[ loring test]# sort -t ":" -k 3 -n /etc/passwd對passwd文件按照uid由大到小的順序排序[ loring test]# sort -t ":" -k 3 -nr /etc/passwd按照gid由小到大的順序打印/etc/group[ loring test]# sort -t: -k 3 -n /etc/group4、 uniq 去重,惟一去除相鄰重複行-c: 顯示重複的行數-i: 忽略大小寫[ loring test]# uniq num.txt111222333444222555 使用uniq時,通常先排序,再去重[ loring test]# sort num.txt | uniq111222333444555[ loring test]# sort num.txt | uniq -c1 1113 2222 3331 4441 555 5、tr 主要做用在於文本轉換或者刪除。將/etc/passwd文件中的小寫字母轉換成大寫字母[root@web test]# cat /etc/passwd | tr '[a-z]' '[A-Z]' 將/etc/passwd文件中的「:」刪除掉[root@web test]# cat /etc/passwd | tr -d ":" 6、paste 文本合併。將文件按照行進行合併,中間使用tab隔開。[root@web test]# cat a.txt12345[root@web test]# cat b.txtabcde按照行合併文件[root@web test]# paste a.txt b.txt1 a2 b3 c4 d5 e 也可使用-d指定合併文件時行間的分隔符[root@web test]# paste -d:a.txt b.txt1:a2:b3:c4:d5:e[root@web test]#練習:統計/etc/passwd文件中一共有幾種shell,並顯示每種shell有幾個用戶[ loring test]# cut -d: -f7 /etc/passwd | sort | uniq -c
相關文章
相關標籤/搜索