Linux中的文件管理命令
一、linux中的目錄管理命令:mkdir
和rmdir
mkdir命令:創建目錄
- SYNOPSIS mkdir [OPTION]... DIRECTORY... - OPTION -p:按需自動創建父目錄 -v:顯示命令執行的詳細過程 -m:創建目錄時直接給定權限,否則為默認權限 - example: [root@centos ~]# mkdir -pv /tmp/x/y/z mkdir: created directory `/tmp/x' mkdir: created directory `/tmp/x/y' mkdir: created directory `/tmp/x/y/z' [root@centos ~]#
rmdir:刪除目錄
- SYNOPSIS: rmdir [OPTION]... DIRECTORY... - OPTION: -p:刪除某空目錄后,如果其父目錄為空,則一起刪除。 -v:顯示命令執行的詳細過程 - example: [root@centos ~]# rmdir -pv /tmp/x/y/z rmdir: removing directory, `/tmp/x/y/z' rmdir: removing directory, `/tmp/x/y' rmdir: removing directory, `/tmp/x' rmdir: removing directory, `/tmp' rmdir: failed to remove directory`/tmp': Directory not empty [root@centos ~]#
二、Linux中文件查看命令:cat
、tac
、head
、tail
、more
和less
more命令:file perusal filter for crt viewing
- SYNOPSIS: more [OPTION] [file ...] - 特點: 翻屏至文件尾部后自動退出
less命令:opposite of more
- SYNOPSIS: less [file ...] - 特點: 其實man讀取幫助手冊就是調用的less指令。所以less命令的操作方式同man
head命令:查看文件的前N行,默認為10行
- SYNOPSIS: head [OPTION]... [FILE]... - OPTION: -n # 或 -#:指定查看前#行
tail命令:查看文件的后N行,默認為10行
- SYNOPSIS: tail [OPTION]... [FILE]... - OPTION: -n # 或 -#:指定查看后#行 -f:查看文件尾部內容結束后不退出,跟隨顯示新增的行
cat:文本文件查看器??蓪⒍鄠€文本文件連接在一起顯示
注:不能查看二進制文件??梢允怯胒ile指令確定文件是否是文本文件(ASCII test)
- SYNOPSIS: cat [OPTION]... [FILE]... - OPTION: -n:帶行編號顯示文件內容(不會修改原文) -E:顯示行結束符$
tac命令:與cat功能相近,只是逆序顯示文件內容
三、Linux中文件管理命令:cp
、mv
和rm
cp:實現文件復制
- SYNOPSIS: 單原復制:cp [OPTION]... [-T] SOURCE DEST 多源復制:cp [OPTION]... SOURCE... DIRECTORY 多源復制:cp [OPTION]... -t DIRECTORY SOURCE... 單原復制:cp [OPTION]... [-T] SOURCE DEST - 如果DEST不存在,則首先創建此文件,并復制源文件的數據流至DEST中 - 如果DEST存在: -- 如果DEST是非目錄文件,則覆蓋目標文件。 -- 如果DEST是目錄文件,則先在DEST目錄下創建一個與源文件同名的文件,并復制數據流到目標文件 多源復制:cp [OPTION]... SOURCE... DIRECTORY 多源復制:cp [OPTION]... -t DIRECTORY SOURCE... - 如果DEST不存在:錯誤。 - 如果DEST存在: -- 如果DEST是非目錄文件,錯誤 -- 如果是目錄文件。分別復制每個文件,至目標目錄中,并保持原名。 - OPTION: -i:交互式復制,即覆蓋之前提醒用戶確認。 -f:強行覆蓋目標文件。 -r:遞歸復制目錄 -d:復制符號鏈接文件的本身,而非其指向的源文件 -a:-dR --preserve=all,用于實現歸檔 --preserve= mode:權限 ownership:屬主和屬組 timestamps:時間戳 context:安全標簽 xattr:擴展屬性 links:符號鏈接 all:上述所有屬性 - example: `單源復制-目標文件不存在情況` [root@centos ~]# ls /tmp/ whatis.q3Uigb yum.log [root@centos ~]# cp /etc/issue /tmp [root@centos ~]# ls /tmp/ issue whatis.q3Uigb yum.log [root@centos ~]# cat /tmp/issue CentOS release 6.5 (Final) Kernel \r on an \m `單源復制-目標文件已存在情況` [root@centos ~]# cp /etc/hosts /tmp/issue cp: overwrite `/tmp/issue'? y [root@centos ~]# cat /tmp/issue 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 `多源復制-目標目錄不存在情況` [root@centos ~]# cp /etc/hosts /etc/issue /tmp/x cp: target `/tmp/x' is not a directory `多源復制-目標目錄已存在情況` [root@centos ~]# mkdir /tmp/x [root@centos ~]# cp /etc/hosts /etc/issue /tmp/x [root@centos ~]# ls /tmp/x hosts issue [root@centos ~]# `-r選項實例` [root@centos ~]# mkdir -p /tmp/souce/x/y/z [root@centos ~]# mkdir /tmp/dest [root@centos ~]# cp -r /tmp/souce/ /tmp/dest/ [root@centos ~]# tree /tmp/dest/ /tmp/dest/ └── souce └── x └── y └── z 4 directories, 0 files [root@centos ~]# `--preserve選項實例` [root@centos ~]# ls -l /tmp/ |grep liubin -rw-rw-r--. 1 liubin liubin 0 Sep 27 04:56 liubin [root@centos ~]# cp --preserve=ownership /tmp/liubin /tmp/root [root@centos ~]# ls -l /tmp/ total 16 drwxr-xr-x. 3 root root 4096 Sep 27 04:53 dest -rw-r--r--. 1 root root 158 Sep 27 04:43 issue -rw-rw-r--. 1 liubin liubin 0 Sep 27 04:56 liubin -rw-rw-r--. 1 liubin liubin 0 Sep 27 04:59 root drwxr-xr-x. 3 root root 4096 Sep 27 04:49 souce drwxr-xr-x. 2 root root 4096 Sep 27 04:44 x -rw-------. 1 root root 0 Sep 27 03:07 yum.log [root@centos ~]#
mv命令:文件移動
- SYNOPSIS: mv [OPTION]... [-T] SOURCE DEST mv [OPTION]... SOURCE... DIRECTORY mv [OPTION]... -t DIRECTORY SOURCE... - OPTION: -i:交互式 -f:強制移動 - example: [root@centos ~]# tree /tmp/x /tmp/x ├── hosts └── issue 0 directories, 2 files [root@centos ~]# tree /tmp/dest/ /tmp/dest/ └── souce └── x └── y └── z 4 directories, 0 files [root@centos ~]# mv /tmp/x /tmp/dest/ [root@centos ~]# tree /tmp/dest/ /tmp/dest/ ├── souce │ └── x │ └── y │ └── z └── x ├── hosts └── issue 5 directories, 2 files [root@centos ~]#
rm命令:文件移除。謹慎使用。
- SYNOPSIS: rm [OPTION]... FILE... - OPTION: -i:交互式 -f:強制 -r:遞歸 - example: [root@centos ~]# ls /tmp dest issue liubin root souce yum.log [root@centos ~]# tree /tmp/souce/ /tmp/souce/ └── x └── y └── z 3 directories, 0 files [root@centos ~]# rm -rf /tmp/souce/ [root@centos ~]# ls anaconda-ks.cfg install.log install.log.syslog [root@centos ~]#
四、shell特性–命令行展開
-
~:自動展開為用戶的家目錄,或指定用戶的家目錄
[root@centos7 liubin]# cd ~
切換到了當前用戶(root)的家目錄
[root@centos7 ~]# cd ~liubin切換到指定用戶(liubin)的家目錄
[root@centos7 liubin]# -
{}:可承載一個以逗號分隔的路徑列表,并能夠將其展開為多個路徑。
例如:/tmp/{a,b} 相當于 /tmp/a /tmp/b
五、shell特性–命令的執行狀態結果
bash通過狀態返回值來輸出此命令執行的狀態結果
-
成功:0
-
失?。?-255
命令執行完成之后,其狀態返回值保存于bash的特殊變量$?中。注:只能獲取最近一次執行的命令結果
[root@centos ~]# ls /tmp/dest/ souce x [root@centos ~]# echo $? 0 [root@centos ~]# ls /tmp/source ls: cannot access /tmp/source: No such file or directory [root@centos ~]# echo $? 2 [root@centos ~]#
練習:
1、創建/tmp/目錄下的:a_c,a_d,b_c,b_d
[root@centos ~]# ls /tmpdest issue liubin root yum.log [root@centos ~]# mkdir -v /tmp/{a,b}_{c,d} 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@centos ~]# ls /tmp/ a_c a_d b_c b_d dest issue liubin root yum.log [root@centos ~]#
2、創建/tmp/mylinux目錄
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@centos ~]# 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}} mkdir: created directory `/tmp/mylinux' mkdir: created directory `/tmp/mylinux/bin' mkdir: created directory `/tmp/mylinux/boot' mkdir: created directory `/tmp/mylinux/boot/grub' mkdir: created directory `/tmp/mylinux/dev' mkdir: created directory `/tmp/mylinux/etc' mkdir: created directory `/tmp/mylinux/etc/rc.d' mkdir: created directory `/tmp/mylinux/etc/rc.d/init.d' mkdir: created directory `/tmp/mylinux/etc/sysconfig' mkdir: created directory `/tmp/mylinux/etc/sysconfig/network-scripts' mkdir: created directory `/tmp/mylinux/lib' mkdir: created directory `/tmp/mylinux/lib/modules' mkdir: created directory `/tmp/mylinux/lib64' mkdir: created directory `/tmp/mylinux/proc' mkdir: created directory `/tmp/mylinux/sbin' mkdir: created directory `/tmp/mylinux/sys' mkdir: created directory `/tmp/mylinux/tmp' mkdir: created directory `/tmp/mylinux/usr' mkdir: created directory `/tmp/mylinux/usr/local' mkdir: created directory `/tmp/mylinux/usr/local/bin' mkdir: created directory `/tmp/mylinux/usr/local/sbin' mkdir: created directory `/tmp/mylinux/var' mkdir: created directory `/tmp/mylinux/var/lock' mkdir: created directory `/tmp/mylinux/var/log' mkdir: created directory `/tmp/mylinux/var/run' [root@centos ~]# 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 └── run24 directories, 0 files [root@centos ~]#
六、文件的元數據
文件的元數據可通過stat命令查看:
[root@centos ~]# stat /etc/issue File: `/etc/issue' Size: 47 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 1966607 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-09-27 03:25:29.196999983 +0800 Modify: 2013-11-27 19:53:33.000000000 +0800 Change: 2016-09-27 03:13:22.876999801 +0800 [root@centos ~]#
-
文件名
-
文件大小
-
文件塊
-
文件類型
-
文件鏈接數
-
權限
-
UID
-
GID
-
文件最后一次被查看的時間
-
文件數據最后一次被修改的時間
-
文件數據和元數據最后一次被改動的時間
touch命令:可于更改前2個時間戳
- SYNOPSIS: touch [OPTION]... FILE... - OPTION: -c:指定的文件路徑不存在時不予創建 -a:僅修改access time -m:僅修改modify time `注意:如果不加選項。默認將三個時間改為當前時間` -t STAMP:更改指定時間 [[CC]YY]MMDDhhmm[.ss - example: [root@centos ~]# touch -a -t 201609262232 /tmp/issue [root@centos ~]# stat /tmp/issue File: `/tmp/issue' Size: 158 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 262148 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-09-26 22:32:00.000000000 +0800 Modify: 2016-09-27 06:31:08.898995817 +0800 Change: 2016-09-27 06:32:04.517998108 +0800 [root@centos ~]#
七、命令別名及命令引用
命令別名查看: [root@centos ~]# alias alias cp='cp -i' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' 創建別名: [root@centos ~]# alias clear='cls' [root@centos ~]# alias alias clear='cls' alias cp='cp -i' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i'
引用命令的執行結果:
使用“引用:
[root@centos ~]# echo `date` Tue Sep 27 06:45:08 CST 2016 [root@centos ~]#
使用$(COMMAND)引用
[root@centos ~]# file $(which --skip-alias ls) /bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped [root@centos ~]#
八、globbing文件通配
1、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其它字符)的文件或目錄。
[root@centos ~]# ls /var/1*[[:digit:]]*[[:lower:]] /var/1Qw2Ws [root@centos ~]#
2、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄
[root@centos ~]# ls /etc/[[:digit:]]*[^[:digit:]] /etc/2wssc. [root@centos ~]#
3、顯示/etc目錄下,以非字母開頭,后面跟了一個字母以及其它任意長度任意字符的文件或目錄。
[root@centos ~]# ls /etc/[^[:alpha:]][[:alpha:]]* /etc/2wssc. [root@centos ~]#
4、在/tmp目錄下創建以tfile開頭,后跟當前日期和時間的文件,文件名形如:tfile-2016-05-27-09-32-22。
[root@centos ~]# touch /tmp/tfile-$(date +%F-%H-%M-%S) [root@centos ~]# ls /tmp/ a_c b_c dest liubin root yum.loga_d b_d issue mylinux tfile-2016-09-27-07-10-10 [root@centos ~]#
5、復制/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中。
[root@centos ~]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1 [root@centos ~]# ls /tmp/mytest1/ pam.d pinforc popt.d prelink.conf protocols pango pki portreserve prelink.conf.d pulse passwd plymouth postfix printcap passwd- pm ppp profile pcmcia pm-utils-hd-apm-restore.conf prelink.cache profile.d [root@centos ~]#
6、復制/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中。
[root@centos ~]# cp -r /etc/*.d /tmp/mytest2/ [root@centos ~]# ls /tmp/mytest2/ bash_completion.d init.d modprobe.d rc0.d rc6.d statetab.dchkconfig.d latrace.d oddjobd.conf.d rc1.d rc.d sudoers.dcron.d ld.so.conf.d pam.d rc2.d request-key.d xinetd.ddepmod.d logrotate.d popt.d rc3.d rsyslog.d yum.repos.ddracut.conf.d lsb-release.d prelink.conf.d rc4.d rwtab.devent.d makedev.d profile.d rc5.d setuptool.d [root@centos ~]#
7、復制/etc/目錄下所有以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3目錄中。
[root@centos ~]# cp -r /etc/[l,m,n]*.conf /tmp/mytest3 [root@centos ~]# ls /tmp/mytest3 latrace.conf libaudit.conf logrotate.conf mke2fs.conf nsswitch.conf numad.confld.so.conf libuser.conf ltrace.conf nfsmount.conf ntp.conf [root@centos ~]#
原創文章,作者:lucklyme,如若轉載,請注明出處:http://www.www58058.com/49296
基礎知識和實戰總結的非常不錯。