1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。
復制命令:cp
cp [OPTION]… [-T] SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE…
cp SRC DEST
SRC是文件:
如果目標不存在:新建DEST,并將SRC中內容填充至DEST中;
[root@centos7study tmp]# cp /etc/inittab /tmp [root@centos7study tmp]# ls /tmp inittab ping.sh testbin testdir testdir.tar.xz test.txt
如果目錄存在:
如果DEST是文件:將SRC中的內容覆蓋至DEST中;
此時建議為cp命令使用-i選項;
如果DEST是目錄:在DEST下新建與原文件同名的文件,并將SRC中內容填充至新文件中;
[root@centos7study tmp]# ls -l /tmp total 13388 -rw-r--r--. 1 root root 511 Jul 16 16:45 inittab -rw-r--r--. 1 root root 157 Jul 11 13:24 ping.sh drwxr-xr-x. 3 root root 20 Jul 11 10:53 testbin dr-xr-xr-x. 2 root root 16384 Jul 11 10:35 testdir -rw-r--r--. 1 root root 13657524 Jul 11 10:51 testdir.tar.xz -rw-r--r--. 1 root root 23 Jul 4 17:27 test.txt [root@centos7study tmp]# cp -i /etc/inittab /tmp cp: overwrite ‘/tmp/inittab’? y [root@centos7study tmp]# ls -l total 13388 -rw-r--r--. 1 root root 511 Jul 16 18:35 inittab -rw-r--r--. 1 root root 157 Jul 11 13:24 ping.sh drwxr-xr-x. 3 root root 20 Jul 11 10:53 testbin dr-xr-xr-x. 2 root root 16384 Jul 11 10:35 testdir -rw-r--r--. 1 root root 13657524 Jul 11 10:51 testdir.tar.xz -rw-r--r--. 1 root root 23 Jul 4 17:27 test.txt
cp SRC… DEST
SRC…:多個文件
DEST必須存在,且為目錄,其它情形均會出錯;
[root@centos7study dir02]# ls /tmp/dir01 test01 test02 [root@centos7study dir02]# ls -l /tmp/dir01 total 8 -rw-r--r--. 1 root root 26 Jul 16 18:40 test01 -rw-r--r--. 1 root root 70 Jul 16 18:40 test02 [root@centos7study dir02]# cp /tmp/dir01/test01 /tmp/dir01/test02 /tmp/dir02 [root@centos7study dir02]# ls /tmp/dir02 test01 test02
cp SRC DEST
SRC是目錄:
此時使用選項:-r
如果DEST不存在:則創建指定目錄,復制SRC目錄中所有文件至DEST中;
如果DEST存在:
如果DEST是文件:報錯
如果DEST是目錄:
[root@centos7study dir02]# cp /tmp/dir01 /tmp/dir02 cp: omitting directory ‘/tmp/dir01’ [root@centos7study dir02]# cp -r /tmp/dir01 /tmp/dir02 [root@centos7study dir02]# ls -l /tmp/dir02 total 8 drwxr-xr-x. 2 root root 32 Jul 16 18:46 dir01 -rw-r--r--. 1 root root 26 Jul 16 18:44 test01 -rw-r--r--. 1 root root 70 Jul 16 18:44 test02
常用選項:
-i:交互式
-r, -R: 遞歸復制目錄及內部的所有內容;
-a: 歸檔,相當于-dR –preserv=all
-d:–no-dereference –preserv=links
–preserv[=ATTR_LIST]
mode: 權限
ownership: 屬主屬組
timestamp: 時間戳
links 鏈接
xattr 擴展屬性
context 安全上下文
all 以上所有
-p: –preserv=mode,ownership,timestamp
-v: –verbose
-f: –force
移動命令:mv
move,移動文件
mv [OPTION]… [-T] SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE…
[root@centos7study dir02]# mv /tmp/dir01/test01 /tmp [root@centos7study dir02]# ls /tmp dir01 dir02 inittab ping.sh test01 testbin testdir testdir.tar.xz test.txt
常用選項:
-i: 交互式
-f: 強制
刪除命令:rm
remove,刪除
rm [OPTION]… FILE…
[root@centos7study dir02]# ls /tmp dir01 dir02 inittab ping.sh test01 testbin testdir testdir.tar.xz test.txt [root@centos7study dir02]# rm /tmp/test01 rm: remove regular file ‘/tmp/test01’? y [root@centos7study dir02]# ls /tmp dir01 dir02 inittab ping.sh testbin testdir testdir.tar.xz test.txt
常用選項:
-i: 交互式
-f: 強制刪除
-r: 遞歸
rm -rf
[root@centos7study dir02]# rm -rf /tmp/dir01 [root@centos7study dir02]# ls /tmp dir02 inittab ping.sh testbin testdir testdir.tar.xz test.txt
2、bash的工作特性之命令執行狀態返回值和命令行展開所涉及的內容及其示例演示。
Bash的變量類型主要有:本地變量、局部變量、環境變量、位置變量和特殊變量,其中特殊變量中的 $? 這個變量隨時在變化,其中保存的數據就是剛剛執行過的命令執行狀態返回值。
[root@centos7study tmp]# ls /tmp/aaaa ls: cannot access /tmp/aaaa: No such file or directory [root@centos7study tmp]# echo $? 2 [root@centos7study tmp]# ls /tmp dir02 inittab ping.sh testbin testdir testdir.tar.xz test.txt [root@centos7study tmp]# echo $? 0
執行狀態返回值$?的數字含義為:
0:成功執行;
1-255:執行失敗。
3、請使用命令行展開功能來完成以下練習:
(1)、創建/tmp目錄下的:a_c, a_d, b_c, b_d
[root@centos7study tmp]# touch {a,b}_{c,d} [root@centos7study tmp]# ls a a_c a_d b_c b_d dir02 inittab ping.sh testbin testdir testdir.tar.xz test.txt
(2)、創建/tmp/mylinux目錄
[root@centos7study tmp]# 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@centos7study tmp]# 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
4、文件的元數據信息有哪些,分別表示什么含義,如何查看?如何修改文件的時間戳信息。
文件的時間戳管理工具:
touch
文件:metadata, data
查看文件狀態:stat
三個時間戳:
access time:訪問時間,簡寫為atime,讀取文件內容
modify time: 修改時間, mtime,改變文件內容(數據)
change time: 改變時間, ctime,元數據發生改變
touch命令:
touch [OPTION]… FILE…
-a: only atime
-m: only mtime
-t STAMP:
[[CC]YY]MMDDhhmm[.ss]
-c: 如果文件不存,則不予創建
5、如何定義一個命令的別名,如何在命令中引用另一個命令的執行結果?
命令別名(alias)
通過alias命令實現:
(1) alias
顯示當前shell進程所有可用的命令別名;
(2) alias NAME='VALUE'
定義別名NAME,其相當于執行命令VALUE;
注意:在命令行中定義的別名,僅對當前shell進程有效;如果想永久有效,要定義在配置文件中;
僅對當前用戶:~/.bashrc
對所有用戶有效:/etc/bashrc
Note: 編輯配置給出的新配置不會立即生效;
bash進程重新讀取配置文件:
source /path/to/config_file
. /path/to/config_file
撤消別名:unalias
unalias [-a] name [name …]
Note: 如果別名同原命令的名稱,則如果要執行原命令,可使用"\COMMAND";
引用另一個命令的執行結果——管道:
COMMAND1 | COMMAND2 | COMMAND3 |…
Note:最后一個命令會在當前shell進程的子shell進程中執行;
6、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其它字符)的文件或目錄。
[root@centos7study]# ls /var/1*[0-9]*[a-z]
7、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄。
[root@centos7study]# ls /etc/[0-9]*[^0-9]
8、顯示/etc目錄下,以非字母開頭,后面跟了一個字母以及其它任意長度任意字符的文件或目錄。
[root@centos7study]# ls /etc/[^[:alpha:]][:alpha:]*
9、在/tmp目錄下創建以tfile開頭,后跟當前日期和時間的文件,文件名形如:tfile-2016-05-27-09-32-22。
[root@centos7study]# touch tfile-`date +%F-%H-%M-%S`
10、復制/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中。
[root@centos7study]# mkdir /tmp/mytest1 [root@centos7study]# cp -r /etc/p*[^0-9] /tmp/mytest1
11、復制/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中。
[root@centos7study]# mkdir /tmp/mytest2 [root@centos7study]# cp -r /etc/*\.d /tmp/mytest2
12、復制/etc/目錄下所有以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3目錄中。
[root@centos7study]# mkdir /tmp/mytest3 [root@centos7study]# cp -r /etc/[l,m,n]*\.conf /tmp/mytest3
原創文章,作者:N21-孟然,如若轉載,請注明出處:http://www.www58058.com/24085
寫的很好,排版也很漂亮,加油