一、Linux常用文件管理命令及用法。
(1)、cat,由第一行開始顯示內容,并將所有內容輸出
用法:cat [OPTION]… [FILE]…
常用選項:
-n:對所有輸出的行數編號
例如:
[root@iZ23fk44yhjZ ~]# cat -n /etc/inittab 1# inittab is only used by upstart for the default runlevel. 2# 3# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. 4# 5# System initialization is started by /etc/init/rcS.conf 6# 7# Individual runlevels are started by /etc/init/rc.conf 8# 9# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf 10# 11# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, 12# with configuration in /etc/sysconfig/init. 13# 14# For information on how to write upstart event handlers, or how 15# upstart works, see init(5), init(8), and initctl(8). 16# 17# Default runlevel. The runlevels used are: 18# 0 - halt (Do NOT set initdefault to this) 19# 1 - Single user mode 20# 2 - Multiuser, without NFS (The same as 3, if you do not have networking) 21# 3 - Full multiuser mode 22# 4 - unused 23# 5 - X11 24# 6 - reboot (Do NOT set initdefault to this) 25# 26id:3:initdefault: [root@iZ23fk44yhjZ ~]#
(2)、tac,從最后一行倒序顯示內容,并將所有內容輸出
用法:tac [OPTION]… [FILE]…
例如:
[root@iZ23fk44yhjZ ~]# tac /etc/inittab id:3:initdefault: # # 6 - reboot (Do NOT set initdefault to this) # 5 - X11 # 4 - unused # 3 - Full multiuser mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 1 - Single user mode # 0 - halt (Do NOT set initdefault to this) # Default runlevel. The runlevels used are: # # upstart works, see init(5), init(8), and initctl(8). # For information on how to write upstart event handlers, or how # # with configuration in /etc/sysconfig/init. # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # # Individual runlevels are started by /etc/init/rc.conf # # System initialization is started by /etc/init/rcS.conf # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # inittab is only used by upstart for the default runlevel.
(3)、head,只顯示頭幾行
用法:head [OPTION]… [FILE]…
常用選項:
-n #:用于獲取前多少行
-q 獲取多個文件的時候不顯示文件頭部
-v 獲取多個文件的內容是顯示頭部,這是默認選項
–c # ,獲取前#個字節的內容
例如:獲取inittab前5行的內容
[root@iZ23fk44yhjZ ~]# head -n 5 /etc/inittab # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf [root@iZ23fk44yhjZ ~]#
(4)、tail,只顯示最后幾行,不指定num,默認顯示10行
用法:tail [OPTION]… [FILE]…
常用選項:
-f 該參數用于監視File文件增長
-c # 從 # 字節位置讀取指定文件
-n # 從 # 行位置讀取指定文件
-k # 從 # 表示的1KB塊位置讀取指定文件
-b # 從 # 表示的512字節塊位置讀取指定文件
例如:
[root@iZ23fk44yhjZ ~]# tail -n 5 /etc/inittab # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:3:initdefault:
(5)、more,根據窗口大小,一頁一頁的現實文件內容
用法:more [-dlfpcsu] [-num] [+/pattern] [+linenum] [file …]
常用選項:
+num: 從第num行開始顯示
-num 定義屏幕大小,為num行
+/pattern 從pattern 前兩行開始顯示
-c 從頂部清屏然后顯示
-p 通過清除窗口而不是滾屏來對文件進行換頁
-s 把連續的多個空行顯示為一行
-u 把文件內容中的下劃線去掉
q:退出
例如:
[root@iZ23fk44yhjZ ~]# more +15 /etc/inittab # upstart works, see init(5), init(8), and initctl(8). # # Default runlevel. The runlevels used are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:3:initdefault:
(6)、less,和more類似,但其優點可以往前翻頁,而且進行可以搜索字符
用法:less [option] File
常用選項:
-e:文件內容顯示完畢后,自動退出
-f:強制顯示文件
-g:不加亮顯示搜索到的所有關鍵詞,僅顯示當前顯示的關鍵字,以提高顯示速度
-l:搜索時忽略大小寫的差異
-N:每一行行首顯示行號
-s:將連續多個空行壓縮成一行顯示
-S:在單行顯示較長的內容,而不換行顯示
例如:
[root@iZ23fk44yhjZ ~]# less -N /etc/inittab 1 # inittab is only used by upstart for the default runlevel. 2 # 3 # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. 4 # 5 # System initialization is started by /etc/init/rcS.conf 6 # 7 # Individual runlevels are started by /etc/init/rc.conf 8 # 9 # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf 10 # 11 # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, 12 # with configuration in /etc/sysconfig/init. 13 # 14 # For information on how to write upstart event handlers, or how 15 # upstart works, see init(5), init(8), and initctl(8). 16 # 17 # Default runlevel. The runlevels used are: 18 # 0 - halt (Do NOT set initdefault to this) 19 # 1 - Single user mode 20 # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) 21 # 3 - Full multiuser mode 22 # 4 - unused 23 # 5 - X11 24 # 6 - reboot (Do NOT set initdefault to this) 25 # 26 id:3:initdefault: /etc/inittab (END)
(7)、cp命令,復制文件或目錄
用法:
cp [OPTION]… [-T] SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE…
常用選項:
-a:該選項通常在拷貝目錄時使用。它保留鏈接、文件屬性,并遞歸地拷貝目錄,其作用等于dpR選項的組合
-d:拷貝時保留鏈接
-f:刪除已經存在的目標文件而不提示
-i:和f選項相反,在覆蓋目標文件之前將給出提示要求用戶確認,回答y時目標文件將被覆蓋,是交互式拷貝
-p:此時cp除復制源文件的內容外,還將把其修改時間和訪問權限也復制到新文件中
-r:若給出的源文件是一目錄文件,此時cp將遞歸復制該目錄下所有的子目錄和文件,此時目標文件必須為一個目錄名
例如:
[root@iZ23fk44yhjZ ~]# cp -ar /etc/init.d/ a_c/ [root@iZ23fk44yhjZ ~]# ll a_c/init.d/ total 304 -rwxr-xr-x 1 root root 26 Aug 1 11:51 DbSecuritySpt -rwxr-xr-x 1 root root 23 Aug 1 11:51 VsystemsshMdt -rwxr-xr-x 1 root root 1288 May 12 04:43 abrt-ccpp -rwxr-xr-x 1 root root 1642 May 12 04:43 abrt-oops -rwxr-xr-x 1 root root 1628 May 12 04:43 abrtd -rwxr-xr-x 1 root root 1818 Feb 17 2016 acpid -rwxr-xr-x 1 root root 2265 Jan 5 2015 aegis
(8)、rm命令,刪除文件或目錄,不跟任何參數不能刪除目錄
用法:rm [OPTION]… FILE…
常用選項:
-f:強制刪除
-i:和-f相反,在刪除時會給出要求用戶確認的提示,同cp -i
-r:遞歸刪除,刪除目錄及目錄下的文件
注意:rm -rf結合使用比較危險
例如:
[root@iZ23fk44yhjZ ~]# rm a_d #不能直接刪目錄 rm: cannot remove `a_d': Is a directory [root@iZ23fk44yhjZ ~]# rm -i lanmp_ins.log rm: remove regular file `lanmp_ins.log'? y #回答y就表示刪除,回答n就表示不刪 [root@iZ23fk44yhjZ ~]# rm -rf a_d #強制刪除,并且不給任何提示,此操作危險,謹慎操作 [root@iZ23fk44yhjZ ~]# ls | grep a_d [root@iZ23fk44yhjZ ~]#
(9)、mv命令,移動文件或目錄,默認等于mv -i,如果目標路徑已存在相同文件或目錄會提示是否覆蓋,如果沒有直接移動到目標路徑
用法:
mv [OPTION]… [-T] SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE…
常用選項:
-i:交互式操作,在移動文件或目錄時需要用戶確認
-f:非交互式,強制移動并覆蓋
-n:不覆蓋目標路徑已存在的文件或目錄
例如:
[root@iZ23fk44yhjZ ~]# touch b_d/123.txt [root@iZ23fk44yhjZ ~]# touch 123.txt [root@iZ23fk44yhjZ ~]# mv 123.txt b_d/ mv: overwrite `b_d/123.txt'?
二、命令執行狀態返回值以及命令展開
(1)命令返回狀態結果
即命令的執行狀態結果值,bash通過狀態返回值來輸出此結果,成功用0表示,失敗則是1-255任意數字表示,命令執行完成之后,其狀態返回值保存于bash的特殊變量$?中。
例如:返回值為0表示執行成功
[root@iZ23fk44yhjZ wwwroot]# ls 123.txt 5ucy_com biandang100_com feishuai_com log2_com log_com phpmyadmin test.zip test_com [root@iZ23fk44yhjZ wwwroot]# echo $? 0 [root@iZ23fk44yhjZ wwwroot]#
例如:返回值為非0表示執行失敗
[root@iZ23fk44yhjZ wwwroot]# lss -bash: lss: command not found [root@iZ23fk44yhjZ wwwroot]# echo $? 127 [root@iZ23fk44yhjZ wwwroot]#
(2)命令行展開
~展開:自動展開為用戶的家目錄,或指定的用戶的家目錄
例如:
[root@iZ23fk44yhjZ ~]# echo ~ /root [root@iZ23fk44yhjZ ~]# echo ~root /root [root@iZ23fk44yhjZ ~]# echo ~user1 /home/user1 [root@iZ23fk44yhjZ ~]#
{}展開:可承載一個以逗號分隔的路徑列表,并能夠將其展開為個路徑
例如:
[root@iZ23fk44yhjZ ~]# mkdir -pv /tmp/mytest/A/{x1/{a,b},x2} mkdir: created directory `/tmp/mytest/A' mkdir: created directory `/tmp/mytest/A/x1' mkdir: created directory `/tmp/mytest/A/x1/a' mkdir: created directory `/tmp/mytest/A/x1/b' mkdir: created directory `/tmp/mytest/A/x2' [root@iZ23fk44yhjZ ~]# tree /tmp/mytest/ /tmp/mytest/ `-- A |-- x1 | |-- a | `-- b `-- x2 5 directories, 0 files [root@iZ23fk44yhjZ ~]#
三、用命令展開完成練習題。
(1)、創建tmp目錄下:a_c,a_d,b_c,b_d
[root@iZ23fk44yhjZ ~]# mkdir -pv /tmp/{a,b}_{c,d} mkdir: created directory `/tmp' mkdir: created directory `/tmp/a_c' mkdir: created directory `/tmp/a_d' mkdir: created directory `/tmp/b_c' mkdir: created directory `/tmp/b_d' [root@iZ23fk44yhjZ ~]# tree /tmp/ /tmp/ |-- a_c |-- a_d |-- b_c `-- b_d 4 directories, 0 files
(2)、創建tmp/mylinux下的多個目錄
例如:
[root@iZ23fk44yhjZ ~]# mkdir -pv /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}} [root@iZ23fk44yhjZ ~]# tree /tmp/mylinux/ /tmp/mylinux/ |-- bin |-- boot | `-- grub |-- dev |-- etc | |-- rc.d | | `-- init.d | `-- sysconfig | `-- network-scripts |-- lib | `-- modules |-- lib64 |-- proc |-- sbin |-- sys |-- tmp |-- usr | `-- local | |-- bin | `-- sbin `-- var |-- lock |-- log `-- run 24 directories, 0 files
四、文件的元數據信息以及查看和修改時間戳。
(1)、查看,使用stat命令查看,例如:
[root@iZ23fk44yhjZ ~]# stat /etc/inittab File: `/etc/inittab' Size: 884 Blocks: 8 IO Block: 4096 regular file Device: fc01h/64513dInode: 918048 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-07-13 00:40:59.000000000 +0800 Modify: 2016-07-13 00:40:59.000000000 +0800 Change: 2016-08-20 12:20:02.502376240 +0800 [root@iZ23fk44yhjZ ~]#
(2)、元數據屬性信息
File:文件名
Size:文件大小
Blocks:占據的塊數(磁盤事先是被分隔成塊的)
IO Block:IO塊的大小,如上例,每個塊大小是4096,共占據了8個塊,就等于8*4096
Device:所在的設備號
Inode:索引節點號
Links:硬鏈接
Access:權限
Uid:屬主以及它的UID號
Gid:屬組以及它的GID號
Access:最近訪問時間
Modify:最近更改
Change:最近改動
(3)、修改時間戳
touch命令,它可以用于修改文件的時間戳,默認只能修改最近訪問和最近修改的時間戳
-a:僅修改訪問時間
-m:僅修改最近修改時間
-c:指定的文件路徑不存在時不予創建
-t:改為指定的時間戳,例如:
[root@iZ23fk44yhjZ ~]# touch -t 201508081818.00 /home/user1/public_html/index.html [root@iZ23fk44yhjZ ~]# stat /home/user1/public_html/index.html File: `/home/user1/public_html/index.html' Size: 2889 Blocks: 8 IO Block: 4096 regular file Device: fc01h/64513dInode: 655653 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1001/ user1) Gid: ( 1002/ user1) Access: 2015-08-08 18:18:00.000000000 +0800 Modify: 2015-08-08 18:18:00.000000000 +0800 Change: 2016-08-20 20:36:45.314436811 +0800 此處默認只修改了最近訪問和最近修改的時間戳
touch不跟任何參數,表示把所有時間戳修改為當前時間,例如:
[root@iZ23fk44yhjZ ~]# stat /home/user1/public_html/index.html File: `/home/user1/public_html/index.html' Size: 2889 Blocks: 8 IO Block: 4096 regular file Device: fc01h/64513dInode: 655653 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1001/ user1) Gid: ( 1002/ user1) Access: 2015-08-08 18:18:00.000000000 +0800 Modify: 2015-08-08 18:18:00.000000000 +0800 Change: 2016-08-20 20:36:45.314436811 +0800 [root@iZ23fk44yhjZ ~]# [root@iZ23fk44yhjZ ~]# touch /home/user1/public_html/index.html [root@iZ23fk44yhjZ ~]# [root@iZ23fk44yhjZ ~]# stat /home/user1/public_html/index.html File: `/home/user1/public_html/index.html' Size: 2889 Blocks: 8 IO Block: 4096 regular file Device: fc01h/64513dInode: 655653 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1001/ user1) Gid: ( 1002/ user1) Access: 2016-08-20 20:41:30.676436413 +0800 Modify: 2016-08-20 20:41:30.676436413 +0800 Change: 2016-08-20 20:41:30.676436413 +0800
五、如何定義一個命令別名以及引用命令的執行結果
(1)、定義別名需使用alias命令
語 法:alias[別名]=[指令名稱],不跟任何參數時,默認顯示已經定義的別名
例如:
[root@iZ23fk44yhjZ a_d]# alias md='mkdir ./123123' [root@iZ23fk44yhjZ a_d]# [root@iZ23fk44yhjZ a_d]# md [root@iZ23fk44yhjZ a_d]# ls 123123 [root@iZ23fk44yhjZ a_d]#
(2)、引用命令的執行結果(不知道如何解釋,直接上示例吧)
例如:把命令的執行結果賦值給變量uid,取命令執行結果用 ` `這兩個符號
[root@iZ23fk44yhjZ a_d]# uid=`id -u user1` [root@iZ23fk44yhjZ a_d]# echo $uid 1001 [root@iZ23fk44yhjZ a_d]#
六、顯示/var下以l開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其他字符)的文件或目錄
[root@iZ23fk44yhjZ ~]# ls -d /var/l[[:digit:]]*[[:lower:]] /var/l3,t /var/l4c [root@iZ23fk44yhjZ ~]#
七、顯示/etc目錄下,以非字母開頭,后面跟了一個字母以及其他任意長度任意字符的文件或目錄
[root@iZ23fk44yhjZ ~]# touch /etc/2t.txt [root@iZ23fk44yhjZ ~]# ls -d /etc/[^[:alpha:]][[:alpha:]].* /etc/1o.txt /etc/2t.txt [root@iZ23fk44yhjZ ~]#
八、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄
[root@iZ23fk44yhjZ ~]# ls -d /etc/[0-9][^0-9] /etc/2t [root@iZ23fk44yhjZ ~]#
九、在tmp目錄下創建以tfile開頭,后跟當前日期和時間的文件
[root@iZ23fk44yhjZ ~]# time=`date +%Y-%m-%d-%H-%M-%S` [root@iZ23fk44yhjZ ~]# echo $time 2016-08-20-22-58-55 [root@iZ23fk44yhjZ ~]# touch /tmp/tfile-"$time" [root@iZ23fk44yhjZ ~]# ls /tmp/ a_c a_d b_c b_d mylinux tfile-2016-08-20-22-58-55 [root@iZ23fk44yhjZ ~]#
十、復制/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到tmp/mytest1目錄中
[root@iZ23fk44yhjZ ~]# mkdir /tmp/mytest1 [root@iZ23fk44yhjZ ~]# ls -d /etc/p*[^0-9] /etc/pam.d /etc/passwd- /etc/pki /etc/pm-utils-hd-apm-restore.conf /etc/ppp /etc/prelink.conf.d /etc/profile.d /etc/pango /etc/pcmcia /etc/plymouth /etc/popt.d /etc/prelink.cache /etc/printcap /etc/protocols /etc/passwd /etc/pinforc /etc/pm /etc/postfix /etc/prelink.conf /etc/profile /etc/pulse [root@iZ23fk44yhjZ ~]# cp -r /etc/p*[^0-9] /tmp/mytest1/ [root@iZ23fk44yhjZ ~]# ls /tmp/mytest1/ pam.d passwd pcmcia pki pm popt.d ppp prelink.conf printcap profile.d pulse pango passwd- pinforc plymouth pm-utils-hd-apm-restore.conf postfix prelink.cache prelink.conf.d profile protocols [root@iZ23fk44yhjZ ~]#
十一、復制/etc下以.d結尾的目錄或文件至/tmp/mytest2中
[root@iZ23fk44yhjZ ~]# ls -d /etc/*.d /etc/bash_completion.d /etc/event.d /etc/lsb-release.d /etc/prelink.conf.d /etc/rc2.d /etc/rsyslog.d /etc/sysctl.d /etc/chkconfig.d /etc/init.d /etc/makedev.d /etc/profile.d /etc/rc3.d /etc/rwtab.d /etc/xinetd.d /etc/cron.d /etc/ld.so.conf.d /etc/modprobe.d /etc/rc.d /etc/rc4.d /etc/setuptool.d /etc/yum.repos.d /etc/depmod.d /etc/libibverbs.d /etc/pam.d /etc/rc0.d /etc/rc5.d /etc/statetab.d /etc/dracut.conf.d /etc/logrotate.d /etc/popt.d /etc/rc1.d /etc/rc6.d /etc/sudoers.d [root@iZ23fk44yhjZ ~]# [root@iZ23fk44yhjZ ~]# mkdir /tmp/mytest2 [root@iZ23fk44yhjZ ~]# cp -r /etc/*.d /tmp/mytest2/ [root@iZ23fk44yhjZ ~]# ls /tmp/mytest2/ bash_completion.d dracut.conf.d libibverbs.d modprobe.d profile.d rc2.d rc6.d statetab.d yum.repos.d chkconfig.d event.d logrotate.d pam.d rc.d rc3.d rsyslog.d sudoers.d cron.d init.d lsb-release.d popt.d rc0.d rc4.d rwtab.d sysctl.d depmod.d ld.so.conf.d makedev.d prelink.conf.d rc1.d rc5.d setuptool.d xinetd.d
十二、復制/etc下以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3中
[root@iZ23fk44yhjZ ~]# ls /etc/[lmn]*.conf /etc/ld.so.conf /etc/libuser.conf /etc/mke2fs.conf /etc/nsswitch.conf /etc/libaudit.conf /etc/logrotate.conf /etc/nscd.conf /etc/ntp.conf [root@iZ23fk44yhjZ ~]# mkdir /tmp/mytest3 [root@iZ23fk44yhjZ ~]# cp -r /etc/[lmn]*.conf /tmp/mytest3/ [root@iZ23fk44yhjZ ~]# ls /tmp/mytest3/ ld.so.conf libaudit.conf libuser.conf logrotate.conf mke2fs.conf nscd.conf nsswitch.conf ntp.conf
原創文章,作者:凸b男波萬,如若轉載,請注明出處:http://www.www58058.com/38036
寫的很好,排版也很棒,加油