天天看點

Linux-檔案目錄管理

Linux-檔案目錄管理

基礎指令

指令格式
指令  [選項]    [參數]

選項:(可以有0個或多個)
    短選項:-
        多個選項可以組合: -a -b = -ab
    長選項:--
        長選項通常不能組合
        
參數:指令的作用對象(可以有0個或多個)

短選項:
[root@localhost ~]# rm -r -f *				
[root@localhost ~]# rm -rf *				//兩種意思相同
長選項:
[root@localhost ~]# ls --all
.   .bash_history  .bash_profile  .cshrc
..  .bash_logout   .bashrc        .tcshrc
[root@localhost ~]# ls --inode
[root@localhost ~]# ls --inodeall
ls: unrecognized option '--inodeall'
Try 'ls --help' for more information.		//長選項不能組合
           

目錄管理

ls

列出目錄内容

[root@localhost ~]# ls
anaconda-ks.cfg
           

-l 長格式

[root@localhost ~]# ls -l
total 4
-rw-------. 1 root root 1184 Nov 13 20:10 anaconda-ks.cfg
           

cd 切換目錄

pwd 檢視目前所在目錄路徑

[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /
[root@localhost /]# pwd
/
           

mkdir

建立目錄

-p 建立目錄時若父目錄不存在則自動建立

[root@localhost ~]# mkdir a/b/c/d
mkdir: cannot create directory ‘a/b/c/d’: No such file or directory
[root@localhost ~]# mkdir -p a/b/c/d
[root@localhost ~]# ls a
b
[root@localhost ~]# ls a/b/
c
[root@localhost ~]# ls a/b/c/
d
           

-v 顯示目錄建立過程

[root@localhost ~]# mkdir -p -v a/b/c/d
mkdir: created directory 'a'
mkdir: created directory 'a/b'
mkdir: created directory 'a/b/c'
mkdir: created directory 'a/b/c/d'
           

建立100個純數字目錄:

[root@localhost ~]# mkdir {1..100}
[root@localhost ~]# ls
1    14  2   25  30  36  41  47  52  58  63  69  74  8   85  90  96
10   15  20  26  31  37  42  48  53  59  64  7   75  80  86  91  97
100  16  21  27  32  38  43  49  54  6   65  70  76  81  87  92  98
11   17  22  28  33  39  44  5   55  60  66  71  77  82  88  93  99
12   18  23  29  34  4   45  50  56  61  67  72  78  83  89  94
13   19  24  3   35  40  46  51  57  62  68  73  79  84  9   95
           

tree

檢視目錄樹(需要先安裝)

[root@localhost ~]# mount /dev/cdrom /mnt
mount: /mnt: WARNING: device write-protected, mounted read-only.
[root@localhost ~]# rpm -ivh /mnt/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm 
warning: /mnt/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.7.0-15.el8                ################################# [100%]
[root@localhost ~]# mkdir -p a/{b/{e,f},c,d/{j,k}}
[root@localhost ~]# ls
a  anaconda-ks.cfg
[root@localhost ~]# tree a
a
├── b
│   ├── e
│   └── f
├── c
└── d
    ├── j
    └── k
    
7 directories, 0 files
           

檔案管理

touch

建立一個空檔案,touch還有一個作用是修改檔案的時間戮

[root@localhost ~]# touch a abc b
[root@localhost ~]# ls
a  abc  b
           

建立100個檔案:

[root@localhost ~]# touch file{1..100}
[root@localhost ~]# ls
file1    file2   file30  file41  file52  file63  file74  file85  file96
file10   file20  file31  file42  file53  file64  file75  file86  file97
file100  file21  file32  file43  file54  file65  file76  file87  file98
file11   file22  file33  file44  file55  file66  file77  file88  file99
file12   file23  file34  file45  file56  file67  file78  file89
file13   file24  file35  file46  file57  file68  file79  file9
file14   file25  file36  file47  file58  file69  file8   file90
file15   file26  file37  file48  file59  file7   file80  file91
file16   file27  file38  file49  file6   file70  file81  file92
file17   file28  file39  file5   file60  file71  file82  file93
file18   file29  file4   file50  file61  file72  file83  file94
file19   file3   file40  file51  file62  file73  file84  file95
           

rm

删除檔案,删除指令預設會提示是否需要删除

-r 遞歸删除,删除目錄時必須使用此選項

-f 強制删除,不詢問

[root@localhost ~]# rm -rf *			//删除所有檔案
[root@localhost ~]# ls

           

cp

複制檔案,一個檔案到一個檔案,多個檔案到一個目錄

//一個檔案到一個檔案:
[root@localhost ~]# cp anaconda-ks.cfg abc
[root@localhost ~]# ll
total 8
-rw-------. 1 root root 1184 Nov 14 18:14 abc
-rw-------. 1 root root 1184 Nov 14 18:13 anaconda-ks.cfg

//多個檔案到一個目錄:
[root@localhost ~]# touch {1..10}
[root@localhost ~]# mkdir 11
[root@localhost ~]# cp {1..10} anaconda-ks.cfg 11
[root@localhost ~]# ls 11
1  10  2  3  4  5  6  7  8  9  anaconda-ks.cfg-r      
           
[root@localhost ~]# mkdir 123
[root@localhost ~]# cp 123 456
cp: -r not specified; omitting directory '123'		//拷貝目錄需要使用-r
[root@localhost ~]# cp -r 123 456
[root@localhost ~]# ll
total 8
drwxr-xr-x. 2 root root    6 Nov 14 18:15 123
drwxr-xr-x. 2 root root    6 Nov 14 18:15 456
-rw-------. 1 root root 1184 Nov 14 18:14 abc
-rw-------. 1 root root 1184 Nov 14 18:13 anaconda-ks.cfg
           

stat

[root@localhost ~]# touch 123
[root@localhost ~]# stat 123
  File: 123
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 101211984   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-11-14 18:11:19.787085296 +0800
Modify: 2020-11-14 18:11:19.787085296 +0800
Change: 2020-11-14 18:11:19.787085296 +0800
 Birth: -
           

mv

[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 14 19:00 123
[root@localhost ~]# mv 123 456
[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 14 19:00 456
[root@localhost ~]# mv 456 /opt
[root@localhost ~]# ll /opt
total 0
-rw-r--r--. 1 root root    0 Nov 14 19:00 456
           

如何擷取指令幫助

//内部指令
    help COMMAND
//外部指令
    COMMAND --help
//線上文檔
    info COMMAND    
//指令手冊  manual
    man COMMAND
//文檔
    /usr/share/doc 
    
whatis COMMAND      //用于檢視COMMAND出現在man的哪一章節中。

//man是分章節的,以下是每一章節的内容介紹:
    1   使用者指令(/bin,/usr/bin,/usr/local/bin);
    2   系統調用;
    3   庫調用;
    4   特殊檔案(裝置檔案);
    5   檔案格式(配置檔案 的文法);
    6   遊戲;
    7   雜項(Miscellaneous);
    8   管理指令(/sbin,/usr/sbin,/usr/local/sbin)
    
//man手冊注意事項:
    []              //可選
    <>              //必選
    ...             //可以出現多次
    |               //多選一
    {}              //分組
    NAME            //指令名稱及功能簡要說明
    SYNOPSIS        //用法說明,包括可用的選項
    DESCRIPTION     //指令功能的詳盡說明,可能包括每一個選項的意義
    OPTIONS         //說明每一個選項的意義
    FILES           //此指令相關的配置檔案
    BUGS            //報告bug
    EXAMPLES        //使用示例
    SEE ALSO        //另外參照
    
//man翻屏
    向後翻一屏       //SPACE
    向前翻一屏       //b
    向後翻一行       //enter
    向前翻一行       //k
    
//查找
    /KEYWORD    //向後
    ?KEYWORD    //向前
    n           //下一個
    N           //前一個
    q           //退出    
           

繼續閱讀