1、Linux文件管理類命令
cp:copy files and directories 復制文件和目錄
單源復制:cp [OPTION]… [-T] SOURCE DEST
如果DEST不存在,則事先創建此文件,并復制源文件的數據流至DEST中;
如果DEST存在:
若DEST是非目錄文件,則覆蓋目標文件;普通用戶下直接覆蓋,管理員用戶下則會提示是否覆蓋
若DEST是目錄文件,則先在DEST目錄下創建一個與源文件同名的文件,并復制其數據流
多源復制:cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE…
如果DEST不存在:錯誤
如果DEST存在:
若DEST是非目錄文件:錯誤
若DEST是目錄文件:分別復制每個文件至目標目錄中,并保持原名
常用選項:
-i:交互式復制,即覆蓋前提醒用戶確認
-f,–force:強制覆蓋目標文件
-r,-R,–recursive:遞歸復制目錄
-d:復制符號鏈接本身,而非其指向的源文件
-a:-dR –preserve=all,archive,用于實現歸檔,備份
–preserve(保留)=
mode:權限
ownership:屬主和屬組
timetamps:時間戳
context:安全標簽
xattr:擴展屬性
links:符號鏈接
all:上述所有屬性
[xw@localhost ~]$ cp /var/log/boot.log test.txt [root@localhost ~]# cp /var/log/boot.log /home/xw/test.txt cp: overwrite ‘/home/xw/test.txt’? y [xw@localhost ~]$ cp /etc/passwd /etc/group test.txt cp: target ‘test.txt’ is not a directory [xw@localhost ~]$ cp /etc/passwd /etc/group test [root@localhost ~]# cp /var/log/ /home/xw/test cp: omitting directory ‘/var/log/’ [root@localhost ~]# cp -rf /var/log/* /home/xw/test/
mv:move (rename) files 移動或重命名文件
語法格式:mv [OPTION]… [-T] SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE..
常用選項:
-i:交互式移動
-f, –force:強制移動
rm:remove files or directories 刪除文件或目錄
語法格式:rm [OPTION]… FILE…
常用選項:
-i:交互式刪除
-f:強制刪除
-r:遞歸刪除
刪除目錄:rm -rf /PATH/TO/DIR
?。?!危險操作:rm -rf /*
注意:所有不用的文件建議不要直接刪除,而是移動至某個專用目錄(模擬回收站功能)
2、bash的基礎特性之:命令的執行狀態結果和命令行展開
命令的執行狀態結果
bash通過狀態返回值來輸出結果:
成功:0
失敗:1-255
命令執行完成之后,其狀態返回值保存于bash的特殊變量 $? 中
[xw@localhost ~]$ ls /etc/passwd /etc/passwd [xw@localhost ~]$ echo $? 0 [xw@localhost ~]$ lss /etc/passwd -bash: lss: command not found [xw@localhost ~]$ echo $? 127
命令行展開
~:自動展開為用戶的家目錄,或指定的用戶的家目錄
{}:可承載一個以逗號分隔的路徑列表,并能夠將其展開為多個路徑
例如:/tmp/{a,b} 相當于 /tmp/a /tmp/b
3、使用命令行展開功能完成以下練習:
(1) 創建/tmp目錄下的:a_c,a_d,b_c,b_d
[xw@localhost ~]$ mkdir /tmp/{a,b}_{c,d} [xw@localhost ~]$ ls /tmp/ a_c a_d b_c b_d
(2) 創建/tmp/mylinux目錄下的如下目錄:
[xw@localhost ~]$ mkdir -p /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}} [xw@localhost tmp]$ tree 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
4、文件的元數據
元數據是指用來描述一個文件的特征的系統數據,包括文件大小,擁有者,所屬的組,修改日期,訪問權限以及文件數據塊的分布信息(inode…)等等。使用 ls -l filename,stat filename 來查看文件的元數據。
[xw@localhost ~]$ ls -l 1.txt -rw-rw-r--. 1 xw xw 8617 Aug 19 10:01 1.txt [xw@localhost ~]$ stat 1.txt File: ‘1.txt’ Size: 8617 Blocks: 24 IO Block: 4096 regular file Device: fd02h/64770d Inode: 136 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ xw) Gid: ( 1000/ xw) Context: unconfined_u:object_r:user_home_t:s0 Access: 2016-08-19 10:01:09.000806088 +0800 Modify: 2016-08-19 10:01:09.000806088 +0800 Change: 2016-08-19 10:01:09.000806088 +0800 Birth: -
-:文件類型:-常規文件,d目錄文件,b塊設備文件,c字符設備文件,l符號鏈接文件,s套接字文件,p命令管道
rw-(左三位):文件屬主的權限
rw-(中三位):文件屬組的權限
r–(右三位):其它用戶(非屬主、屬組)的權限
1:表示文件被硬鏈接的次數
xw:文件的屬主
xw:文件的屬組
8617:表示文件的大小,單位是字節
Aug 19 10:01:文件最近一次被修改的時間
Access(最近訪問): 2016-08-19 10:01:09.000806088 +0800
Modify(最近修改): 2016-08-19 10:01:09.000806088 +0800
Change(最近更改): 2016-08-19 10:01:09.000806088 +0800
修改文件的時間戳信息—— touch 命令:
語法格式:touch [OPTION]… FILE…
-a:僅修改access time
-m:僅修改modify time
-t STAMP:修改為指定時間
格式:[[CC]YY]MMDDhhmm[.ss]
5、命令別名 alias:將一些需要頻繁使用但又過于冗長的命令設置一個別名,這樣只需輸入簡短的別名就可以達到同樣的作用
獲取所有可用別名的定義:~]$ alias
定義別名:~]$ alias NAME='COMMAND'
注意:僅對當前shell進程有效
撤銷別名:~]$ unalias NAME
[xw@localhost ~]$ alias e='echo' [xw@localhost ~]$ e 'hello' hello
在命令中引用另一個命令的執行結果:
$(COMMAND) 或 `COMMAND`
[xw@localhost ~]$ echo now is $(date +"%F %T") now is 2016-08-19 15:02:28 [xw@localhost ~]$ echo now is `date +"%F %T"` now is 2016-08-19 15:02:42
6、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其它字符)的文件或目錄
[root@localhost var]# ls -d /var/l*[0-9]*[[:lower:]] /var/l3%%d /var/l_6gs /var/l_6r1e /var/l8d
7、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄
[root@localhost etc]# ls -d /etc/[0-9]*[^0-9] /etc/68ef% /etc/8ef /etc/90_f
8、顯示/etc目錄下,以非字母開頭,后面跟了一個字母以及其它任意長度任意字符的文件或目錄
[root@localhost etc]# ls -d /etc/[^[:alpha:]][[:alpha:]]* /etc/8ef /etc/9dfg% /etc/9dfg1
9、在/tmp目錄下創建以tfile開頭,后跟當前日期和時間的文件,文件名形如:tfile-2016-05-27-09-32-22
[root@localhost tmp]# touch /tmp/tfile-$(date +"%Y-%m-%d-%H-%M-%S") [root@localhost tmp]# ls a_c a_d b_c b_d mylinux tfile-2016-08-19-16-00-07
10、復制/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中
[root@localhost tmp]# cp -rf /etc/p*[^0-9] /tmp/mytest1 [root@localhost tmp]# ls mytest1/ pam.d passwd- plymouth popt.d ppp printcap profile.d python passwd pki pm postfix prelink.conf.d profile protocols
11、復制/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中
[root@localhost tmp]# cp -rf /etc/*.d /tmp/mytest2 [root@localhost tmp]# ls mytest2 bash_completion.d grub.d pam.d rc3.d statetab.d binfmt.d init.d popt.d rc4.d sudoers.d chkconfig.d ld.so.conf.d prelink.conf.d rc5.d sysctl.d cron.d logrotate.d profile.d rc6.d tmpfiles.d depmod.d modprobe.d rc0.d rc.d xinetd.d dnsmasq.d modules-load.d rc1.d rsyslog.d yum.repos.d dracut.conf.d my.cnf.d rc2.d rwtab.d
12、復制/etc目錄下所有以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3目錄中
[root@localhost tmp]# cp -rf /etc/[lmn]*.conf /tmp/mytest3 [root@localhost tmp]# ls mytest3 ld.so.conf libuser.conf logrotate.conf mke2fs.conf libaudit.conf locale.conf man_db.conf nsswitch.conf
原創文章,作者:蘿卜,如若轉載,請注明出處:http://www.www58058.com/36902
寫的很好,排版還可以在漂亮一點,加油,3大題2小題跟要求不一樣