1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。
文件管理類命令: 複製:CP ,移動:MV 刪除:RM
cp
命令功能:將一個或多個源文件或目錄複製到指定的目標文件或目錄
命令格式:
cp [OPTION]... [-T]SOURCE DEST //cp [選項]…[-T]源目的
cp [OPTION]...SOURCE... DIRECTORY // cp [選項]…源…目錄
cp [OPTION]... -tDIRECTORY SOURCE... // cp [選項]…-t 目錄 源…
常用選項:
-i:交互式複製,即覆蓋之前提醒用戶確認
-f:強制覆蓋目標文件
-r:遞歸複製目錄(大寫R也是這個功能)
-d:--no-dereference --preserv=links 複製符號鏈接文件本身,而非其指向的源文件
-a:歸檔,相當於-dR --preserve=all,archive,用於實現歸檔;
--preserve[=ATTR_LIST]
mode:權限 #默認
ownership:屬主和屬組 #默認
timestamps:時間戳 #默認
context:安全標籤
xattr:擴展屬性
links:符號鏈接
all:上述所有屬性
-p:--preserv=mode,ownership,timestamp
-v:--verbose
命令示例:
(1)複製和改名
[root@hiall2016 ~]# ls
anaconda-ks.cfg conf.n cshrc cshrcvc h opt
[root@hiall2016 ~]# cp anaconda-ks.cfg /tmp
[root@hiall2016 ~]# ls /tmp
anaconda-ks.cfg gates.lod hsperfdata_root moni.lod
[root@hiall2016 ~]# cp anaconda-ks.cfg /tmp
cp: overwrite ‘/tmp/anaconda-ks.cfg’? y
[root@hiall2016 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
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'
alias which='alias | /usr/bin/which--tty-only --read-alias --show-dot --show-tilde'
[root@hiall2016 ~]# cp anaconda-ks.cfg /tmp/ana.cfg
[root@hiall2016 ~]# ls /tmp
ana.cfg anaconda-ks.cfg gates.lod hsperfdata_root moni.lod
(2)使用-r參數,複製目錄
[root@hiall2016 ~]# cp test /tmp
cp: omitting directory ‘test’
[root@hiall2016 ~]# cp -r test /tmp
[root@hiall2016 ~]# ls /tmp/test/
test.txt
(3)使用-a參數,把權限等都複製過來
[root@hiall2016 tmp]# ls -l test
total 4
-rw-r--r--. 1 root root 3 Aug 14 10:43test.txt
[root@hiall2016 tmp]# ls /root
anaconda-ks.cfg conf.n cshrc cshrcvc h opt test
[root@hiall2016 tmp]# cp /root/conf.n .
[root@hiall2016 tmp]# ls -l
total 20
-rw-------. 1 root root 1651 Aug 14 10:39ana.cfg
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rw-r--r--. 1 root root 73 Aug 14 10:52 conf.n
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 21 Aug 14 10:43 test
drw-------. 2 root root 6 Aug 14 10:48 test2
[root@hiall2016 tmp]# rm conf.n
rm: remove regular file ‘conf.n’? y
[root@hiall2016 tmp]# cp -a /root/conf.n .
[root@hiall2016 tmp]# ls -l
total 20
-rw-------. 1 root root 1651 Aug 14 10:39ana.cfg
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rw-r--r--. 1 root root 73 Aug 14 10:19 conf.n
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 21 Aug 14 10:43 test
drw-------. 2 root root 6 Aug 14 10:48 test2
mv
命令功能:爲文件或目錄改名,或將文件或目錄移動到其它位置
命令格式:
mv [OPTION]...[-T] SOURCE DEST
mv [OPTION]...SOURCE... DIRECTORY
mv [OPTION]...-t DIRECTORY SOURCE...
常用選項:
-i:交互式
-f:force強制覆蓋,不提示
-v:顯示移動過程
-u:若目標文件已經存在,且 source 比較新,纔會更新(update)
-b:若需覆蓋文件,則覆蓋前先行備份
-t:即指定mv的目標目錄,該選項適用於移動多個源文件到一個目錄的情況,此時目標目錄在前,源文件在後
(1)移動文件
[root@hiall2016 tmp]# ll
total 20
-rw-------. 1 root root 1651 Aug 14 10:39ana.cfg
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rw-r--r--. 1 root root 73 Aug 14 10:19 conf.n
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 21 Aug 14 10:43 test
drw-------. 2 root root 6 Aug 14 10:48 test2
[root@hiall2016 tmp]# mv test2 /root
[root@hiall2016 tmp]# ll
total 20
-rw-------. 1 root root 1651 Aug 14 10:39ana.cfg
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rw-r--r--. 1 root root 73 Aug 14 10:19 conf.n
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 21 Aug 14 10:43 test
[root@hiall2016 tmp]# mv conf.n test
[root@hiall2016 tmp]# ls /test
ls: cannot access /test: No such file ordirectory
[root@hiall2016 tmp]# ls /tmp/test/
conf.n test.txt
(2)使用-f參數,強制移動不提示
[root@hiall2016 tmp]# ll
total 16
-rw-------. 1 root root 1651 Aug 14 10:39ana.cfg
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 3 root root 46 Aug 14 11:02 test
drwxr-xr-x. 2 root root 6 Aug 14 11:00 test2
[root@hiall2016 tmp]# cp ana.cfg /test2
[root@hiall2016 tmp]# ls
ana.cfg anaconda-ks.cfg gates.lod hsperfdata_root moni.lod test test2
[root@hiall2016 tmp]# mv ana.cfg /test2
mv: overwrite ‘/test2’? n
[root@hiall2016 tmp]# mv -f ana.cfg /test2
rm
命令功能:刪除一個目錄中的一個或多個文件或目錄
命令格式:rm[OPTION]... FILE...
命令選項:
-i:interactive交互式
-f:force強制刪除
-r:recursive遞歸刪除
刪除目錄:rm -rf/PATH/TO/DIR
危險操作:rm -rf /*(6和7有提示,5沒有)
命令示例:
[root@hiall2016tmp]# ls
anaconda-ks.cfg gates.lod hsperfdata_root moni.lod test test2
[root@hiall2016 tmp]# cd test
[root@hiall2016 test]# ls
conf.n test2 test.txt
[root@hiall2016 test]# cd ..
[root@hiall2016 tmp]# rm test
rm: cannot remove ‘test’: Is a directory
[root@hiall2016 tmp]# rm -f test
rm: cannot remove ‘test’: Is a directory
[root@hiall2016 tmp]# rm -rf test
[root@hiall2016 tmp]# ll
total 12
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 6 Aug 14 11:00 test2
2、bash的工作特性之命令執行狀態返回值和命令行展開涉及的內容及其示例演示。
答:bash命令執行完成後會有一個返回值,保存在$?中,如果正常執行,返回0,錯誤則返回值爲1-255之間的數字。當執行命令後,執行echo$?查看。
命令示例:
[root@hiall2016 tmp]# echo $?
0
[root@hiall2016 tmp]# lss -l
-bash: lss: command not found
[root@hiall2016 tmp]# ls -l
total 12
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 6 Aug 14 11:00 test2
[root@hiall2016 tmp]# echo $?
0
[root@hiall2016 tmp]# lssss -l
-bash: lssss: command not found
[root@hiall2016 tmp]# echo $?
127
[root@hiall2016 tmp]#
3、請使用命令行展開功能來完成以下練習:
(1)、創建/tmp目錄下的:a_c, a_d,b_c, b_d
(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
1
[root@hiall2016 tmp]# mkdir -v {a,b}_{c,d}
mkdir: created directory ‘a_c’
mkdir: created directory ‘a_d’
mkdir: created directory ‘b_c’
2
root@cat ~]# 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}
4、文件的元數據信息有哪些,分別表示什麼含義,如何查看?如何修改文件的時間戳信息。
答:
三個時間戳:
Accesstime: 訪問時間,簡寫爲atime,讀取文件內容
Modifytime: 修改時間,mtime,改變文件內容(數據)
Changetime: 改動時間,ctime,元數據發生改變
使用stat命令查看時間戳信息
命令事例:
[root@hiall2016 mylinux]# stat bin
File: ‘bin’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 806h/2054d Inode: 8429698 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context:unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-14 11:14:33.271718977 -0400
Modify: 2016-08-14 11:14:27.771718824 -0400
Change: 2016-08-14 11:14:27.771718824 -0400
Birth: -
[root@hiall2016 mylinux]# ls
bin boot dev etc lib lib64 lock log proc run sbin sys tmp usr var
[root@hiall2016 mylinux]# ls
bin boot dev etc lib lib64 lock log proc run sbin sys tmp usr var
[root@hiall2016 mylinux]# touchu bin
-bash: touchu: command not found
[root@hiall2016 mylinux]# touch bin
[root@hiall2016 mylinux]# stat bin
File: ‘bin’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 806h/2054d Inode: 8429698 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context:unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-14 11:20:54.901729622 -0400
Modify: 2016-08-14 11:20:54.901729622 -0400
Change: 2016-08-14 11:20:54.901729622 -0400
Birth: -
[root@hiall2016 mylinux]# touchu -a bin
-bash: touchu: command not found
[root@hiall2016 mylinux]# touch -a bin
[root@hiall2016 mylinux]# stat bin
File: ‘bin’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 806h/2054d Inode: 8429698 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context:unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-14 11:21:33.325730694 -0400
Modify: 2016-08-14 11:20:54.901729622 -0400
Change: 2016-08-14 11:21:33.325730694 -0400
Birth: -
[root@hiall2016 mylinux]#
(2)使用-m -t參數改修改時間
[root@hiall2016 mylinux]# touch -m -t201508142324.35 bin
[root@hiall2016 mylinux]# stat bin
File: ‘bin’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 806h/2054d Inode: 8429698 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context:unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-14 11:21:33.325730694 -0400
Modify: 2015-08-14 23:24:35.000000000 -0400
Change: 2016-08-14 11:24:39.415735884 -0400
Birth: -
5、如何定義一個命令的別名,如何在命令中引用另一個命令的執行結果?
答:
(1)定義命令別名通過alias命令實現:
alias NAME='VALUE'
[root@hiall2016 mylinux]# aliasipconfiginfo='cat /etc/sysconfig/network-scripts/ifcfg-em1'
[root@hiall2016 mylinux]# ipconfiginfo |tail -3
IPADDR=192.168.1.252
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
6、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其它字符)的文件或目錄。
[root@hiall2016 mylinux]# touch Iabc2.a
[root@hiall2016 mylinux]# ls
bin boot dev etc Iabc2.a lib lib64 lock log proc run sbin sys tmp usr var
[root@hiall2016 mylinux]# ls -dl*[[:digit:]]*[[:lower:]]
ls: cannot accessl*[[:digit:]]*[[:lower:]]: No such file or directory
[root@hiall2016 mylinux]# ls -d I*[[:digit:]]*[[:lower:]]
Iabc2.a
7、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄。
[root@hiall2016 mylinux]# mkdir a333 3aaa
[root@hiall2016 mylinux]# ls -d/tmp/mylinux/[0-9]*[^0-9]
/tmp/mylinux/233aaaa23233a /tmp/mylinux/3aaa
[root@hiall2016 mylinux]#
8、顯示/etc目錄下,以非字母開頭,後面跟了一個字母以及其它任意長度任意字符的文件或目錄。
[root@hiall2016 mylinux]# ls
233aaaa23233a 23fdfds2 3aaa a333 bin boot dev etc Iabc2.a [email protected] lib lib64 lock log proc run sbin sys tmp usr var
[root@hiall2016 mylinux]# ls -d /etc/[^[:alpha:]][[:alpha:]]*
ls: cannot access/etc/[^[:alpha:]][[:alpha:]]*: No such file or directory
[root@hiall2016 mylinux]# ls -d/tmp/mylinux/[^[:alpha:]][[:alpha:]]*
/tmp/mylinux/3aaa
9、在/tmp目錄下創建以tfile開頭,後跟當前日期和時間的文件,文件名形如:tfile-2016-08-06-09-32-22。
[root@hiall2016 mylinux]# touch /tmp/tfile-`date+"%Y-%m-%d-%H-%M-%S"`
[root@hiall2016 mylinux]# ls -d /tmp/tf*[0-9]
/tmp/tfile-2016-08-14-11-45-51
[root@hiall2016 mylinux]#
10、複製/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中。
[root@hiall2016 tmp]# ls
a_c a_d anaconda-ks.cfg b_c b_d gates.lod hsperfdata_root moni.lod mylinux mytest1 test2 tfile-2016-08-14-11-45-51
[root@hiall2016 tmp]# ls -d /etc/p*[^[:digit:]]
/etc/pam.d /etc/passwd- /etc/plymouth /etc/popt.d /etc/ppp /etc/printcap /etc/profile.d /etc/protocols
/etc/passwd /etc/pki /etc/pm /etc/postfix /etc/prelink.conf.d /etc/profile /etc/profile.ori /etc/python
[root@hiall2016 tmp]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1/
[root@hiall2016 tmp]# ls -d/tmp/mytest1/p*[^[:digit:]]
/tmp/mytest1/pam.d /tmp/mytest1/passwd- /tmp/mytest1/plymouth /tmp/mytest1/popt.d /tmp/mytest1/ppp /tmp/mytest1/printcap /tmp/mytest1/profile.d /tmp/mytest1/protocols
/tmp/mytest1/passwd /tmp/mytest1/pki /tmp/mytest1/pm /tmp/mytest1/postfix /tmp/mytest1/prelink.conf.d /tmp/mytest1/profile /tmp/mytest1/profile.ori /tmp/mytest1/python
[root@hiall2016 tmp]#
11、複製/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中。、
[root@hiall2016tmp]# mkdir /tmp/mytest2
[root@hiall2016 tmp]# ls -d /etc/*.d
/etc/bash_completion.d /etc/depmod.d /etc/init.d /etc/modules-load.d /etc/prelink.conf.d /etc/rc2.d /etc/rc6.d /etc/statetab.d /etc/xinetd.d
/etc/binfmt.d /etc/dnsmasq.d /etc/ld.so.conf.d /etc/my.cnf.d /etc/profile.d /etc/rc3.d /etc/rc.d /etc/sudoers.d /etc/yum.repos.d
/etc/chkconfig.d /etc/dracut.conf.d /etc/logrotate.d /etc/pam.d /etc/rc0.d /etc/rc4.d /etc/rsyslog.d /etc/sysctl.d
/etc/cron.d /etc/grub.d /etc/modprobe.d /etc/popt.d /etc/rc1.d /etc/rc5.d /etc/rwtab.d /etc/tmpfiles.d
[root@hiall2016 tmp]# cp -r /etc/*.d /tmp/mytest2
[root@hiall2016 tmp]# ls -d /tmp/mytest2/*.d
/tmp/mytest2/bash_completion.d /tmp/mytest2/dnsmasq.d /tmp/mytest2/logrotate.d /tmp/mytest2/popt.d /tmp/mytest2/rc2.d /tmp/mytest2/rc.d /tmp/mytest2/sysctl.d
/tmp/mytest2/binfmt.d /tmp/mytest2/dracut.conf.d /tmp/mytest2/modprobe.d /tmp/mytest2/prelink.conf.d /tmp/mytest2/rc3.d /tmp/mytest2/rsyslog.d /tmp/mytest2/tmpfiles.d
/tmp/mytest2/chkconfig.d /tmp/mytest2/grub.d /tmp/mytest2/modules-load.d /tmp/mytest2/profile.d /tmp/mytest2/rc4.d /tmp/mytest2/rwtab.d /tmp/mytest2/xinetd.d
/tmp/mytest2/cron.d /tmp/mytest2/init.d /tmp/mytest2/my.cnf.d /tmp/mytest2/rc0.d /tmp/mytest2/rc5.d /tmp/mytest2/statetab.d /tmp/mytest2/yum.repos.d
/tmp/mytest2/depmod.d /tmp/mytest2/ld.so.conf.d /tmp/mytest2/pam.d /tmp/mytest2/rc1.d /tmp/mytest2/rc6.d /tmp/mytest2/sudoers.d
[root@hiall2016 tmp]#
12、複製/etc/目錄下所有以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3目錄中。
[root@hiall2016 tmp]# mkdir/tmp/mytest3
[root@hiall2016 tmp]# ls -d /etc/[1,m,n]*.conf
/etc/man_db.conf /etc/mke2fs.conf /etc/nsswitch.conf
[root@hiall2016 tmp]# cp -r/etc/[1,m,n]*.conf /tmp/mytest3/
[root@hiall2016 tmp]# ls -d/tmp/mytest3/[1,m,n]*.conf
/tmp/mytest3/man_db.conf /tmp/mytest3/mke2fs.conf /tmp/mytest3/nsswitch.conf
[root@hiall2016 tmp]#