Linux基礎——文件管理

Linux基礎——文件管理

文件管理及常用命令詳解

      Linux文件系統是以/為基本的倒置樹狀結構,我們來進一步學習下文件或者目錄的操作和管理命令: 

目錄相關操作:cd、pwd、mkdir、rmdir 
目錄及文件檢索:ls 
復制、移動、刪除:cp、mv、rm 
文件內容查看:cat、tac、more、less、head、tail 
查看文件類型:file(type:查看命令類型) 
命令和文件搜索:which、whereis、find 

下面我們逐個進行解釋:

1.cd

Change directory的縮寫,變換工作目錄的命令。
    cd [dir] 最簡單的用法,目錄可以是絕對路徑也可以是相對路徑
    cd ~$USERNAME 表示切換至$USERNAME的家目錄下

2、pwd

Print Working Directory,顯示當前所在目錄的路徑
    【OPTION】
    -P [dir]    表示實際的路徑,而非使用link的路徑
    【FOR EXAMPOR】
        [root@localhost ppp]# cd -P /var/mail/
        [root@localhost mail]# pwd
        /var/spool/mail
        [root@localhost mail]# pwd -P
        /var/spool/mail

3、mkdir

Make Directories,創建目錄文件
    【OPTION】
    -m,--mode=MODE  
            set file mode,not a=rwx-umask指定權限,不受umask影響
    -p,--parents
            no error if existing,make parent directories as needed
            根據需要生成父目錄(如果父目錄不存在),不會報錯
    -v,--verbose
            print a message for each created directories.
            打印創建目錄消息
    【FOR EXAMPLE】
        [root@localhost tmp]# mkdir -m 555 test
        [root@localhost tmp]# umask
        0022
        [root@localhost tmp]# ll -a
        dr-xr-xr-x.  2 root root    6 Sep 24 22:45 test
        [root@localhost tmp]# mkdir -pv test/testx/testy
        mkdir: created directory ‘test/testx’
        mkdir: created directory ‘test/testx/testy

4、rmdir

Remove empty directories,刪除空目錄
    【SYNOPSIS】
    rmdir [option]... [dir]...
    【OPTION】
        -p, --parents   
            remove DIRECTORY and its ancestors(上代、祖先)
            刪除目錄及其上級目錄一起刪除
        -v,--verbose
    【FOR EXAMPLE】
        [root@localhost tmp]# ls -ld /tmp/*
        -rw-r--r--.  1 root root  134 Sep 21 23:07 /tmp/issue
        drwx------.  2 ftp  ftp    59 Sep 21 22:20 /tmp/skel
        dr-xr-xr-x.  3 root root   18 Sep 24 22:53 /tmp/test
        -rwx------.  1 root root    0 Sep 21 20:27 /tmp/test1
        drwxr-xr-x. 11 root root 4096 Sep 22 01:35 /tmp/x
        [root@localhost tmp]# rmdir -p test/testx/testy
        [root@localhost tmp]# ls -ld /tmp/*
        -rw-r--r--.  1 root root  134 Sep 21 23:07 /tmp/issue
        drwx------.  2 ftp  ftp    59 Sep 21 22:20 /tmp/skel
        -rwx------.  1 root root    0 Sep 21 20:27 /tmp/test1
        drwxr-xr-x. 11 root root 4096 Sep 22 01:35 /tmp/x

5、ls

List Directory contents,列舉路徑目錄
    【SYNOPSIS】
    ls [option]... [file]...
    【OPTION】
        -a,--all
            don't ignore entries starting with.
            列舉全部的文檔,連同隱藏的文檔(.開頭的文檔)
        -A,--almost-all
            don't list implied . and ..
        -l  use a long listing format
            使用長格式顯示,每一行數據代表一個文件或者目錄
        -f  do not sort, enable -aU, disable -ls --color
            不進行排序的顯示
        -h,--human-readable
            with -l,print sizes in human readable format
            結合-l選項一起使用,以人類可讀的格式顯示(例如:kb,MB,GB等)
        -i,--inode
            print the index number for each file
            顯示每個文件的inode號
        -s,--size
            print the allocated size of each file,in blocks
            顯示每個文件分配的大小
        -S  sort by file size
            按文件大小排序,結合-l選項使用更清楚
        -r,--reverse(逆向)
            reverse order while sorting
            逆向排序顯示,跟ls命令單獨使用的結果相反
        -R,--recursive
            list subdirectories recursively
            連同子目錄的內容一并列舉(此命令慎用)
        -g  like -l,but don't list owner
        -G,--no-group

6、cp

Copy Files and Directories,復制文件和目錄,如果目標不存在,則創建目標文件或者目錄,如果目標存在,則直接覆蓋
    【SYNOPSIS】
    cp [OPTION]... [-T] SOURCE DEST     /*單源復制,SOURCE源文件,DESTINATION目標*/
    cp [OPTION]... SOURCE... DIRECTORY      /*多源復制*/
    cp [OPTION]... -t DIRECTORY SOURCE...       /*多源復制*/
    【OPTION】
        -a,--archive
            same as -dR --preserve=all
            保留源文件的所有屬性,遞歸復制
        -i,--interactive
            prompt before overwrite 覆蓋之前交互提示
        -f,--force
            if an existing destination file cannot be opened,remove it and try again
            如果目標文件無法打開,則刪除它并重新嘗試復制。也就是不進行提示,強制復制
        -R,-r,--recursive
            copy directories recursively 遞歸式復制
        -d,same as --no-dereference --preserve=links
            如果源文件是個鏈接文件,則將鏈接文件的源文件內容復制,但不指向源文件;選項與-p(P)同

7、mv

Move or Rename files
    【SYNOPSIS】
    mv [OPTION]... [-T] SOURCE DEST
    mv [OPTION]... SOURCE... DIRECTORY
    mv [OPTION]... -t DIRECTORY SOURCE...
    【OPTION】
        -i,--interactive 交互式進行移動或者重命名
        -f,--force  強制移動或者重命名
        -n,--no-clobber
            don't overwrite an existing file 對已存在文件不進行重寫

8、rm

Remove files and directories
    【SYNOPSIS】
    rm [OPTION]... FILE...
    【OPTION】
        -f,--force  強制刪除不提示
        -i  每刪除一個文件均提示確認
        -I  當刪除多于3個文件時只提示一次
        --no-preserve-root  don't treat '/' specially   可刪除/

9、cat

concatenate files and print on the standard output 標準輸出文件內容
    【SYNOPSIS】
    cat [option]... [file]...
    【OPTION】
        -A,--show all   equivalent to -vET
        -b,--number-nonblank
            number nonempty output lines,overrides -n
            非空白行顯示行號顯示內容,與-n選項相反
        -E,--show-ends
            display $ at the end of each line
            每行結尾處帶$符號顯示
        -n,--number
            number all output lines 每行顯示行號,包括空白行
        -T,--show-tabs
            display TAB characters as ^I,用^I替換TAB空白字符顯示文件
        -v,--show-nonprinting
            use ^ and M- notation,except for LFD and TAB
        -s,--squeeze-blank
            suppress repeated empty output lines 重復空白行只顯示一行
    【FOR EXAMPLE】

10、tac

concatenate files and print in reverse  反向輸出文檔內容,是cat的反義<br />

11、 more

    file perusal filter for crt viewing     分頁式查看文件內容,類似cat
    【SYNOPSIS】          
    more [options] file [...]
    【OPTION】
        -number This  option  specifies an integer number which
          is the screen size (in lines)
            設置每頁顯示的行數
        -d  display help instead of ring bell
            顯示幫助信息:SPACE繼續查看,q退出
        -l  suppress pause after form feet
            遇到^字符時不會終止瀏覽
        -p  清屏后顯示內容
        -c  顯示內容后清理其他內容 
        -s  遇到連續重復的空白行后只顯示一行
        +NUM    display file beginning from line number NUM
                從給定的行號開始顯示全部內容
        +/string    display file beginning from search string match
                根據匹配字符串的位置開始顯示文檔內容
    【FOR EXAMPLE】
            [root@localhost tmp]# more -10 +/unset functions 
                ...skipping
                  . /etc/profile.d/lang.sh 2>/dev/null
                  # avoid propagating LANGSH_SOURCED any further
                  unset LANGSH_SOURCED
                fi                  
                # Read in our configuration
                if [ -z "${BOOTUP:-}" ]; then
                  if [ -f /etc/sysconfig/init ]; then
                      . /etc/sysconfig/init
                  else
                --More--(13%)

12、less

    opposite of more 同樣是翻頁顯示內容,只是less可以上下翻頁及其他功能
    【OPTION】
        -b <緩沖區大小> 設置緩沖區的大小
        -e  當文件顯示結束后,自動離開           
        -f  強迫打開特殊文件,例如外圍設備代號、目錄和二進制文件          
        -g  只標志最后搜索的關鍵詞         
        -i  忽略搜索時的大小寫           
        -m  顯示類似more命令的百分比          
        -N  顯示每行的行號         
        -o <文件名> 將less 輸出的內容在指定文件中保存起來          
        -Q  不使用警告音          
        -s  顯示連續空行為一行           
        -S  行過長時間將超出部分舍棄            
        -x <數字> 將“tab”鍵顯示為規定的數字空格

13、head

    output the first part of files 顯示文件的第一部分
    【SYNOPSIS】
    head [OPTION]... [FILE]...
    【OPTION】
        -c,--bytes  按字節輸出內容,例如:-c 20 顯示前20個字節的內容
        -n,--lines  按行數顯示內容
        -q,--quiet  靜默模式,不顯示文件名
        -v,--verbose 總是顯示文件名
    【FOR EXAMPLE】
        [root@localhost tmp]# head -v -n 3 functions 
        ==> functions <==
        # -*-Shell-script-*-
        #
        # functions This file contains functions to be used by most or all
        [root@localhost tmp]# head -c 20 issue
        hwo are you?
        fine,th[root@localhost tmp]#

14、tail

    Print the last 10 lines of each FILE to standard output. 標準輸出每一份文件的最后10行
    【SYNOPSIS】
    tail [OPTION]... [FILE]...
    【OPTION】
        -f,--follow     隨著文件自增將內容追加到標準輸出
        -n,--lines      設定顯示的行數
        -s,--sleep-interval=N
                與-f參數一起用,表示刷新間隔
        -c,--bytes=K    從K字節位置顯示內容
    【FOR EXAMPLE】       
        基本用法同head類似

15、file

    Determine type of FILEs 詳細明確文件類型
    【SYNOPSIS】
    file [OPTION...] [FILE...]
    【OPTION】
    【FOR EXAMPLE】
        [root@localhost tmp]# file x/af1.txt 
        x/af1.txt: ASCII text
文件的元數據

stat

Display file or file system status
[root@localhost tmp]# stat issue
  File: ‘issue’  文件名
  Size: 191   文件大小      Blocks: 8  塊大小      IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 101365706   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2016-09-25 11:42:52.578622335 +0800訪問時間
Modify: 2016-09-25 11:42:48.371622436 +0800修改時間
Change: 2016-09-25 11:42:48.372622436 +0800元數據改變時間
 Birth: -

stat 默認會得到文件的元數據,其包括該文件的所有信息如上;我們通常可以根據文件的時間戳來確定文件的被使用情況,可通過命令touch來修改時間戳數據,不做詳細解釋。

命令別名

alias

Define or display aliases   定義或者顯示aliases
alias作為一個linux內置命令,可以重新定義已有命令的格式;例如:alias rm=rm -i之后,使用rm命令則表示刪除file之前且確認是否刪除
【OPTION】
    -p  Print all defined aliases in a reusable format 顯示所有定義的aliases
【FOR EXAMPLE】
    [root@localhost ~]# alias -p
    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 vi='vim'

    [root@localhost ~]#alias tail='tail -f'
命令引用

命令的執行結果有很多種方式,其中之一如下:

  • “也就是1(?。┲暗哪莻€符號,表示優先執行

[root@localhost tmp]# cat `pwd`
cat: /tmp: Is a directory
[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# cat /tmp
cat: /tmp: Is a directory
  • 變量,簡單的命令引用可使用變量

[root@localhost ~]# cat $(pwd)
cat: /root: Is a directory
與上一個命令的結果一樣。

命令行展開演示

     創建/tmp目錄下的a_c,a_d,b_c,b_d

        [root@localhost tmp]# mkdir -pv /tmp/{a,b}_{c,d}
        mkdir: created directory ‘/tmp/a_c’
        mkdir: created directory ‘/tmp/a_d’
        mkdir: created directory ‘/tmp/b_c’
        mkdir: created directory ‘/tmp/b_d’
        [root@localhost tmp]# ls
        a_c  a_d  b_c  b_d  issue  skel  test1  x

     創建/tmp/mylinux目錄

    [root@localhost tmp]# mkdir -pv /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-
    scripts},lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}
        mkdir: created directory ‘/tmp/mylinux’
        mkdir: created directory ‘/tmp/mylinux/bin’
        mkdir: created directory ‘/tmp/mylinux/boot’
        mkdir: created directory ‘/tmp/mylinux/boot/grub’
        mkdir: created directory ‘/tmp/mylinux/dev’
        mkdir: created directory ‘/tmp/mylinux/etc’
        mkdir: created directory ‘/tmp/mylinux/etc/rc.d’
        mkdir: created directory ‘/tmp/mylinux/etc/rc.d/init.d’
        mkdir: created directory ‘/tmp/mylinux/etc/sysconfig’
        mkdir: created directory ‘/tmp/mylinux/etc/sysconfig/network-scripts’
        mkdir: created directory ‘/tmp/mylinux/lib64’
        mkdir: created directory ‘/tmp/mylinux/proc’
        mkdir: created directory ‘/tmp/mylinux/sbin’
        mkdir: created directory ‘/tmp/mylinux/sys’
        mkdir: created directory ‘/tmp/mylinux/tmp’
        mkdir: created directory ‘/tmp/mylinux/usr’
        mkdir: created directory ‘/tmp/mylinux/usr/local’
        mkdir: created directory ‘/tmp/mylinux/usr/local/bin’
        mkdir: created directory ‘/tmp/mylinux/usr/local/sbin’
        mkdir: created directory ‘/tmp/mylinux/var’
        mkdir: created directory ‘/tmp/mylinux/var/lock’
        mkdir: created directory ‘/tmp/mylinux/var/log’
        mkdir: created directory ‘/tmp/mylinux/var/run’
        [root@localhost tmp]# tree -d /tmp/mylinux/
        /tmp/mylinux/
        ├── bin
        ├── boot
        │   └── grub
        ├── dev
        ├── etc
        │   ├── rc.d
        │   │   └── init.d
        │   └── sysconfig
        │       └── network-scripts
        ├── lib64
        ├── proc
        ├── sbin
        ├── sys
        ├── tmp
        ├── usr
        │   └── local
        │       ├── bin
        │       └── sbin
        └── var
            ├── lock
            ├── log
            └── run         
        22 directories

顯示/var目錄下所有以了開頭,以一個小寫字母結尾,且中間至少有一位數字(可以有其他字符)的文件或者目錄

[root@localhost tmp]# ls -dl /var/l*[0-9]*[a-z]

顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或者目錄

[root@localhost tmp]#ll -d /etc/[0-9]*[^0-9]

顯示/etc目錄下,以非字母開頭,后面跟了一個字母以及其他任意長度任意字符的目錄或者文件

[root@localhost tmp]#ll -d /etc/[^a-z][^[:digit:]]*
[root@localhost tmp]# ll -d /tmp/[^[:alpha:]][^[:digit:]]*
-rw-r--r--. 1 root root 0 Sep 25 12:22 /tmp/1tfile2016n
-rw-r--r--. 1 root root 0 Sep 25 13:35 /tmp/2ABsdf
-rw-r--r--. 1 root root 0 Sep 25 13:34 /tmp/2asdljfl

在/tmp目錄下創建以tfile開頭,后跟當前日期和時間的文件,文件格式:tfile-2016-05-27-09-32-22

[root@localhost tmp]#touch /tmp/tfile-`date +%Y-%m-%d-%H-%M-%S`
[root@localhost tmp]#ll -a 
-rw-r--r--.  1 root root    0 Sep 25 13:39 tfile-2016-09-25-13-39-47

復制/etc目錄下所有以P開頭,以非數字結尾的文件或者目錄到/tmp/mytest1目錄中

[root@localhost tmp]#mkdir mytest1
[root@localhost tmp]#cp -af /etc/p*[^0-9] /tmp/mytest1
[root@localhost mytest1]# ls
pam.d   passwd-  plymouth  popt.d   ppp             printcap  profile.d  python
passwd  pki      pm        postfix  prelink.conf.d  profile   protocols

復制/etc目錄下所有以.d結尾的文件或者目錄至/tmp/mytest2/下

[root@localhost tmp]#mkdir mytest2
[root@localhost tmp]# cp -af /etc/*.d /tmp/mytest2/
[root@localhost mytest2]# ls
bash_completion.d  dnsmasq.d      logrotate.d     popt.d          rc2.d  rc.d        sysctl.d
binfmt.d           dracut.conf.d  modprobe.d      prelink.conf.d  rc3.d  rsyslog.d   tmpfiles.d
chkconfig.d        grub.d         modules-load.d  profile.d       rc4.d  rwtab.d     xinetd.d
cron.d             init.d         my.cnf.d        rc0.d           rc5.d  statetab.d  yum.repos.d
depmod.d           ld.so.conf.d   pam.d           rc1.d           rc6.d  sudoers.d

復制/etc目錄下所有以l或者m或者n開頭的,以.conf結尾的文件至/tmp/mytest3目錄中

[root@localhost tmp]# mkdir mytest3
[root@localhost tmp]# cp -a /etc/[lmn]*.conf /tmp/mytest3/
[root@localhost tmp]# ls -Al mytest3/
total 48
-rw-r--r--.  1 root root   28 Feb 28  2013 ld.so.conf
-rw-r-----.  1 root root  191 Oct 29  2014 libaudit.conf
-rw-r--r--.  1 root root 2391 Oct 13  2013 libuser.conf
-rw-r--r--.  1 root root   19 Sep 12 22:32 locale.conf
-rw-r--r--.  1 root root  662 Jul 31  2013 logrotate.conf
-rw-r--r--.  1 root root 5171 Jun 10  2014 man_db.conf
-rw-r--r--.  1 root root  936 Mar  6  2015 mke2fs.conf
-rw-r--r--.  1 root root 1717 Sep 12 22:32 nsswitch.conf
-rw-r--r--.  1 root root 2050 Sep 12 22:32 ntp.conf


原創文章,作者:396064847,如若轉載,請注明出處:http://www.www58058.com/49164

(0)
396064847396064847
上一篇 2016-09-27
下一篇 2016-09-28

相關推薦

  • 系統管理至grub故障排錯及自建linux

    第二章    系統啟動故障排除     1、grub配置文件寫錯,無法進入系統     步驟:(修復完成后記得修改配置文件為正確的文件)     方法一:進入啟動菜單項后,修改菜單項為正確的內容,然后…

    Linux干貨 2016-09-13
  • 如何在linux系統上獲取命令的幫助信息,man文檔的章節劃分

    內置命令獲取幫助文檔的方式:   判斷一個命令是否是Linux內置的,可以用type命令,只有Linux的內置命令才能使用 help 命令獲得幫助。 type cd 輸出:cd is a shell builtin 說明這個命令是系統自帶的內置命令,那么就可以使用 help命令來獲取幫助。 help 命令名 外部命令獲取幫助文檔的方式: 第一種辦法是使用 &…

    Linux干貨 2016-10-31
  • PHP的類自動加載機制

    在PHP開發過程中,如果希望從外部引入一個class,通常會使用include和require方法,去把定義這個class的文件包含進來。這個在小規模開發的時候,沒什么大問題。但在大型的開發項目中,這么做會產生大量的require或者include方法調用,這樣不因降低效率,而且使得代碼難以維護,況且require_once的代價很大。 在PHP5之前,各個…

    Linux干貨 2015-04-10
  • N21沉舟11周作業

    1、詳細描述一次加密通訊的過程,結合圖示最佳。 2、描述創建私有CA的過程,以及為客戶端發來的證書請求進行辦法證書。 一、CA服務器端 #進入CA目錄:cd    /etc/pki/CA #創建初始文件touch index.txt serialecho 01 >&nbsp…

    Linux干貨 2016-09-19
  • N25-第三周作業

    1.列出當前系統所有已經登錄的用戶的用戶名,注意同一個用戶登錄多次,只顯示一次即可   who | cut -d" " -f1 |uniq 2.取出最后登錄到當前系統的用戶的相關信息    w | tail -1 3.取出當前系統上被用戶當做其默認shell最多的那個shell &nbsp…

    Linux干貨 2016-12-19
  • 第三周作業

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。       首先用who命令列出當前系統上所有登陸用戶,然后使用cut命令以空格為分隔符取出第一列,再使用sort排序并去除重復的行        …

    Linux干貨 2017-01-18
欧美性久久久久