第一周第五次課
日期:2017-05-22##
2.1.1 創(chuàng)建和刪除目錄
[TOC]
mkdir 創(chuàng)建目錄
[root@bogon ~]# mkdir /tmp/123
[root@bogon ~]# pwd
/root
[root@bogon ~]# mkdir 234
[root@bogon ~]# ls -d /root/234
/root/234
[root@bogon ~]#
這里里面學(xué)會(huì)了相對(duì)路徑和絕對(duì)路徑祝旷;
相對(duì)路徑可以是相對(duì)當(dāng)前用戶的捣辆,也可以再某個(gè)目錄下直接創(chuàng)建旁钧,也算是相對(duì)路徑;“包含
~/
還有./
以及../
這些都是表示的相對(duì)路徑寞秃。絕對(duì)路徑是將目錄一級(jí)一級(jí)都寫(xiě)清楚穷蛹,通常直接以
/
開(kāi)頭。
--help mkdir幫助文檔
[root@bogon tmp]# mkdir --help
用法:mkdir [選項(xiàng)]... 目錄...
若指定目錄不存在則創(chuàng)建目錄煤蹭。
長(zhǎng)選項(xiàng)必須使用的參數(shù)對(duì)于短選項(xiàng)時(shí)也是必需使用的笔喉。
-m, --mode=模式 設(shè)置權(quán)限模式(類(lèi)似chmod),而不是rwxrwxrwx 減umask
-p, --parents 需要時(shí)創(chuàng)建目標(biāo)目錄的上層目錄硝皂,但即使這些目錄已存在也不當(dāng)作錯(cuò)誤處理
-v, --verbose 每次創(chuàng)建新目錄都顯示信息
-Z, --context=CTX 將每個(gè)創(chuàng)建的目錄的SELinux 安全環(huán)境設(shè)置為CTX
--help 顯示此幫助信息并退出
--version 顯示版本信息并退出
請(qǐng)向bug-coreutils@gnu.org 報(bào)告mkdir 的錯(cuò)誤
GNU coreutils 項(xiàng)目主頁(yè):<http://www.gnu.org/software/coreutils/>
GNU 軟件一般性幫助:<http://www.gnu.org/gethelp/>
請(qǐng)向<http://translationproject.org/team/zh_CN.html> 報(bào)告mkdir 的翻譯錯(cuò)誤
要獲取完整文檔常挚,請(qǐng)運(yùn)行:info coreutils 'mkdir invocation'
[root@bogon tmp]#
mkdir -p 創(chuàng)建連串目錄
mkdir -v 創(chuàng)建過(guò)程可查看
rmdir 只能刪除空目錄
--help rmdir幫助文檔
[root@bogon tmp]# rmdir --help
用法:rmdir [選項(xiàng)]... 目錄...
刪除指定的空目錄。
--ignore-fail-on-non-empty
忽略僅由目錄非空產(chǎn)生的所有錯(cuò)誤
-p, --parents 刪除指定目錄及其上級(jí)文件夾稽物,例如"rmdir -p a/b/c'"
與"rmdir a/b/c a/b a'" 基本相同
-v, --verbose 輸出處理的目錄詳情
--help 顯示此幫助信息并退出
--version 顯示版本信息并退出
請(qǐng)向bug-coreutils@gnu.org 報(bào)告rmdir 的錯(cuò)誤
GNU coreutils 項(xiàng)目主頁(yè):<http://www.gnu.org/software/coreutils/>
GNU 軟件一般性幫助:<http://www.gnu.org/gethelp/>
請(qǐng)向<http://translationproject.org/team/zh_CN.html> 報(bào)告rmdir 的翻譯錯(cuò)誤
要獲取完整文檔待侵,請(qǐng)運(yùn)行:info coreutils 'rmdir invocation'
[root@bogon tmp]#
-p 刪除連串的空目錄
-v 可以查看刪除的空目錄
[root@bogon ~]# mkdir /tmp/1/2/3
mkdir: 無(wú)法創(chuàng)建目錄"/tmp/1/2/3": 沒(méi)有那個(gè)文件或目錄
[root@bogon ~]# man mkdir
[root@bogon ~]# mkdir -pv /tmp/1/2/3
mkdir: 已創(chuàng)建目錄 "/tmp/1"
mkdir: 已創(chuàng)建目錄 "/tmp/1/2"
mkdir: 已創(chuàng)建目錄 "/tmp/1/2/3"
[root@bogon ~]#
rmdir 刪除空目錄
[root@bogon ~]# rmdir /tmp/123
[root@bogon ~]# ls /tmp/
1 yum.log yum_save_tx-2017-02-22-13-5628OmsB.yumtx
[root@bogon ~]# tree
-bash: tree: command not found
[root@bogon ~]#
[root@bogon ~]#
[root@bogon ~]# yum install -y tree
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.sohu.com
* extras: mirrors.163.com
* updates: mirrors.sohu.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package tree.i686 0:1.5.3-3.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=====================================================
Package Arch Version Repository
Size
=====================================================
Installing:
tree i686 1.5.3-3.el6 base 36 k
Transaction Summary
=====================================================
Install 1 Package(s)
Total download size: 36 k
Installed size: 63 k
Downloading Packages:
tree-1.5.3-3.el6.i686.rpm | 36 kB 00:06
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : tree-1.5.3-3.el6.i686 1/1
Verifying : tree-1.5.3-3.el6.i686 1/1
Installed:
tree.i686 0:1.5.3-3.el6
Complete!
[root@bogon ~]# tree /tmp/1
/tmp/1
└── 2
└── 3
2 directories, 0 files
[root@bogon ~]#
[root@bogon ~]# man rmdir
[root@bogon ~]# cd /tmp/
[root@bogon tmp]# rmdir -pv 1/2/3
rmdir: 正在刪除目錄 "1/2/3"
rmdir: 正在刪除目錄 "1/2"
rmdir: 正在刪除目錄 "1"
[root@bogon tmp]#
tree 命令可以查看文檔結(jié)構(gòu)
需要使用yum install -y tree
來(lái)安裝;
--help tree幫助文檔
[root@bogon tmp]# tree --help
usage: tree [-adfghilnpqrstuvxACDFNS] [-H baseHREF] [-T title ] [-L level [-R]]
[-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]
[--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]
[--filelimit #] [<directory list>]
-a All files are listed.
-d List directories only.
-l Follow symbolic links like directories.
-f Print the full path prefix for each file.
-i Don't print indentation lines.
-q Print non-printable characters as '?'.
-N Print non-printable characters as is.
-p Print the protections for each file.
-u Displays file owner or UID number.
-g Displays file group owner or GID number.
-s Print the size in bytes of each file.
-h Print the size in a more human readable way.
-D Print the date of last modification.
-F Appends '/', '=', '*', or '|' as per ls -F.
-v Sort files alphanumerically by version.
-r Sort files in reverse alphanumeric order.
-t Sort files by last modification time.
-x Stay on current filesystem only.
-L level Descend only level directories deep.
-A Print ANSI lines graphic indentation lines.
-S Print with ASCII graphics indentation lines.
-n Turn colorization off always (-C overrides).
-C Turn colorization on always.
-P pattern List only those files that match the pattern given.
-I pattern Do not list files that match the given pattern.
-H baseHREF Prints out HTML format with baseHREF as top directory.
-T string Replace the default HTML title and H1 header with string.
-R Rerun tree when max dir level reached.
-o file Output to file instead of stdout.
--inodes Print inode number of each file.
--device Print device ID number to which each file belongs.
--noreport Turn off file/directory count at end of tree listing.
--nolinks Turn off hyperlinks in HTML output.
--dirsfirst List directories before files.
--charset X Use charset X for HTML and indentation line output.
--filelimit # Do not descend dirs with more than # files in them.
[root@bogon tmp]#
2.1.2 rm 刪除文件
rm 不僅可以刪除文件也可以刪除目錄姨裸;
--help rm幫助文檔
[root@bogon tmp]# rm --help
用法:rm [選項(xiàng)]... 文件...
刪除 (unlink) 文件秧倾。
-f, --force 強(qiáng)制刪除怨酝。忽略不存在的文件,不提示確認(rèn)
-i 在刪除前需要確認(rèn)
-I 在刪除超過(guò)三個(gè)文件或者遞歸刪除前要求確認(rèn)那先。此選項(xiàng)比-i 提
示內(nèi)容更少农猬,但同樣可以阻止大多數(shù)錯(cuò)誤發(fā)生
--interactive[=WHEN] 根據(jù)指定的WHEN 進(jìn)行確認(rèn)提示:never,once (-I)售淡,
或者always (-i)斤葱。如果此參數(shù)不加WHEN 則總是提示
--one-file-system 遞歸刪除一個(gè)層級(jí)時(shí),跳過(guò)所有不符合命令行參
數(shù)的文件系統(tǒng)上的文件
--no-preserve-roo 不特殊對(duì)待"/"
--preserve-root 不允許刪除"/"(默認(rèn))
-r, -R, --recursive 遞歸刪除目錄及其內(nèi)容
-v, --verbose 詳細(xì)顯示進(jìn)行的步驟
--help 顯示此幫助信息并退出
--version 顯示版本信息并退出
默認(rèn)時(shí)揖闸,rm 不會(huì)刪除目錄揍堕。使用--recursive(-r 或-R)選項(xiàng)可刪除每個(gè)給定
的目錄,以及其下所有的內(nèi)容汤纸。
要?jiǎng)h除第一個(gè)字符為"-"的文件 (例如"-foo")衩茸,請(qǐng)使用以下方法之一:
rm -- -foo
rm ./-foo
請(qǐng)注意,如果使用rm 來(lái)刪除文件贮泞,通常仍可以將該文件恢復(fù)原狀楞慈。如果想保證
該文件的內(nèi)容無(wú)法還原,請(qǐng)考慮使用shred啃擦。
請(qǐng)向bug-coreutils@gnu.org 報(bào)告rm 的錯(cuò)誤
GNU coreutils 項(xiàng)目主頁(yè):<http://www.gnu.org/software/coreutils/>
GNU 軟件一般性幫助:<http://www.gnu.org/gethelp/>
請(qǐng)向<http://translationproject.org/team/zh_CN.html> 報(bào)告rm 的翻譯錯(cuò)誤
要獲取完整文檔囊蓝,請(qǐng)運(yùn)行:info coreutils 'rm invocation'
[root@bogon tmp]#
-f --force 過(guò)濾提示信息,靜默令蛉;
-i 默認(rèn)不提醒
-r --recursive 刪除目錄
-v 刪除過(guò)程可視化
[root@bogon tmp]# which rm
alias rm='rm -i'
/bin/rm
[root@bogon tmp]# -f 忽略錯(cuò)我提示聚霜;-i 刪除提示;-r 用于刪除目錄珠叔;-v 是可視化蝎宇;--help 可以打印man文檔^C
[root@bogon tmp]# touch 1.txt
[root@bogon tmp]# rm 1.txt
rm:是否刪除普通空文件 "1.txt"?y
[root@bogon tmp]# touch 1.txt
[root@bogon tmp]# ls
1.txt yum_save_tx-2017-02-22-13-5628OmsB.yumtx
yum.log
[root@bogon tmp]# /bin/rm 1.txt
[root@bogon tmp]# mkdir 123
[root@bogon tmp]# /bin/rm 123
/bin/rm: 無(wú)法刪除"123": 是一個(gè)目錄
[root@bogon tmp]# rm 123
rm: 無(wú)法刪除"123": 是一個(gè)目錄
[root@bogon tmp]# rm -r 123
rm:是否刪除目錄 "123"运杭?y
[root@bogon tmp]# ls
yum.log yum_save_tx-2017-02-22-13-5628OmsB.yumtx
[root@bogon tmp]# mkdir -p 123/234
[root@bogon tmp]# rm -rv 123
rm:是否進(jìn)入目錄"123"? y
rm:是否刪除目錄 "123/234"夫啊?y
已刪除目錄:"123/234"
rm:是否刪除目錄 "123"函卒?y
已刪除目錄:"123"
[root@bogon tmp]# mkdir -p 123/234
[root@bogon tmp]# /bin/rm -frv 123
已刪除目錄:"123/234"
已刪除目錄:"123"
[root@bogon tmp]# mkdir -p 123/234
[root@bogon tmp]# /bin/rm -rv 123
已刪除目錄:"123/234"
已刪除目錄:"123"
[root@bogon tmp]# rm -r 2
rm: 無(wú)法刪除"2": 沒(méi)有那個(gè)文件或目錄
[root@bogon tmp]# rm -rfv 2
[root@bogon tmp]#
02.1.3 cp 復(fù)制拷貝命令
--help cp幫助文檔
[root@bogon tmp]# cp --help
用法:cp [選項(xiàng)]... [-T] 源文件 目標(biāo)文件
或:cp [選項(xiàng)]... 源文件... 目錄
或:cp [選項(xiàng)]... -t 目錄 源文件...
將源文件復(fù)制至目標(biāo)文件辆憔,或?qū)⒍鄠€(gè)源文件復(fù)制至目標(biāo)目錄。
長(zhǎng)選項(xiàng)必須使用的參數(shù)對(duì)于短選項(xiàng)時(shí)也是必需使用的报嵌。
-a, --archive 等于-dR --preserve=all
--backup[=CONTROL 為每個(gè)已存在的目標(biāo)文件創(chuàng)建備份
-b 類(lèi)似--backup 但不接受參數(shù)
--copy-contents 在遞歸處理是復(fù)制特殊文件內(nèi)容
-d 等于--no-dereference --preserve=links
-f, --force 如果目標(biāo)文件無(wú)法打開(kāi)則將其移除并重試(當(dāng) -n 選項(xiàng)
存在時(shí)則不需再選此項(xiàng))
-i, --interactive 覆蓋前詢問(wèn)(使前面的 -n 選項(xiàng)失效)
-H 跟隨源文件中的命令行符號(hào)鏈接
-l, --link 鏈接文件而不復(fù)制
-L, --dereference 總是跟隨符號(hào)鏈接
-n, --no-clobber 不要覆蓋已存在的文件(使前面的 -i 選項(xiàng)失效)
-P, --no-dereference 不跟隨源文件中的符號(hào)鏈接
-p 等于--preserve=模式,所有權(quán),時(shí)間戳
--preserve[=屬性列表 保持指定的屬性(默認(rèn):模式,所有權(quán),時(shí)間戳)虱咧,如果
可能保持附加屬性:環(huán)境、鏈接锚国、xattr 等
-c same as --preserve=context
--sno-preserve=屬性列表 不保留指定的文件屬性
--parents 復(fù)制前在目標(biāo)目錄創(chuàng)建來(lái)源文件路徑中的所有目錄
-R, -r, --recursive 遞歸復(fù)制目錄及其子目錄內(nèi)的所有內(nèi)容
--reflink[=WHEN] 控制克隆/CoW 副本腕巡。請(qǐng)查看下面的內(nèi)如。
--remove-destination 嘗試打開(kāi)目標(biāo)文件前先刪除已存在的目的地
文件 (相對(duì)于 --force 選項(xiàng))
--sparse=WHEN 控制創(chuàng)建稀疏文件的方式
--strip-trailing-slashes 刪除參數(shù)中所有源文件/目錄末端的斜杠
-s, --symbolic-link 只創(chuàng)建符號(hào)鏈接而不復(fù)制文件
-S, --suffix=后綴 自行指定備份文件的后綴
-t, --target-directory=目錄 將所有參數(shù)指定的源文件/目錄
復(fù)制至目標(biāo)目錄
-T, --no-target-directory 將目標(biāo)目錄視作普通文件
-u, --update copy only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
-x, --one-file-system stay on this file system
-Z, --context=CONTEXT set security context of copy to CONTEXT
--help 顯示此幫助信息并退出
--version 顯示版本信息并退出
默認(rèn)情況下血筑,源文件的稀疏性僅僅通過(guò)簡(jiǎn)單的方法判斷绘沉,對(duì)應(yīng)的目標(biāo)文件目標(biāo)文件也
被為稀疏煎楣。這是因?yàn)槟J(rèn)情況下使用了--sparse=auto 參數(shù)。如果明確使用
--sparse=always 參數(shù)則不論源文件是否包含足夠長(zhǎng)的0 序列也將目標(biāo)文件創(chuàng)文
建為稀疏件车伞。
使用--sparse=never 參數(shù)禁止創(chuàng)建稀疏文件择懂。
當(dāng)指定了--reflink[=always] 參數(shù)時(shí)執(zhí)行輕量化的復(fù)制,即只在數(shù)據(jù)塊被修改的
情況下才復(fù)制另玖。如果復(fù)制失敗或者同時(shí)指定了--reflink=auto困曙,則返回標(biāo)準(zhǔn)復(fù)制模式。
備份文件的后綴為"~"谦去,除非以--suffix 選項(xiàng)或是SIMPLE_BACKUP_SUFFIX
環(huán)境變量指定慷丽。版本控制的方式可通過(guò)--backup 選項(xiàng)或VERSION_CONTROL 環(huán)境
變量來(lái)選擇。以下是可用的變量值:
none, off 不進(jìn)行備份(即使使用了--backup 選項(xiàng))
numbered, t 備份文件加上數(shù)字進(jìn)行排序
existing, nil 若有數(shù)字的備份文件已經(jīng)存在則使用數(shù)字鳄哭,否則使用普通方式備份
simple, never 永遠(yuǎn)使用普通方式備份
有一個(gè)特別情況:如果同時(shí)指定--force 和--backup 選項(xiàng)要糊,而源文件和目標(biāo)文件
是同一個(gè)已存在的一般文件的話,cp 會(huì)將源文件備份窃诉。
請(qǐng)向bug-coreutils@gnu.org 報(bào)告cp 的錯(cuò)誤
GNU coreutils 項(xiàng)目主頁(yè):<http://www.gnu.org/software/coreutils/>
GNU 軟件一般性幫助:<http://www.gnu.org/gethelp/>
請(qǐng)向<http://translationproject.org/team/zh_CN.html> 報(bào)告cp 的翻譯錯(cuò)誤
要獲取完整文檔杨耙,請(qǐng)運(yùn)行:info coreutils 'cp invocation'
[root@bogon tmp]#
-r 用于連同目錄一起復(fù)制
-i 覆蓋前詢問(wèn)
例如:
[root@bogon ~]# ls
anaconda-ks.cfg install.log install.log.syslog
[root@bogon ~]# cp install.log 123.txt
[root@bogon ~]# ls
123.txt install.log
anaconda-ks.cfg install.log.syslog
[root@bogon ~]# mkdir 111
[root@bogon ~]# cp 111 222
cp: 略過(guò)目錄"111"
[root@bogon ~]# cp -r 111 222
[root@bogon ~]# ls
111 222 install.log
123.txt anaconda-ks.cfg install.log.syslog
[root@bogon ~]# cp 123.txt 111/
[root@bogon ~]# cp -rv 111/ 234
"111/" -> "234"
"111/123.txt" -> "234/123.txt"
[root@bogon ~]# cp -r 用于拷貝目錄;^C
[root@bogon ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 16G 1.8G 14G 12% /
tmpfs 947M 0 947M 0% /dev/shm
/dev/sda1 194M 26M 158M 15% /boot
[root@bogon ~]# df -h查看當(dāng)前系統(tǒng)分區(qū)情況^C
[root@bogon ~]# cp -rv /boot/grub/ /tmp/
02.1.4 mv 移動(dòng)和重命名
--help mv幫助文檔
[root@bogon tmp]# mv --help
用法:mv [選項(xiàng)]... [-T] 源文件 目標(biāo)文件
或:mv [選項(xiàng)]... 源文件... 目錄
或:mv [選項(xiàng)]... -t 目錄 源文件...
將源文件重命名為目標(biāo)文件飘痛,或?qū)⒃次募苿?dòng)至指定目錄珊膜。
長(zhǎng)選項(xiàng)必須使用的參數(shù)對(duì)于短選項(xiàng)時(shí)也是必需使用的。
--backup[=CONTROL] 為每個(gè)已存在的目標(biāo)文件創(chuàng)建備份
-b 類(lèi)似--backup 但不接受參數(shù)
-f, --force 覆蓋前不詢問(wèn)
-i, --interactive 覆蓋前詢問(wèn)
-n, --no-clobber 不覆蓋已存在文件
如果您指定了-i宣脉、-f车柠、-n 中的多個(gè),僅最后一個(gè)生效塑猖。
--strip-trailing-slashes 去掉每個(gè)源文件參數(shù)尾部的斜線
-S, --suffix=SUFFIX 替換常用的備份文件后綴
-t, --target-directory=DIRECTORY 將所有參數(shù)指定的源文件或目錄
移動(dòng)至 指定目錄
-T, --no-target-directory 將目標(biāo)文件視作普通文件處理
-u, --update 只在源文件文件比目標(biāo)文件新竹祷,或目標(biāo)文件
不存在時(shí)才進(jìn)行移動(dòng)
-v, --verbose 詳細(xì)顯示進(jìn)行的步驟
--help 顯示此幫助信息并退出
--version 顯示版本信息并退出
備份文件的后綴為"~",除非以--suffix 選項(xiàng)或是SIMPLE_BACKUP_SUFFIX
環(huán)境變量指定羊苟。版本控制的方式可通過(guò)--backup 選項(xiàng)或VERSION_CONTROL 環(huán)境
變量來(lái)選擇塑陵。以下是可用的變量值:
none, off 不進(jìn)行備份(即使使用了--backup 選項(xiàng))
numbered, t 備份文件加上數(shù)字進(jìn)行排序
existing, nil 若有數(shù)字的備份文件已經(jīng)存在則使用數(shù)字,否則使用普通方式備份
simple, never 永遠(yuǎn)使用普通方式備份
請(qǐng)向bug-coreutils@gnu.org 報(bào)告mv 的錯(cuò)誤
GNU coreutils 項(xiàng)目主頁(yè):<http://www.gnu.org/software/coreutils/>
GNU 軟件一般性幫助:<http://www.gnu.org/gethelp/>
請(qǐng)向<http://translationproject.org/team/zh_CN.html> 報(bào)告mv 的翻譯錯(cuò)誤
要獲取完整文檔蜡励,請(qǐng)運(yùn)行:info coreutils 'mv invocation'
[root@bogon tmp]#
-i 覆蓋前詢問(wèn)
-v 可視化顯示進(jìn)行的步驟
[root@bogon tmp]# ls
yum.log yum_save_tx-2017-02-22-13-5628OmsB.yumtx
[root@bogon tmp]# mv yum.log 123.txt
[root@bogon tmp]# cp 123.txt yum.log
[root@bogon tmp]# ls
123.txt yum_save_tx-2017-02-22-13-5628OmsB.yumtx
yum.log
[root@bogon tmp]# mv 123.txt yum.log
mv:是否覆蓋"yum.log"令花? n
[root@bogon tmp]#
[root@bogon tmp]# mkdir -p 111/123
[root@bogon tmp]# tree
.
├── 111
│ └── 123
├── 123.txt
├── yum.log
└── yum_save_tx-2017-02-22-13-5628OmsB.yumtx
2 directories, 3 files
[root@bogon tmp]# mv 111 222
[root@bogon tmp]# tree
.
├── 123.txt
├── 222
│ └── 123
├── yum.log
└── yum_save_tx-2017-02-22-13-5628OmsB.yumtx
2 directories, 3 files
[root@bogon tmp]#
跨分區(qū)挪動(dòng)文件需要?jiǎng)澐值臅r(shí)間比較常。這里涉及當(dāng)磁盤(pán)的塊分區(qū)凉倚;