3、基本命令-2

基本命令-2

壓縮和歸檔

打包:

? ? 即歸檔,類似于旅游之前收拾行李

壓縮:

? ? 為了減少占用的磁盤空間,可以做備份,在網絡上傳輸時節省網絡帶寬。


打包壓縮軟件

? ? windows:winrar 360壓縮 好壓 7zip winzip

? ? linux:壓縮格式:gz ,bz2,xz,zip,Z

? 壓縮算法不同,導致壓縮比不同

? ? ? ? ? 壓縮軟件 gzip bzip2? xz? zip

? ? ? ? ? 既能打包又能壓縮的軟件:tar ****


一、zip:

? 壓縮后的文件一般以.zip結尾,可以壓縮目錄

? ? 壓縮的語法:zip filename.zip? file1 file2 ...

zip 壓縮后的文件名 待壓縮文件

壓縮后不刪除原文件

archive:既歸檔又壓縮

? ? [root@server150 acltest]# zip com.zip com.txt

? ? ? ? adding: com.txt (deflated 99%)


? ? 解壓縮:unzip

-d:指定壓縮路徑



二、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。

三、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? 不解壓查看


小實驗:比較bzip2壓縮和gzip壓縮后文件的大小

以后常見的壓縮包的格式? ?

? ? .zip? .tar.gz? .tar.bz2

五、tar? ***? 既可以打包,又可以壓縮

? ? tar 選項 包名 打包的文件或目錄? //切記:一定要注意語法格式,先是打包后的名字,然后才是要打包的東西

tar: 歸檔工具, .tar

例如:tar -cf

-c: 創建歸檔文件

-f FILE.tar: 操作的歸檔文件

-x: 展開歸檔

--xattrs: 歸檔時,保留文件的擴展屬性信息

-t: 不展開歸檔,直接查看歸檔了哪些文件

-C:解壓時指定路徑

-r:向包中追加文件

-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.bz2

? ? ? ? # tar zcvf com.tar.gz com.txt

? ? ? ? ? ? com.txt

? ? ? ? # ll

? ? ? ? ? ? total 67968

? ? ? ? ? ? -rw-r--r-- 1 root root? 367957 Jul 30 09:24 com.tar.gz

? ? ? ? # 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:不解包查看包中的內容

# 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中查找


1、查看用戶的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 found

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

2、添加路徑到PATH

? ? 1)臨時修改PATH值

? ? ? ? # 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? ? //局部配置文件,只對特定用戶生效

? ? ? ? # 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

? ? 1、查看當前系統中有哪些別名(root用戶和普通用戶的別名可能不一樣)

? ? ? ? # alias


? ? 2、設置命令的別名

? ? ? ? 1)臨時

? ? ? ? ? ? # alias vi='vim'

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

? ? ? ? 2)永久,改文件

? ? ? ? ? ? 別名在哪定義的?

? ? ? ? ? ? ? ? (1)/root/.bashrc? ? ? ? ? cp? ? rm? ? mv

? ? ? ? ? ? ? ? (2) /etc/profile.d

? ? ? ? ? ? ? ? ? ? ? ? colorls.sh

? ? ? ? ? ? ? ? ? ? ? ? which2.sh

? 3、取消別名

? ? ? ? [? profile.d]# unalias vi

? ? ? ? [? profile.d]# vi /etc/passwd? //沒顏色了


locate?

? ? -- 通過文件名檢索文件,檢索速度最快

? ? -- 所有能夠檢索的東西,都是存放在數據庫中的

? ? -- locate局限性,有的文件系統、有的文件及有的目錄默認是不會搜索的

1、假設我知道網卡配置文件的名字,但是不知道具體路徑:? ?

? ? # 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



2、手動更新數據庫

? ? # cp `which vim` /root/vim3

? ? # locate vim3? ? //未查詢到結果

? ? ? ? 原因:因為locate的數據庫是一天一更新,不是實時更新的。

? ? # updatedb

? ? # locate vim3

? ? ? ? /root/vim3

數據庫文件:/var/lib/mlocate/mlocate.db?

? ? ----------------------------------------------------------

? ? 報錯:

? ? 1)數據庫文件不存在?

? ? 2)手動生成它

? ? ? ? # updatedb

? ? ----------------------------------------------------------

3、locate數據庫配置文件

? ? # vim /etc/updatedb.conf

? ? # ls /tmp/vim2

? ? ? ? /tmp/vim2

? ? # locate vim2? ? //搜索不到,因為/tmp在排除列表中


find? *****

? ? -- 全局性搜索文件

? ? -- 工作方式:沿著文件的層次結構依次向下搜索,找到符合條件的,打印或者是執行相應的操作


一、語法格式

? ? find 要搜索路徑? 條件(選項) [動作]

? ? 1、基本例子

? ? ? ? # 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

? ? 2、按條件查找

? ? ? ? 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

? ? ? ? ? ? ? ? ? ? 筆試題

? ? ? ? ? ? ? ? ? ? ? ? 1、打印文件的數字權限

? ? ? ? ? ? ? ? ? ? ? ? ? ? # stat -c %a file

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 755

? ? ? ? ? ? ? ? ? ? ? ? 2、打印文件的字母權限

? ? ? ? ? ? ? ? ? ? ? ? ? ? # 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

? ? 3、動作

? ? ? ? -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

? ? ? ? 練習:

? ? ? ? ? ? 1、查找/find目錄下,類型是 普通文件的文件將其移動到/test目錄下

? ? ? ? ? ? ? ? [? find]# find . -type f -exec mv {} /test \;

? ? ? ? ? ? ? ? [? find]# ls /test/

? ? ? ? ? ? ? ? ? ? ? ? f1M? f2M? f3M? f4M

? ? ? ? ? ? ? ? 或者

? ? ? ? ? ? ? ? [? find]# mv `find . -type f` /test

? ? ? ? ? ? 2、查找/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


? ? ? ? ? ? 3、刪除/test目錄下修改時間在一天以內的普通文件

? ? ? ? ? ? ? ? [? find]# find /test/ -type f -mtime -1 -exec rm {} \;?

grep文本過濾

一.grep:目的是過濾出用戶感興趣的內容 ***


語法:grep [選項] 模式或關鍵字 文件列表

簡單例子:

[ loring ~]# grep root /etc/passwd

? ? root:x:0:0:root:/root:/bin/bash

? ? operator:x:11:0:operator:/root:/sbin/nologin

1、--color 帶顏色顯示匹配到的關鍵字

[ loring ~]# grep --color root /etc/passwd

? ? root:x:0:0:root:/root:/bin/bash

? ? operator:x:11:0:operator:/root:/sbin/nologin


2、-i 忽略大小寫

? ? [ loring tmp]# grep --color -i root /find/passwd

? ? ? ? Root:x:0:0:root:/root:/bin/bash

? ? ? ? operator:x:11:0:operator:/root:/sbin/nologin


3、-v 取反

? 過濾出不包含nologin的行

? ? [ loring tmp]# grep -v nologin /etc/passwd

4、^? ? 以某關鍵字開頭

? 顯示/root/.bashrc文件中的非注釋行

? [ loring tmp]# grep -v ^# /root/.bashrc


5、$? ? 以某關鍵字結尾

? 顯示passwd文件中以sh結尾的行

? [ loring tmp]# grep sh$ /etc/passwd


6、^$? ? 空行

? 顯示/root/.bashrc文件中的非注釋行和非空行

? ? [ loring tmp]# grep -v ^# /root/.bashrc? | grep -v ^$

7、-c? ? ? count,統計匹配到的行數

? ? [ loring tmp]# grep -c root /etc/passwd

? ? ? ? 2


8、 -l? ? 一般和-r聯用,只顯示包含關鍵字的文件的名字,而不是顯示文件內容


9、-r? ? 遞歸檢索

? 顯示test目錄下文件內容中含有root的文件名

? ? ? [ loring tmp]# grep -rl root /test

10、-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 /asdaf

? ? ? ? grep: /asdaf: No such file or directory

? ? [ loring tmp]# echo $?? //文件不存在返回2

? ? ? ? 2

11、-n? 顯示匹配行的行號

? ? [ loring tmp]# grep -n root /etc/passwd

? ? ? ? 1:root:x:0:0:root:/root:/bin/bash

? ? ? ? 11:operator:x:11:0:operator:/root:/sbin/nologin

-A

-B

-C

grep練習:

1、顯示/etc/group文件中含有root的行

? ? [ loring test]# grep root /etc/group

2、顯示/etc/passwd文件中以rp開頭的行

? ? [ loring test]# grep ^rp /etc/passwd

3、顯示/etc/group文件中不以:為結尾的行

? ? [ loring test]# grep -v :$ /etc/group

4、顯示/etc/rc.local文件中的空行,要求帶行號

? ? [ loring test]# grep -n ^$ /etc/rc.local

? ? ? ? 6:

5、顯示僅/mnt/cdrom目錄下的文件類型為目錄的文件(前提是光盤已掛載,且不使用find)

? ? [ loring test]# ll /mnt/cdrom/ | grep ^d

二、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,3

? ? ? ? root:0

? ? ? ? bin:1

? ? ? ? daemon:2

? ? ? ? adm:3

? ? ? ? lp:4


? ? [ loring test]# head -5 /etc/passwd > t1

? ? [ loring test]# vim t1

? ? ? ? :%s/:/ /g?

? ? [ loring test]# cat t1

? ? ? ? root x 0 0 root /root /bin/bash

? ? ? ? bin x 1 1 bin /bin /sbin/nologin

? ? ? ? daemon x 2 2 daemon /sbin /sbin/nologin

? ? ? ? adm x 3 4 adm /var/adm /sbin/nologin

? ? ? ? lp x 4 7 lp /var/spool/lpd /sbin/nologin

? ? 以空格為分隔,取出第一個字段

? ? [ loring test]# cut -d" " -f1 t1

? ? ? ? root

? ? ? ? bin

? ? ? ? daemon

? ? ? ? adm

? ? ? ? lp

取出/etc/group文件中組名的字段

? ? cut -d: -f1 /etc/group

打印/etc/passwd 文件中每行的第1-5個字符,以及第7-10個字符的內容

[root@web test]# cat /etc/passwd | cut -c 1-5,7-10

三、sort? ? 排序

? ? ? ? -t:指定字段分隔符

? ? ? ? -k:指定第幾個字段

? ? ? ? -n:按照數字順序排序

? ? ? ? -r:反向排序? reverse

-u:排序后重復行只打印一次? unique

[root@web test]# cat sort.txt

b:3

c:2

a:4

e:5

d:1

f:11

對輸出內容直接排序,默認按照每行的第一個字符進行排序

[root@web test]# cat sort.txt | sort

a:4

b:3

c:2

d:1

e:5

f:11

對輸出內容進行反向排序

[root@web test]# cat sort.txt | sort -r

f:11

e:5

d:1

c:2

b:3

a:4

使用“:”做分隔符,對第2個字段進行排序

[root@web test]# cat sort.txt | sort -t ":" -k 2

d:1

f:11

c:2

b:3

a:4

e:5

使用“:”做分隔符,對第2個字段進行排序,按照數字大小排序

[root@web test]# cat sort.txt | sort -t ":" -k 2 -n

d:1

c:2

b:3

a:4

e:5

f: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: -k3 -n /etc/group

四、 uniq? 去重,唯一?

? ? ? ? 去除相鄰重復行

? -c: 顯示重復的行數

? -i:? 忽略大小寫

[ loring test]# uniq num.txt

? ? 111

? ? 222

? ? 333

? ? 444

? ? 222

? ? 555

使用uniq時,一般先排序,再去重

? ? [ loring test]# sort num.txt | uniq

? ? ? ? 111

? ? ? ? 222

? ? ? ? 333

? ? ? ? 444

? ? ? ? 555

? ? [ loring test]# sort num.txt | uniq -c

? ? ? 1 111

? ? ? 3 222

? ? ? 2 333

? ? ? 1 444

? ? ? 1 555

五、tr 主要作用在于文本轉換或者刪除。

將/etc/passwd文件中的小寫字母轉換成大寫字母

[root@web test]# cat /etc/passwd | tr '[a-z]' '[A-Z]'

將/etc/passwd文件中的“:”刪除掉

[root@web test]# cat /etc/passwd | tr -d ":"

六、paste 文本合并。將文件按照行進行合并,中間使用tab隔開。

[root@web test]# cat a.txt

1

2

3

4

5

[root@web test]# cat b.txt

a

b

c

d

e

按照行合并文件

[root@web test]# paste a.txt b.txt

1 a

2 b

3 c

4 d

5 e

也可以使用-d指定合并文件時行間的分隔符

[root@web test]# paste -d: a.txt b.txt

1:a

2:b

3:c

4:d

5:e

[root@web test]#

練習:

? ? 統計/etc/passwd文件中一共有幾種shell,并顯示每種shell有幾個用戶

? ? [ loring test]# cut -d: -f7 /etc/passwd | sort | uniq -c

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,428評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,024評論 3 413
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,285評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,548評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,328評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,878評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,971評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,098評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,616評論 1 331
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,554評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,725評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,243評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,971評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,361評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,613評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,339評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,695評論 2 370

推薦閱讀更多精彩內容