美文网首页
Linux文件与目录管理

Linux文件与目录管理

作者: Hye_Lau | 来源:发表于2018-05-18 21:49 被阅读0次

    一.目录的相关操作

    常见的处理目录的命令

    • cd:切换目录
    • pwd:显示当前目录
    • mkdir:新建一个新的目录
    • rmdir:删除一个空的目录

    1.cd命令

    命令格式

    cd [/PATH/TO/SOMEDIR]
    

    常用选项

    cd ~USERNAME:切换至指定用户的家目录;
    cd ~:切换回自己的家目录;
      注意:bash中,~表示家目录;
    cd:切换回家目录;
      注意:没有加任何路径,也是代表回到自己家目录的意思;
    cd..:表示切换到目前的上层目录;
    cd -:在上一次所在目录与当前目录之间来回切换;
    

    实例

    [root@localhost ~]# cd ~gentoo
    [root@localhost gentoo]# cd ~
    [root@localhost ~]# cd -
    /users/gentoo
    [root@localhost gentoo]# cd
    [root@localhost ~]# cd /var/spool/mail
    [root@localhost mail]# cd ..
    [root@localhost spool]# cd 
    [root@localhost ~]# 
    

    2.mkdir命令

    命令格式

     mkdir [OPTION]... DIRECTORY...
    

    命令选项

    -p:自动按需创建父目录;
    -v:verbose,显示详细过程;
    -m MODE:直接给定权限;
    注意:路径基名为命令的作用对象;基名之前的路径必须得存在;
    

    实例

    [root@localhost tmp]# ls
    [root@localhost tmp]# 
    [root@localhost tmp]# ls
    [root@localhost tmp]# mkdir test
    [root@localhost tmp]# ls
    test
    [root@localhost tmp]# mkdir test1/test2/test3/test4
    mkdir: 无法创建目录"test1/test2/test3/test4": 没有那个文件或目录
    [root@localhost tmp]# mkdir -p test1/test2/test3/test4
    [root@localhost tmp]# ls
    test  test1
    [root@localhost tmp]# cd test1/test2/test3/test4
    [root@localhost test4]# cd ..
    [root@localhost test3]# cd ..
    [root@localhost test2]# cd ..
    [root@localhost test1]# cd ..
    [root@localhost tmp]# mkdir -pv x1/x2
    mkdir: 已创建目录 "x1"
    mkdir: 已创建目录 "x1/x2"
    [root@localhost tmp]# mkdir -m 743 test2
    [root@localhost tmp]# ls -l
    总用量 16
    drwxr-xr-x. 2 root root 4096 5月  17 04:42 test
    drwxr-xr-x. 3 root root 4096 5月  17 04:43 test1
    drwxr---wx. 2 root root 4096 5月  17 04:46 test2
    drwxr-xr-x. 3 root root 4096 5月  17 04:46 x1
    [root@localhost tmp]# 
    
    

    3.rmdir命令

    命令格式

    rmdir [OPTION]... DIRECTORY...
    

    命令选项

     -p:删除某目录后,如果其父目录为空,则一并删除;
     -v:显示过程;
    

    实例

    [root@localhost tmp]# ls
    test  test1  test2  x1
    [root@localhost tmp]# rmdir test
    [root@localhost tmp]# rmdir -p test1/test2/test3/test4
    [root@localhost tmp]# ls
    test2  x1
    [root@localhost tmp]# ls -l
    总用量 8
    drwxr---wx. 2 root root 4096 5月  17 04:46 test2
    drwxr-xr-x. 2 root root 4096 5月  17 04:50 x1
    

    二.文件与目录管理工具

    常用命令

    • 查看文件与目录:ls
    • 复制、删除与移动:cp,rm,mv

    1.ls命令

    命令格式

    ls [OPTION]... [FILE]...
    
     -a:显示所有文件,包括隐藏文件
     -d:查看目录自身而非其内部的文件列表;
     -l:--long,长格式列表,即显示文件的详细属性信息; 
         (以上三个为常用选项)
     -A:显示除.和..以外的任何文件
     -h:--human-readable,对文件大小单位换算;换算后结果可能会非精确值
    -r :reverse,逆序显示;
     -R:recursive,递归显示;
    

    实例

    [root@localhost ~]# ls
    a_c  anaconda-ks.cfg  b_d        grep.txt  install.log         myfirst.sh   mytest2
    a_d  b_c              functions  inittab   install.log.syslog  mysecond.sh  scripts
    [root@localhost ~]# ls -a
    .    anaconda-ks.cfg  .bashrc  .frist.shell.swp  install.log         mysecond.sh  .viminfo
    ..   .bash_history    b_c      functions         install.log.syslog  mytest2      .Xauthority
    a_c  .bash_logout     b_d      grep.txt          .lesshst            scripts
    a_d  .bash_profile    .cshrc   inittab           myfirst.sh          .tcshrc
    [root@localhost ~]# ls -l
    总用量 84
    drwxr-xr-x. 2 root root  4096 5月   4 03:52 a_c
    drwxr-xr-x. 2 root root  4096 5月   4 03:52 a_d
    -rw-------. 1 root root  1208 7月   2 2017 anaconda-ks.cfg
    drwxr-xr-x. 2 root root  4096 5月   4 03:52 b_c
    drwxr-xr-x. 2 root root  4096 5月   4 03:52 b_d
    -rw-r--r--. 1 root root  1655 5月   7 03:02 functions
    -rw-r--r--. 1 root root    19 5月   3 05:17 grep.txt
    -rwxr-xr-x. 1 root root   884 5月   2 04:27 inittab
    -rw-r--r--. 1 root root 24772 7月   2 2017 install.log
    -rw-r--r--. 1 root root  7690 7月   2 2017 install.log.syslog
    -rw-r--r--. 1 root root    28 5月   3 03:17 myfirst.sh
    -rw-r--r--. 1 root root   111 5月   3 03:27 mysecond.sh
    -rw-r-----. 1 root root     0 5月   2 04:16 mytest2
    drwxr-xr-x. 2 root root  4096 5月  16 23:23 scripts
    [root@localhost ~]# ls -d
    .
    

    2.cp命令--复制文件或目录

    命令格式

    1.单源复制:

    • cp [OPTION]... [-T] SOURCE DEST
      (1) 如果DEST不存在:则事先创建此文件,并复制源文件的数据流至DEST中;
      (2)如果DEST存在:
    • 如果DEST是非目录文件:则覆盖目标文件;
    • 如果DEST是目录文件:则先在DEST目录下创建一个与源文件同名的文件,并复制其数据流;
      2.多源复制:
    • cp [OPTION]... SOURCE... DIRECTORY
    • cp [OPTION]... -t DIRECTORY SOURCE...
      (1) 如果DEST不存在:错误;
      (2)如果DEST存在:
    • 如果DEST是非目录文件:错误;
    • 如果DEST是目录文件:分别复制每个文件至目标目录中,并保持原名;

    常用选项

    -a:相当于-pdr
    -i:若目标文件存在,在覆盖之前提醒用户确认;
    -r:递归复制目录;
         (以上三个为常用选项)
    -p:连同文件的属性一起复制,而非使用默认属性(备份常用);
    -d:复制符号链接文件本身,而非其指向的源文件;
    -f:强制覆盖目标文件;
    

    实例

    [root@localhost ~]# cp /etc/passwd /tmp/passwd
    [root@localhost ~]# cp -i /etc/passwd /tmp/passwd
    cp:是否覆盖"/tmp/passwd"? y
    
    [root@localhost ~]# cp /etc /tmp
    cp: 略过目录"/etc"
    [root@localhost ~]# cp -r /etc /tmp
    [root@localhost ~]# cd /tmp
    [root@localhost tmp]# ls
    etc  passwd  test2  x1
    
    [root@localhost tmp]# cp -a /var/log/wtmp wtmp_a
    -rw-rw-r--. 1 root utmp   49920 5月  16 16:56 wtmp
    -rw-rw-r--.   1 root utmp 50688 5月  17 23:23 wtmp_a
    两个文件的属性一样!
    
    [root@localhost tmp]# cp -p /var/log/wtmp wtmp_test
    -rw-rw-r--.   1 root utmp 49920 5月  16 16:56 wtmp_test
    -rw-rw-r--. 1 root utmp   49920 5月  16 16:56 wtmp
    两个文件的属性一样!
    

    3.mv命令--移动文件或目录,或更名

    mv [OPTION]... [-T] SOURCE DEST
    mv [OPTION]... SOURCE... DIRECTORY
    mv [OPTION]... -t DIRECTORY SOURCE...
    

    常用选项:

     -i:如果目标文件已经存在,询问是否覆盖
     -f:force,如果目标文件已经存在,不会询问直接覆盖
    

    实例

    (1)移动单个文件
    [root@localhost tmp]# cp ~/.bashrc bashrc
    [root@localhost tmp]# mkdir mvtest
    [root@localhost tmp]# mv bashrc mvtest
    [root@localhost tmp]# cd mvtest
    [root@localhost mvtest]# ls
    bashrc
    
    (2)移动多个文件
    [root@localhost tmp]# cp ~/.cshrc cshrc1
    [root@localhost tmp]# cp ~/.cshrc cshrc2
    [root@localhost tmp]# mv cshrc1 cshrc2 mvtest2
    [root@localhost tmp]# cd mvtest2/
    [root@localhost mvtest2]# ls
    bashrc  cshrc1  cshrc2
    
    (3)重命名
    [root@localhost tmp]# mv mvtest mvtest2
    [root@localhost tmp]# ls
    etc  mvtest2  passwd  test2  wtmp_a  wtmp_test  x1
    

    4.rm命令--删除文件或目录

    命令格式

     rm [OPTION]... FILE...
    

    常用选项:

     -i:交互式;在删除前会询问用户是否操作;
     -r:递归删除;
     -f:强制;
    
    删除目录:rm -rf /PATH/TO/DIR
    危险操作:rm -rf /*
    注意:所有不用的文件建议不要直接删除,而是移动至某个专用目录;(模拟回收站)
    

    实例

    [root@localhost tmp]# ls
    etc  mvtest2  passwd  test2  wtmp_a  wtmp_test  x1
    [root@localhost tmp]# rm -f x1
    rm: 无法删除"x1": 是一个目录
    [root@localhost tmp]# rm -rf x1
    [root@localhost tmp]# ls
    etc  mvtest2  passwd  test2  wtmp_a  wtmp_test
    !!删除目录如果不使用-f,会一直询问是否删除其中的子文件
    
    [root@localhost tmp]# rm -i passwd
    rm:是否删除普通文件 "passwd"?y
    [root@localhost tmp]# ls
    etc  mvtest2  test2  wtmp_a  wtmp_test
    

    三.查阅文件内容

    常见命令用途

    • cat:由第一行开始显示文件内容
    • tac:从最后一行开始显示
    • more:一页一页地显示文件内容
    • less:与more类似,但是可以向前翻页
    • head:查看文件的前n行
    • tail:查看文件的后n行
    • od:以二进制的方式读取文件内容

    1.cat命令

    命令格式

    cat [OPTION]... [FILE]...
    

    常用选项

    -n:给显示的文本行编号;
    -E:显示行结束符$;
    

    实例

    [root@localhost ~]# cat /etc/issue
    CentOS release 6.3 (Final)
    Kernel \r on an \m
    
    [root@localhost ~]# cat -n /etc/issue
         1  CentOS release 6.3 (Final)
         2  Kernel \r on an \m
         3  
    [root@localhost ~]# cat -E /etc/issue
    CentOS release 6.3 (Final)$
    Kernel \r on an \m$
    $
    

    2.tac命令

    命令格式

    tac [OPTION]... [FILE]...
    

    实例

    [root@localhost ~]# tac /etc/issue
    
    Kernel \r on an \m
    CentOS release 6.3 (Final)
    

    3.more命令--分屏查看

    命令格式

    more [OPTION]... [FILE]...
    

    常用选项

    -d:显示翻页及退出提示
    

    实例

    [root@localhost ~]# more /etc/man.config
    #
    # Generated automatically from man.conf.in by the
    # configure script.
    ......
    # If no catdir is given, it is assumed to be equal to the mandir
    # (so that this dir has both man1 etc. and cat1 etc. subdirs).
    # This is the traditional Unix setup.
    # Certain versions of the FSSTND recommend putting formatted versions
    --More--(17%)  <==光标会在这里等待用户输入命令
    

    常用按键

    空格键(Space):代表向下翻一页;
    Enter:代表向下滚动一行;
    /字符串:代表在这个显示的内容中,向下查询“字符串”这个关键字;
    :f:立刻显示出文件名以及目前显示的行数;
    q:代表立刻离开more;
    b:代表往回翻页,只对文件有用,对管道无用;
    

    4.less命令--分屏查看

    实例

    [root@localhost ~]# less /etc/man.config
    #
    # Generated automatically from man.conf.in by the
    # configure script.
    #
    # man.conf from man-1.6f
    ......
    # Certain versions of the FSSTND recommend putting formatted versions
     : <==这里等待用户输入命令
    

    常用按键

    空格键:向下翻动一页
    [PageDown]:向下翻动一页;
    [PageUp]:向上翻动一页;
    /字符串:向下查询“字符串”;
    ?字符串:向上查询“字符串”;
    n:重复前一个查询(与/或?有关);
    N:反向重复前一个查询;
    q:离开;
    

    5.head命令

    命令格式

    head [options] FILE
    

    常用选项

    -n#或-#:指定获取前n行;
    

    实例

    [root@localhost ~]# head /etc/man.config
    #
    # Generated automatically from man.conf.in by the
    # configure script.
    #
    # man.conf from man-1.6f
    #
    # For more information about this file, see the man pages man(1)
    # and man.conf(5).
    #
    # This file is read by man to configure the default manpath (also used
    //默认显示前10行
    [root@localhost ~]# head -n 3 /etc/man.config
    #
    # Generated automatically from man.conf.in by the
    # configure script.
    //显示前3行
    [root@localhost ~]# head -1 /etc/man.config
    #
    //显示前1行
    

    6.tail命令

    命令格式

    tail [OPTION]... [FILE]...
    

    命令选项

    -n #或-#:查看文件的后n行;
    -f:查看文件尾部内容结束后不退出,跟随显示新增的行;
    

    实例

    [root@localhost ~]# tail /etc/man.config 
    #
    # Enable/disable makewhatis database cron updates.
    # If MAKEWHATISDBUPDATES variable is uncommented
    # and set to n or N, cron scripts 
    # /etc/cron.daily/makewhatis.cron
    # /etc/cron.weekly/makewhatis.cron
    # will not update makewhatis database.
    # Otherwise the database will be updated.
    # 
    #MAKEWHATISDBUPDATES    n
    //默认显示文件的后n行
    
    [root@localhost ~]# tail -2 /etc/man.config 
    # 
    #MAKEWHATISDBUPDATES    n
    //显示后2行
    
    [root@localhost ~]# tail -f /etc/man.config 
    #
    # Enable/disable makewhatis database cron updates.
    # If MAKEWHATISDBUPDATES variable is uncommented
    # and set to n or N, cron scripts 
    # /etc/cron.daily/makewhatis.cron
    # /etc/cron.weekly/makewhatis.cron
    # will not update makewhatis database.
    # Otherwise the database will be updated.
    # 
    #MAKEWHATISDBUPDATES    n
    ^C
    //输入[Crtl] -c之后才会离开tail命令
    

    7.touch命令--修改文件时间或创建新文件

    命令格式

    touch [OPTIONS]...FILE...
    

    常用选项

    -a:仅修改access time;
    -m:仅修改modify time;
    -c:指定的文件路径不存在时不予创建
    -t :后面跟想要修改的时间,格式为[[CC]YY]MMDDhhmm[.ss] 
    

    touch最常被使用情况

    • 创建一个空的文件;
    • 将某个文件日期修改为目前日期(mtime与atime)

    实例

    //创建一个空文件
    [root@localhost tmp]# touch testtouch
    [root@localhost tmp]# ls
    mvtest2  test2  testtouch  wtmp_a  wtmp_test
    [root@localhost tmp]# ll testtouch 
    -rw-r--r--. 1 root root 0 5月  19 05:26 testtouch
    //
    [root@localhost tmp]# cp -a ~/.cshrc cshrc
    [root@localhost tmp]# ll cshrc    
    -rw-r--r--. 1 root root 100 9月  23 2004 cshrc   <==mtime
    [root@localhost tmp]# ll --time=atime cshrc  
    -rw-r--r--. 1 root root 100 5月  18 05:33 cshrc   <==atime
    [root@localhost tmp]# ll --time=ctime cshrc 
    -rw-r--r--. 1 root root 100 5月  19 05:40 cshrc   <==ctime
    //
    [root@localhost tmp]# touch -t 1805182146 cshrc
    [root@localhost tmp]# ll cshrc
    -rw-r--r--. 1 root root 100 5月  18 21:46 cshrc  <==mtime改变
    [root@localhost tmp]# ll --time=atime cshrc
    -rw-r--r--. 1 root root 100 5月  18 21:46 cshrc  <==atime改变
    [root@localhost tmp]# ll --time=ctime cshrc
    -rw-r--r--. 1 root root 100 5月  19 05:46 cshrc
    
    • 参考书籍:
      《鸟哥的私房菜--基础学习篇》

    相关文章

      网友评论

          本文标题:Linux文件与目录管理

          本文链接:https://www.haomeiwen.com/subject/vlqsdftx.html