1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。
文件管理命令常用有:cp mv mr
(一):cp命令
NAME cp - copy files and directories SYNOPSIS cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY SOURCE...
常用選項
-a, --archive same as -dR --preserve=all -i, --interactive prompt before overwrite (overrides a previous -n option) -R, -r, --recursive copy directories recursively -p same as --preserve=mode,ownership,timestamps -v, --verbose explain what is being done -f, --force if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)
cp SRC DEST
SRC是文件:
如果目標不存在:
新建DEST,并將SRC中內容填充至DEST
[root@localhost ~]# ll /tmp 總用量 28 drwxr-xr-x. 2 root root 4096 7月 27 19:44 centos drwx------. 2 centos centos 4096 7月 8 08:08 keyring-msbg6a -rw-r--r--. 1 root root 12 7月 27 19:32 mylinux drwx------. 2 gdm gdm 4096 7月 27 19:01 orbit-gdm drwx------. 2 gdm gdm 4096 7月 27 19:00 pulse-1oftcWYpft28 drwx------. 2 centos centos 4096 7月 8 08:08 pulse-ofuaZv9KOxpz -rw-r--r--. 1 root root 12 7月 27 19:29 test.txt [root@localhost ~]# cp /tmp/test.txt /tmp/centos [root@localhost ~]# tree /tmp /tmp ├── centos │ └── test.txt
如果目錄存在:
如果DEST是文件:將SRC中的內容覆蓋至DEST;此時建議為cp命令使用-i選項;
[root@localhost ~]# ll /tmp 總用量 28 drwxr-xr-x. 2 root root 4096 7月 27 19:45 centos drwx------. 2 centos centos 4096 7月 8 08:08 keyring-msbg6a -rw-r--r--. 1 root root 12 7月 27 19:32 mylinux drwx------. 2 gdm gdm 4096 7月 27 19:01 orbit-gdm drwx------. 2 gdm gdm 4096 7月 27 19:00 pulse-1oftcWYpft28 drwx------. 2 centos centos 4096 7月 8 08:08 pulse-ofuaZv9KOxpz -rw-r--r--. 1 root root 12 7月 27 19:29 test.txt [root@localhost ~]# cat /tmp/test.txt holle linux [root@localhost ~]# cp /tmp/test.txt /tmp/mylinux cp:是否覆蓋"/tmp/mylinux"? y [root@localhost ~]# cat /tmp/mylinux holle linux
如果DEST是目錄:在DEST下新建與原文件同名的文件,并將SRC中內容填充至新文件中;
[root@localhost ~]# ll /tmp 總用量 32 drwxr-xr-x. 2 root root 4096 7月 27 20:20 centos drwx------. 2 centos centos 4096 7月 8 08:08 keyring-msbg6a -rw-r--r--. 1 root root 12 7月 27 19:50 mylinux drwx------. 2 gdm gdm 4096 7月 27 19:01 orbit-gdm drwx------. 2 gdm gdm 4096 7月 27 19:00 pulse-1oftcWYpft28 drwx------. 2 centos centos 4096 7月 8 08:08 pulse-ofuaZv9KOxpz -rw-r--r--. 1 root root 12 7月 27 19:29 test.txt -rw-r--r--. 1 root root 16 7月 27 19:54 text.txt [root@localhost ~]# cp /tmp/text.txt /tmp/centos/text.txt [root@localhost ~]# ll /tmp/centos 總用量 8 -rw-r--r--. 1 root root 0 7月 27 20:19 134jhk23 -rw-r--r--. 1 root root 0 7月 27 20:17 324 -rw-r--r--. 1 root root 12 7月 27 19:45 test.txt -rw-r--r--. 1 root root 16 7月 27 20:23 text.txt
cp SRC… DEST
SRC…是多個文件:
DEST必須存在,且為目錄,其他情形均會出錯;
[root@localhost ~]# ll /tmp 總用量 32 -rw-r--r--. 1 root root 0 7月 27 20:27 5b3 -rw-r--r--. 1 root root 0 7月 27 20:27 abc -rw-r--r--. 1 root root 0 7月 27 20:27 b3a drwxr-xr-x. 2 root root 4096 7月 27 20:23 centos drwx------. 2 centos centos 4096 7月 8 08:08 keyring-msbg6a -rw-r--r--. 1 root root 12 7月 27 19:50 mylinux drwx------. 2 gdm gdm 4096 7月 27 19:01 orbit-gdm drwx------. 2 gdm gdm 4096 7月 27 19:00 pulse-1oftcWYpft28 drwx------. 2 centos centos 4096 7月 8 08:08 pulse-ofuaZv9KOxpz -rw-r--r--. 1 root root 12 7月 27 19:29 test.txt -rw-r--r--. 1 root root 16 7月 27 19:54 text.txt [root@localhost ~]# cp /tmp/{5b3,abc,b3a} /tmp/centos [root@localhost ~]# ll /tmp/centos 總用量 8 -rw-r--r--. 1 root root 0 7月 27 20:19 134jhk23 -rw-r--r--. 1 root root 0 7月 27 20:17 324 -rw-r--r--. 1 root root 0 7月 27 20:28 5b3 -rw-r--r--. 1 root root 0 7月 27 20:28 abc -rw-r--r--. 1 root root 0 7月 27 20:28 b3a -rw-r--r--. 1 root root 12 7月 27 19:45 test.txt -rw-r--r--. 1 root root 16 7月 27 20:23 text.txt [root@localhost ~]#
cp SRC DEST
SRC是目錄;
此時使用選項:-r
[root@localhost ~]# ll /tmp 總用量 36 -rw-r--r--. 1 root root 0 7月 27 20:27 5b3 -rw-r--r--. 1 root root 0 7月 27 20:27 abc -rw-r--r--. 1 root root 0 7月 27 20:27 b3a drwxr-xr-x. 2 root root 4096 7月 27 20:32 centos drwx------. 2 centos centos 4096 7月 8 08:08 keyring-msbg6a -rw-r--r--. 1 root root 12 7月 27 19:50 mylinux drwxr-xr-x. 2 root root 4096 7月 27 20:30 mylinux2 drwx------. 2 gdm gdm 4096 7月 27 19:01 orbit-gdm drwx------. 2 gdm gdm 4096 7月 27 19:00 pulse-1oftcWYpft28 drwx------. 2 centos centos 4096 7月 8 08:08 pulse-ofuaZv9KOxpz -rw-r--r--. 1 root root 12 7月 27 19:29 test.txt -rw-r--r--. 1 root root 16 7月 27 19:54 text.txt [root@localhost ~]# cp -r /tmp/mylinux2 /tmp/centos [root@localhost ~]# ll /tmp/centos 總用量 12 -rw-r--r--. 1 root root 0 7月 27 20:19 134jhk23 -rw-r--r--. 1 root root 0 7月 27 20:17 324 -rw-r--r--. 1 root root 0 7月 27 20:28 5b3 -rw-r--r--. 1 root root 0 7月 27 20:28 abc -rw-r--r--. 1 root root 0 7月 27 20:28 b3a drwxr-xr-x. 2 root root 4096 7月 27 20:33 mylinux2 -rw-r--r--. 1 root root 12 7月 27 19:45 test.txt -rw-r--r--. 1 root root 16 7月 27 20:23 text.txt [root@localhost ~]#
如果DEST不存在:則創建指定目錄,復制SRC目錄中所有文件至DEST中;
[root@localhost ~]# ll /tmp 總用量 36 -rw-r--r--. 1 root root 0 7月 27 20:27 5b3 -rw-r--r--. 1 root root 0 7月 27 20:27 abc -rw-r--r--. 1 root root 0 7月 27 20:27 b3a drwxr-xr-x. 3 root root 4096 7月 27 20:33 centos drwx------. 2 centos centos 4096 7月 8 08:08 keyring-msbg6a -rw-r--r--. 1 root root 12 7月 27 19:50 mylinux drwxr-xr-x. 2 root root 4096 7月 27 20:30 mylinux2 drwx------. 2 gdm gdm 4096 7月 27 19:01 orbit-gdm drwx------. 2 gdm gdm 4096 7月 27 19:00 pulse-1oftcWYpft28 drwx------. 2 centos centos 4096 7月 8 08:08 pulse-ofuaZv9KOxpz -rw-r--r--. 1 root root 12 7月 27 19:29 test.txt -rw-r--r--. 1 root root 16 7月 27 19:54 text.txt [root@localhost ~]# [root@localhost ~]# ll /tmp/mylinux2 總用量 12 drwxr-xr-x. 2 root root 4096 7月 27 20:36 324 drwxr-xr-x. 2 root root 4096 7月 27 20:36 gsd drwxr-xr-x. 2 root root 4096 7月 27 20:36 sdf [root@localhost ~]# cp -r /tmp/mylinux2 /tmp/centos2 [root@localhost ~]# ll /tmp/centos2 總用量 12 drwxr-xr-x. 2 root root 4096 7月 27 20:38 324 drwxr-xr-x. 2 root root 4096 7月 27 20:38 gsd drwxr-xr-x. 2 root root 4096 7月 27 20:38 sdf [root@localhost ~]#
如果DEST存在:
如果DEST是文件:報錯
如果DEST是目錄:成功
[root@localhost ~]# ll /tmp 總用量 40 -rw-r--r--. 1 root root 0 7月 27 20:27 5b3 -rw-r--r--. 1 root root 0 7月 27 20:27 abc -rw-r--r--. 1 root root 0 7月 27 20:27 b3a drwxr-xr-x. 3 root root 4096 7月 27 20:33 centos drwxr-xr-x. 5 root root 4096 7月 27 20:38 centos2 drwx------. 2 centos centos 4096 7月 8 08:08 keyring-msbg6a -rw-r--r--. 1 root root 12 7月 27 19:50 mylinux drwxr-xr-x. 5 root root 4096 7月 27 20:36 mylinux2 drwx------. 2 gdm gdm 4096 7月 27 19:01 orbit-gdm drwx------. 2 gdm gdm 4096 7月 27 19:00 pulse-1oftcWYpft28 drwx------. 2 centos centos 4096 7月 8 08:08 pulse-ofuaZv9KOxpz -rw-r--r--. 1 root root 12 7月 27 19:29 test.txt -rw-r--r--. 1 root root 16 7月 27 19:54 text.txt [root@localhost ~]# cp -r /tmp/centos2 /tmp/test.txt cp: 無法以目錄"/tmp/centos2" 來覆蓋非目錄"/tmp/test.txt" [root@localhost ~]# echo $? 1
mv:move,移動文件
mv [OPTION]… [-T] SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE…
常用選項:
-i:交互式
-f:強制
[root@localhost ~]# ls /tmp 134jhk23 keyring-msbg6a orbit-gdm pulse-ofuaZv9KOxpz text.txt centos mylinux pulse-1oftcWYpft28 test.txt [root@localhost ~]# mv /tmp/134jhk23 /tmp/centos [root@localhost ~]# ls /tmp/centos 134jhk23 324 test.txt
rm:remove,刪除
rm [OPTION]… FILE…
常用選項:
-i:交互式
-f:強制刪除
-r:遞歸
rm -rf
[root@localhost ~]# ll /tmp 總用量 32 -rw-r--r--. 1 root root 0 7月 27 20:16 324 drwxr-xr-x. 2 root root 4096 7月 27 19:45 centos drwx------. 2 centos centos 4096 7月 8 08:08 keyring-msbg6a -rw-r--r--. 1 root root 12 7月 27 19:50 mylinux drwx------. 2 gdm gdm 4096 7月 27 19:01 orbit-gdm drwx------. 2 gdm gdm 4096 7月 27 19:00 pulse-1oftcWYpft28 drwx------. 2 centos centos 4096 7月 8 08:08 pulse-ofuaZv9KOxpz -rw-r--r--. 1 root root 12 7月 27 19:29 test.txt -rw-r--r--. 1 root root 16 7月 27 19:54 text.txt [root@localhost ~]# rm /tmp/324 rm:是否刪除普通空文件 "/tmp/324"?y
2、bash的工作特性之命令執行狀態返回值和命令行展開所涉及的內容及其示例演示。
bash命令執行狀態返回值分別為0:成功;1-255:失敗;bash使用特殊變量$?來保存最近一條命令的執行狀態結果;
命令行展開
~:展開為用戶的主目錄
~USERNAME:展開為指定用戶的主目錄
{}:可承載一個以逗號分隔的列表,并將其展開為多個路徑
/tmp{a,b} = /tmp/a, /tmp/b
/tmp/{tmp,jerry}/hi = /tmp/tom/hi, /tem/jerry/hi
[root@localhost ~]# tty /dev/pts/0 [root@localhost ~]# echo $? 0 [root@localhost ~]# tty2 -bash: tty2: 未找到命令 [root@localhost ~]# echo $? 127 [root@localhost ~]#
3、請使用命令行展開功能來完成以下練習:
(1)、創建/tmp目錄下的:a_c, a_d, b_c, b_d
[root@localhost ~]# mkdir /tmp/{a,b}_{c,d} [root@localhost ~]# ll /tmp 總用量 4 drwxr-xr-x. 2 root root 6 7月 27 18:00 a_c drwxr-xr-x. 2 root root 6 7月 27 18:00 a_d drwxr-xr-x. 2 root root 6 7月 27 18:00 b_c drwxr-xr-x. 2 root root 6 7月 27 18:00 b_d -rwx------. 1 root root 813 7月 19 17:09 ks-script-Yc2f_Y -rw-------. 1 root root 0 7月 19 17:04 yum.log [root@localhost ~]#
(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@localhost ~]# ll /tmp 總用量 4 drwxr-xr-x. 2 root root 6 7月 27 2016 a_c drwxr-xr-x. 2 root root 6 7月 27 2016 a_d drwxr-xr-x. 2 root root 6 7月 27 2016 b_c drwxr-xr-x. 2 root root 6 7月 27 2016 b_d -rwx------. 1 root root 813 7月 19 17:09 ks-script-Yc2f_Y -rw-------. 1 root root 0 7月 19 17:04 yum.log [root@localhost ~]# 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}} mkdir: 已創建目錄 "/tmp/mylinux" mkdir: 已創建目錄 "/tmp/mylinux/bin" mkdir: 已創建目錄 "/tmp/mylinux/boot" mkdir: 已創建目錄 "/tmp/mylinux/boot/grub" mkdir: 已創建目錄 "/tmp/mylinux/dev" mkdir: 已創建目錄 "/tmp/mylinux/etc" mkdir: 已創建目錄 "/tmp/mylinux/etc/rc.d" mkdir: 已創建目錄 "/tmp/mylinux/etc/rc.d/init.d" mkdir: 已創建目錄 "/tmp/mylinux/etc/sysconfig" mkdir: 已創建目錄 "/tmp/mylinux/etc/sysconfig/network-scripts" mkdir: 已創建目錄 "/tmp/mylinux/lib" mkdir: 已創建目錄 "/tmp/mylinux/lib/modules" mkdir: 已創建目錄 "/tmp/mylinux/lib64" mkdir: 已創建目錄 "/tmp/mylinux/proc" mkdir: 已創建目錄 "/tmp/mylinux/sbin" mkdir: 已創建目錄 "/tmp/mylinux/sys" mkdir: 已創建目錄 "/tmp/mylinux/tmp" mkdir: 已創建目錄 "/tmp/mylinux/usr" mkdir: 已創建目錄 "/tmp/mylinux/usr/local" mkdir: 已創建目錄 "/tmp/mylinux/usr/local/bin" mkdir: 已創建目錄 "/tmp/mylinux/usr/local/sbin" [root@localhost ~]# 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 20 directories, 0 files
4、文件的元數據信息有哪些,分別表示什么含義,如何查看?如何修改文件的時間戳信息。
文件的元數據包含文件本身的一些屬性信息,如:文件名、文件的各類權限、各類時間戳、inode號碼、鏈接次數等。
文件包括三個時間戳:atime(訪問時間)、mtime(修改時間)、ctime(改變時間),可以用touch命令來修改時間戳
touch命令:
touch [OPTION]….FILE….
-a:only atime
-m:only mtime
-t STAMP:
[[CC]YY]MMDDhhmm[.ss]
-c:如果文件不存在,則不予創建
5、如何定義一個命令的別名,如何在命令中引用另一個命令的執行結果?
命令別名:alias
通過alias命令實現:
(1)alias
顯示當前shell進程所有可用的命令別名;
[root@localhost ~]# 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@localhost ~]#
(2)alias NAME="VALUE"
定義別名NAME,其相當于執行命令VALUE;
[root@localhost ~]# alias lstmp="ls -a /tmp/" [root@localhost ~]# lstmp . a_d .font-unix mylinux mytest3 .X11-unix .. b_c .ICE-unix mytest1 .Test-unix .XIM-unix a_c b_d ks-script-Yc2f_Y mytest2 tfile-2016-07-27-15-23-25 yum.log [root@localhost ~]#
撤銷別名:unalias
unalias [-a] name [name…]
[root@localhost ~]# unalias lstmp [root@localhost ~]# lstmp -bash: lstmp: 未找到命令
6、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其它字符)的文件或目錄。
[root@localhost ~]# ls -a /var . 12bn adm db gopher local mail opt sdf34ds.d var .. 4321331.d cache empty kerberos lock mwrf34sd preserve spool yp 123s account crash games lib log nis run tmp [root@localhost ~]# ls -d /var/1*[0-9]*[[:lower:]] /var/123s /var/12bn
7、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄。
[root@localhost ~]# mkdir /etc/{1bv,2hg,233} [root@localhost ~]# ls -d /etc/[0-9]*[^0-9] /etc/1bv /etc/2hg
8、顯示/etc目錄下,以非字母開頭,后面跟了一個字母以及其它任意長度任意字符的文件或目錄。
[root@localhost ~]# ls -d /etc/[^a-z][[:alnum:]]* /etc/1bv /etc/233 /etc/2hg
9、在/tmp目錄下創建以tfile開頭,后跟當前日期和時間的文件,文件名形如:tfile-2016-05-27-09-32-22。
[root@localhost ~]# touch /tmp/tfile-"$(date +%Y-%m-%d-%H-%M-%S)" [root@localhost ~]# ll /tmp 總用量 16 drwxr-xr-x. 2 root root 6 7月 27 2016 a_c drwxr-xr-x. 2 root root 6 7月 27 2016 a_d drwxr-xr-x. 2 root root 6 7月 27 2016 b_c drwxr-xr-x. 2 root root 6 7月 27 2016 b_d -rwx------. 1 root root 813 7月 19 17:09 ks-script-Yc2f_Y drwxr-xr-x. 13 root root 4096 7月 27 11:17 mylinux drwxr-xr-x. 11 root root 4096 7月 27 14:21 mytest1 drwxr-xr-x. 31 root root 4096 7月 27 14:25 mytest2 drwxr-xr-x. 2 root root 62 7月 27 14:33 mytest3 -rw-r--r--. 1 root root 0 7月 27 15:23 tfile-2016-07-27-15-23-25 -rw-------. 1 root root 0 7月 19 17:04 yum.log
10、復制/etc目錄下所有以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中。
[root@localhost ~]# mkdir /tmp/mytest1 [root@localhost ~]# [root@localhost ~]# cp -a /etc/p*[^0-9] /tmp/mytest1 [root@localhost ~]# ll /tmp/mytest1 總用量 48 drwxr-xr-x. 2 root root 4096 7月 19 17:08 pam.d -rw-r--r--. 1 root root 1201 7月 19 17:08 passwd -rw-r--r--. 1 root root 1201 7月 19 17:08 passwd- -rw-r--r--. 1 root root 2872 6月 10 2014 pinforc drwxr-xr-x. 11 root root 4096 7月 19 17:08 pki drwxr-xr-x. 2 root root 27 7月 19 17:06 plymouth drwxr-xr-x. 5 root root 49 7月 19 17:05 pm drwxr-xr-x. 2 root root 6 6月 10 2014 popt.d drwxr-xr-x. 2 root root 4096 7月 19 17:06 postfix drwxr-xr-x. 3 root root 4096 7月 19 17:06 ppp drwxr-xr-x. 2 root root 75 7月 19 17:06 prelink.conf.d -rw-r--r--. 1 root root 233 6月 7 2013 printcap -rw-r--r--. 1 root root 1750 6月 7 2013 profile drwxr-xr-x. 2 root root 4096 7月 19 17:06 profile.d -rw-r--r--. 1 root root 6545 6月 7 2013 protocols [root@localhost ~]#
11、復制/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中。
[root@localhost ~]# ls -d /etc/*.d /etc/bash_completion.d /etc/ld.so.conf.d /etc/rc1.d /etc/statetab.d /etc/binfmt.d /etc/logrotate.d /etc/rc2.d /etc/sudoers.d /etc/chkconfig.d /etc/modprobe.d /etc/rc3.d /etc/sysctl.d /etc/cron.d /etc/modules-load.d /etc/rc4.d /etc/tmpfiles.d /etc/depmod.d /etc/my.cnf.d /etc/rc5.d /etc/usb_modeswitch.d /etc/dnsmasq.d /etc/pam.d /etc/rc6.d /etc/xinetd.d /etc/dracut.conf.d /etc/popt.d /etc/rc.d /etc/yum.repos.d /etc/gdbinit.d /etc/prelink.conf.d /etc/rsyslog.d /etc/grub.d /etc/profile.d /etc/rwtab.d /etc/init.d /etc/rc0.d /etc/setuptool.d [root@localhost ~]# mkdir /tmp/mytest2 [root@localhost ~]# cp -a /etc/*.d /tmp/mytest2 [root@localhost ~]# ls /tmp/mytest2 bash_completion.d gdbinit.d my.cnf.d rc2.d rwtab.d xinetd.d binfmt.d grub.d pam.d rc3.d setuptool.d yum.repos.d chkconfig.d init.d popt.d rc4.d statetab.d cron.d ld.so.conf.d prelink.conf.d rc5.d sudoers.d depmod.d logrotate.d profile.d rc6.d sysctl.d dnsmasq.d modprobe.d rc0.d rc.d tmpfiles.d dracut.conf.d modules-load.d rc1.d rsyslog.d usb_modeswitch.d
12、復制/etc/目錄下所有以l或m或n開頭,以.conf結尾的文件至/tmp/mytest3目錄中。
[root@localhost ~]# mkdir /tmp/mytest3 [root@localhost ~]# ls /etc/[1mn]*.conf /etc/man_db.conf /etc/mke2fs.conf /etc/nsswitch.conf [root@localhost ~]# cp -a /etc/[1mn]*conf /tmp/mytest3 [root@localhost ~]# ls /tmp/mytest3 man_db.conf mke2fs.conf nsswitch.conf [root@localhost ~]#
原創文章,作者:N21-3分之1,如若轉載,請注明出處:http://www.www58058.com/24970
寫的很好,排版還可以在漂亮一點,加油