2017-05-22 第一周第五次課

第一周第五次課

日期: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é)會了相對路徑和絕對路徑;

相對路徑可以是相對當(dāng)前用戶的,也可以再某個目錄下直接創(chuàng)建,也算是相對路徑;“包含 ~/還有./ 以及../ 這些都是表示的相對路徑。

絕對路徑是將目錄一級一級都寫清楚,通常直接以/ 開頭。

--help mkdir幫助文檔
[root@bogon tmp]# mkdir --help
用法:mkdir [選項]... 目錄...
若指定目錄不存在則創(chuàng)建目錄。

長選項必須使用的參數(shù)對于短選項時也是必需使用的。
  -m, --mode=模式     設(shè)置權(quán)限模式(類似chmod),而不是rwxrwxrwx 減umask
  -p, --parents         需要時創(chuàng)建目標(biāo)目錄的上層目錄,但即使這些目錄已存在也不當(dāng)作錯誤處理
  -v, --verbose         每次創(chuàng)建新目錄都顯示信息
  -Z, --context=CTX     將每個創(chuàng)建的目錄的SELinux 安全環(huán)境設(shè)置為CTX
      --help        顯示此幫助信息并退出
      --version     顯示版本信息并退出

請向bug-coreutils@gnu.org 報告mkdir 的錯誤
GNU coreutils 項目主頁:<http://www.gnu.org/software/coreutils/>
GNU 軟件一般性幫助:<http://www.gnu.org/gethelp/>
請向<http://translationproject.org/team/zh_CN.html> 報告mkdir 的翻譯錯誤
要獲取完整文檔,請運行:info coreutils 'mkdir invocation'
[root@bogon tmp]# 
mkdir -p 創(chuàng)建連串目錄
mkdir -v 創(chuàng)建過程可查看

rmdir 只能刪除空目錄

--help rmdir幫助文檔
[root@bogon tmp]# rmdir --help
用法:rmdir [選項]... 目錄...
刪除指定的空目錄。

      --ignore-fail-on-non-empty
            忽略僅由目錄非空產(chǎn)生的所有錯誤
  -p, --parents     刪除指定目錄及其上級文件夾,例如"rmdir -p a/b/c'"
            與"rmdir a/b/c a/b a'" 基本相同
  -v, --verbose     輸出處理的目錄詳情
      --help        顯示此幫助信息并退出
      --version     顯示版本信息并退出

請向bug-coreutils@gnu.org 報告rmdir 的錯誤
GNU coreutils 項目主頁:<http://www.gnu.org/software/coreutils/>
GNU 軟件一般性幫助:<http://www.gnu.org/gethelp/>
請向<http://translationproject.org/team/zh_CN.html> 報告rmdir 的翻譯錯誤
要獲取完整文檔,請運行:info coreutils 'rmdir invocation'
[root@bogon tmp]# 
-p 刪除連串的空目錄
-v 可以查看刪除的空目錄
[root@bogon ~]# mkdir /tmp/1/2/3
mkdir: 無法創(chuàng)建目錄"/tmp/1/2/3": 沒有那個文件或目錄
[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 來安裝;

--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 [選項]... 文件...
刪除 (unlink) 文件。

  -f, --force       強制刪除。忽略不存在的文件,不提示確認(rèn)
  -i            在刪除前需要確認(rèn)
  -I            在刪除超過三個文件或者遞歸刪除前要求確認(rèn)。此選項比-i 提
            示內(nèi)容更少,但同樣可以阻止大多數(shù)錯誤發(fā)生
      --interactive[=WHEN]  根據(jù)指定的WHEN 進(jìn)行確認(rèn)提示:never,once (-I),
                或者always (-i)。如果此參數(shù)不加WHEN 則總是提示
      --one-file-system     遞歸刪除一個層級時,跳過所有不符合命令行參
                數(shù)的文件系統(tǒng)上的文件
      --no-preserve-roo 不特殊對待"/"
      --preserve-root   不允許刪除"/"(默認(rèn))
  -r, -R, --recursive   遞歸刪除目錄及其內(nèi)容
  -v, --verbose     詳細(xì)顯示進(jìn)行的步驟
      --help        顯示此幫助信息并退出
      --version     顯示版本信息并退出

默認(rèn)時,rm 不會刪除目錄。使用--recursive(-r 或-R)選項可刪除每個給定
的目錄,以及其下所有的內(nèi)容。

要刪除第一個字符為"-"的文件 (例如"-foo"),請使用以下方法之一:
  rm -- -foo
  rm ./-foo

請注意,如果使用rm 來刪除文件,通常仍可以將該文件恢復(fù)原狀。如果想保證
該文件的內(nèi)容無法還原,請考慮使用shred。

請向bug-coreutils@gnu.org 報告rm 的錯誤
GNU coreutils 項目主頁:<http://www.gnu.org/software/coreutils/>
GNU 軟件一般性幫助:<http://www.gnu.org/gethelp/>
請向<http://translationproject.org/team/zh_CN.html> 報告rm 的翻譯錯誤
要獲取完整文檔,請運行:info coreutils 'rm invocation'
[root@bogon tmp]# 
-f --force 過濾提示信息,靜默;
-i 默認(rèn)不提醒
-r --recursive 刪除目錄
-v 刪除過程可視化
[root@bogon tmp]# which rm
alias rm='rm -i'
    /bin/rm
[root@bogon tmp]# -f 忽略錯我提示;-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: 無法刪除"123": 是一個目錄
[root@bogon tmp]# rm 123
rm: 無法刪除"123": 是一個目錄
[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: 無法刪除"2": 沒有那個文件或目錄
[root@bogon tmp]# rm -rfv 2
[root@bogon tmp]# 

02.1.3 cp 復(fù)制拷貝命令

--help cp幫助文檔
[root@bogon tmp]# cp --help
用法:cp [選項]... [-T] 源文件 目標(biāo)文件
 或:cp [選項]... 源文件... 目錄
 或:cp [選項]... -t 目錄 源文件...
將源文件復(fù)制至目標(biāo)文件,或?qū)⒍鄠€源文件復(fù)制至目標(biāo)目錄。

長選項必須使用的參數(shù)對于短選項時也是必需使用的。
  -a, --archive         等于-dR --preserve=all
      --backup[=CONTROL     為每個已存在的目標(biāo)文件創(chuàng)建備份
  -b                類似--backup 但不接受參數(shù)
      --copy-contents       在遞歸處理是復(fù)制特殊文件內(nèi)容
  -d                等于--no-dereference --preserve=links
  -f, --force           如果目標(biāo)文件無法打開則將其移除并重試(當(dāng) -n 選項
                    存在時則不需再選此項)
  -i, --interactive     覆蓋前詢問(使前面的 -n 選項失效)
  -H                跟隨源文件中的命令行符號鏈接
  -l, --link            鏈接文件而不復(fù)制
  -L, --dereference     總是跟隨符號鏈接
  -n, --no-clobber      不要覆蓋已存在的文件(使前面的 -i 選項失效)
  -P, --no-dereference      不跟隨源文件中的符號鏈接
  -p                等于--preserve=模式,所有權(quán),時間戳
      --preserve[=屬性列表  保持指定的屬性(默認(rèn):模式,所有權(quán),時間戳),如果
                    可能保持附加屬性:環(huán)境、鏈接、xattr 等
  -c                           same as --preserve=context
      --sno-preserve=屬性列表   不保留指定的文件屬性
      --parents         復(fù)制前在目標(biāo)目錄創(chuàng)建來源文件路徑中的所有目錄
  -R, -r, --recursive       遞歸復(fù)制目錄及其子目錄內(nèi)的所有內(nèi)容
      --reflink[=WHEN]      控制克隆/CoW 副本。請查看下面的內(nèi)如。
      --remove-destination  嘗試打開目標(biāo)文件前先刪除已存在的目的地
                    文件 (相對于 --force 選項)
      --sparse=WHEN     控制創(chuàng)建稀疏文件的方式
      --strip-trailing-slashes  刪除參數(shù)中所有源文件/目錄末端的斜杠
  -s, --symbolic-link       只創(chuàng)建符號鏈接而不復(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)情況下,源文件的稀疏性僅僅通過簡單的方法判斷,對應(yīng)的目標(biāo)文件目標(biāo)文件也
被為稀疏。這是因為默認(rèn)情況下使用了--sparse=auto 參數(shù)。如果明確使用
--sparse=always 參數(shù)則不論源文件是否包含足夠長的0 序列也將目標(biāo)文件創(chuàng)文
建為稀疏件。
使用--sparse=never 參數(shù)禁止創(chuàng)建稀疏文件。

當(dāng)指定了--reflink[=always] 參數(shù)時執(zhí)行輕量化的復(fù)制,即只在數(shù)據(jù)塊被修改的
情況下才復(fù)制。如果復(fù)制失敗或者同時指定了--reflink=auto,則返回標(biāo)準(zhǔn)復(fù)制模式。

備份文件的后綴為"~",除非以--suffix 選項或是SIMPLE_BACKUP_SUFFIX
環(huán)境變量指定。版本控制的方式可通過--backup 選項或VERSION_CONTROL 環(huán)境
變量來選擇。以下是可用的變量值:

  none, off       不進(jìn)行備份(即使使用了--backup 選項)
  numbered, t     備份文件加上數(shù)字進(jìn)行排序
  existing, nil   若有數(shù)字的備份文件已經(jīng)存在則使用數(shù)字,否則使用普通方式備份
  simple, never   永遠(yuǎn)使用普通方式備份

有一個特別情況:如果同時指定--force 和--backup 選項,而源文件和目標(biāo)文件
是同一個已存在的一般文件的話,cp 會將源文件備份。

請向bug-coreutils@gnu.org 報告cp 的錯誤
GNU coreutils 項目主頁:<http://www.gnu.org/software/coreutils/>
GNU 軟件一般性幫助:<http://www.gnu.org/gethelp/>
請向<http://translationproject.org/team/zh_CN.html> 報告cp 的翻譯錯誤
要獲取完整文檔,請運行:info coreutils 'cp invocation'
[root@bogon tmp]# 
-r 用于連同目錄一起復(fù)制
-i 覆蓋前詢問

例如:

[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: 略過目錄"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 移動和重命名

--help mv幫助文檔
[root@bogon tmp]# mv --help
用法:mv [選項]... [-T] 源文件 目標(biāo)文件
 或:mv [選項]... 源文件... 目錄
 或:mv [選項]... -t 目錄 源文件...
將源文件重命名為目標(biāo)文件,或?qū)⒃次募苿又林付夸洝?
長選項必須使用的參數(shù)對于短選項時也是必需使用的。
      --backup[=CONTROL]       為每個已存在的目標(biāo)文件創(chuàng)建備份
  -b                           類似--backup 但不接受參數(shù)
  -f, --force                  覆蓋前不詢問
  -i, --interactive            覆蓋前詢問
  -n, --no-clobber             不覆蓋已存在文件
如果您指定了-i、-f、-n 中的多個,僅最后一個生效。
      --strip-trailing-slashes  去掉每個源文件參數(shù)尾部的斜線
  -S, --suffix=SUFFIX       替換常用的備份文件后綴
  -t, --target-directory=DIRECTORY  將所有參數(shù)指定的源文件或目錄
                    移動至 指定目錄
  -T, --no-target-directory 將目標(biāo)文件視作普通文件處理
  -u, --update          只在源文件文件比目標(biāo)文件新,或目標(biāo)文件
                不存在時才進(jìn)行移動
  -v, --verbose     詳細(xì)顯示進(jìn)行的步驟
      --help        顯示此幫助信息并退出
      --version     顯示版本信息并退出

備份文件的后綴為"~",除非以--suffix 選項或是SIMPLE_BACKUP_SUFFIX
環(huán)境變量指定。版本控制的方式可通過--backup 選項或VERSION_CONTROL 環(huán)境
變量來選擇。以下是可用的變量值:

  none, off       不進(jìn)行備份(即使使用了--backup 選項)
  numbered, t     備份文件加上數(shù)字進(jìn)行排序
  existing, nil   若有數(shù)字的備份文件已經(jīng)存在則使用數(shù)字,否則使用普通方式備份
  simple, never   永遠(yuǎn)使用普通方式備份

請向bug-coreutils@gnu.org 報告mv 的錯誤
GNU coreutils 項目主頁:<http://www.gnu.org/software/coreutils/>
GNU 軟件一般性幫助:<http://www.gnu.org/gethelp/>
請向<http://translationproject.org/team/zh_CN.html> 報告mv 的翻譯錯誤
要獲取完整文檔,請運行:info coreutils 'mv invocation'
[root@bogon tmp]# 
-i 覆蓋前詢問
-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)磁盤的塊分區(qū);

rename 用于重命名

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在Linux系統(tǒng)中,以“.”開頭的文件,表明該文件是隱藏文件。下面主要介紹一下ls指令的用法以及tree指令的用法...
    丿水木灬年華閱讀 751評論 1 0
  • 1.Linux上的文件管理類命令,其常用的使用方法及其相關(guān)示例演示 文件與目錄的管理上,不外乎顯示屬性、 拷貝、刪...
    whamai閱讀 340評論 0 0
  • 命令是系統(tǒng)操作員對系統(tǒng)傳入的指令,傳入指令后回車,系統(tǒng)接收到指令做出相應(yīng)的行為 1.查看用戶位于系統(tǒng)什么位置 【p...
    子非初心閱讀 493評論 0 1
  • Linux基礎(chǔ) review: Linux發(fā)展史 裝機注意事項 ls、tree "."也是文件名的一部分 ====...
    張神救我閱讀 322評論 1 0
  • 1、Linux上的文件管理類命令常用的使用方法 本篇涉及命令:cat,tac,more,less,head,tai...
    zhangdaifu閱讀 1,414評論 0 3

友情鏈接更多精彩內(nèi)容