zoukankan      html  css  js  c++  java
  • 文件和目录管理

    二、文件和目录管理

    2.1/2.2 系统目录结构

    tree命令用来以树形显示文件的目录结构

    -L 指定查看的目录层级,如 tree -L 3,显示3层目录

    [root@bluequark ~]# which tree
    /usr/bin/which: no tree in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
    [root@bluequark ~]# yum -y install tree   #安装tree命令
    
    [root@bluequark ~]# tree -L 1 /usr/
    /usr/
    ├── bin
    ├── etc
    ├── games
    ├── include
    ├── lib
    ├── lib64
    ├── libexec
    ├── local
    ├── sbin
    ├── share
    ├── src
    └── tmp -> ../var/tmp
    

    Linux的顶层目录结构(参考FHS http://www.pathname.com/fhs/pub/fhs-2.3.pdf)

    /
    ├── bin -> usr/bin
    ├── boot
    ├── dev
    ├── etc
    ├── home
    ├── lib -> usr/lib
    ├── lib64 -> usr/lib64
    ├── media
    ├── mnt
    ├── opt
    ├── proc
    ├── root
    ├── run
    ├── sbin -> usr/sbin
    ├── srv
    ├── swap.add
    ├── sys
    ├── tmp
    ├── usr
    └── var
    

    / 根目录
    /bin 基本命令的二进制文件
    /boot 引导系统加载的静态文件
    /dev 设备文件目录
    /etc 本地计算机系统配置文件
    /etc/X11 对 X windows系统的本地计算机配置
    /home 用户家目录
    /lib 共享库和内核模块
    /lib/modules 可加载内核模块
    /mnt 临时文件系统挂载点
    /opt 附加软件包,可选软件包
    /proc 内核和进程信息的虚拟文件系统
    /root root用户的家目录
    /sbin 超级用户程序目录,包含引导进程所需的工具
    /tmp 临时文件夹
    /usr 第二主文件夹层次
    /usr/bin 大多数用户命令
    /usr/include c程序包含的头文件
    /usr/lib 库文件
    /usr/local 本地文件层次结构
    /usr/sbin 非关键的用于系统管理的二进制文件
    /usr/share/ 各种文档
    /usr/src 源代码文件
    /var 可变数据,如日志,邮件等
    /var/log 日志文件
    /var/spool 假脱机应用数据

    2.3 ls命令

    用来列出目录内容

    常用选项

    -l 以长格式显示文件信息

    [root@bluequark ~]# ls -l anaconda-ks.cfg
    -rw-------. 1 root root 1654 Apr 20 23:21 anaconda-ks.cfg
    

    iWCjQ.png

    -F 附加指示符到输出条目

    [root@bluequark ~]# ls -F /
    bin@   dev/  home/  lib64@  mnt/  proc/  run/   srv/      sys/  usr/
    boot/  etc/  lib@   media/  opt/  root/  sbin@  swap.add  tmp/  var/
    

    -i 查看inode

    [root@bluequark ~]# ls -i
    16797762 anaconda-ks.cfg
    

    -a 显示隐藏文件

    [root@bluequark ~]# ls -a
    .   anaconda-ks.cfg  .bash_logout   .bashrc  .lesshst  .ssh     .viminfo
    ..  .bash_history    .bash_profile  .cshrc   .pki      .tcshrc
    

    -d 只列目录本身

    [root@bluequark ~]# ls /
    bin   dev  home  lib64  mnt  proc  run   srv       sys  usr
    boot  etc  lib   media  opt  root  sbin  swap.add  tmp  var
    [root@bluequark ~]# ls -d /
    /
    

    -t 按时间顺序

    2.4 文件类型

    • 普通文件

      • 普通文件文件
      • 二进制执行文件
    • 目录

    • 伪文件

      • 设备文件
      • 命名管道
      • proc文件
      [root@bluequark ~]# ls -l anaconda-ks.cfg 
      -rw-------. 1 root root 1654 Apr 20 23:21 anaconda-ks.cfg
      
      [root@bluequark ~]# ls -ld /
      dr-xr-xr-x. 17 root root 281 May 12 10:50 /
      
      [root@bluequark ~]# ls -l /dev/log 
      srw-rw-rw- 1 root root 0 May 12 10:50 /dev/log
      
      [root@bluequark ~]# ls -l /lib
      lrwxrwxrwx. 1 root root 7 Apr 20 23:17 /lib -> usr/lib
      
      [root@bluequark ~]# ls -l named.pipe 
      prw-r--r-- 1 root root 0 May 14 00:43 named.pipe
      
      

    2.5 alias命令

    别名命令,用来定义别名

    定制alias环境文件: ~/.bashrc

    临时取消别名功能的方式

    • 以绝对路径方式执行命令
    • 在命令前加上\
    • 用unalias临时取消别名

    常见用法

    [root@bluequark ~]# alias
    alias cls='clear'
    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 which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    
    [root@bluequark ~]# unalias cp
    [root@bluequark ~]# alias 
    alias cls='clear'
    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 which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    

    2.6 相对和绝对路径

    绝对路径:从根目录到文件的路径名,一定以/开始 ,例如/usr/share/doc

    相对路径:从工作目录到文件的路径名,不以/开始 , 例如要由/usr/share/doc进入到/usr/share/man目录下,可以写成"cd ../man",这就是相对路径的写法,相对路径是指相对当前工作目录的路径。

    2.7 cd命令

    改变用户工作目录,如cd命令后不跟任何参数,则直接进入当前用户的家目录

    特殊目录: “.” “..”

    常见用法

    #进入/usr/local/bin目录
    [root@bluequark ~]# cd /usr/local/bin/
    #打印当前所在目录
    [root@bluequark bin]# pwd
    /usr/local/bin
    #代表用户的家目录
    [root@bluequark bin]# cd ~
    [root@bluequark ~]# pwd
    /root
    
    [root@bluequark ~]# cd /usr/local/bin/
    #".."代表当前目录的上一级目录
    [root@bluequark bin]# cd ..
    [root@bluequark local]# pwd
    /usr/local
    #"-"代表上一次所在的目录
    [root@bluequark local]# cd -
    /usr/local/bin
    #"."代表当前目录
    [root@bluequark bin]# cd .
    [root@bluequark bin]# pwd
    /usr/local/bin
    

    2.8 创建和删除目录mkdir/rmdir

    mkdir创建目录

    常用选项

    -p 递归创建目录,目录存在也不会报错。

    #不加-p无法创建成功
    [root@bluequark bin]# mkdir /dir1/dir2/test
    mkdir: cannot create directory ‘/dir1/dir2/test’: No such file or directory
    
    #加-p后创建成功
    [root@bluequark bin]# mkdir -p /dir1/dir2/test
    [root@bluequark bin]# tree /dir1
    /dir1
    └── dir2
        └── test
    
    2 directories, 0 files
    
    #目录存在也不会报错
    [root@bluequark bin]# mkdir -p /dir1/dir2/test
    [root@bluequark bin]# 
    
    

    rmdir只能删除空目录,有很大的局限性,几乎不用。

    2.9 rm命令

    删除文件或目录

    常用选项

    -r 删除目录

    -f 强制删除,不提示。极度危险的命令,使用要相当小心。

    #不加-r选项无法删除目录
    [root@bluequark bin]# rm /dir1/dir2/test/
    rm: cannot remove ‘/dir1/dir2/test/’: Is a directory
    #加上-r删除成功
    [root@bluequark bin]# rm -r !$
    rm -r /dir1/dir2/test/
    rm: remove directory ‘/dir1/dir2/test/’? y
    [root@bluequark bin]# ls -ld /dir1/dir2/test/
    ls: cannot access /dir1/dir2/test/: No such file or directory
    
    [root@bluequark bin]# mkdir -p !$
    mkdir -p /dir1/dir2/test/
    [root@bluequark bin]# ls -ld !$
    ls -ld /dir1/dir2/test/
    drwxr-xr-x 2 root root 6 May 14 01:25 /dir1/dir2/test/
    [root@bluequark bin]# rm -rf /dir1/dir2/test/
    [root@bluequark bin]# ls -ld !$
    ls -ld /dir1/dir2/test/
    ls: cannot access /dir1/dir2/test/: No such file or directory
    

    2.10 环境变量PATH

    环境变量(environment variables)一般是指在操作系统中用来指定操作系统运行环境的一些参数,如:临时文件夹位置和应用程序搜索位置等。

    环境变量PATH定义了一组文件位置,当要求系统运行一个程序而没有告诉它程序所在的完整路径时,系统除了在当前目录下面寻找些程序外,还会到PATH所指定的路径云找。用户通过设置环境变量,来更好的运行进程。

    #显示当前用户的PATH变量内容
    [root@bluequark bin]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    
    #增加PATH路径
    [root@bluequark ~]# ls 
    anaconda-ks.cfg  named.pipe  path_demo
    [root@bluequark ~]# PATH=$PATH:/path_demo
    [root@bluequark ~]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/path_demo
    

    which命令依赖于环境变量PATH的内容

    2.11 cp命令

    复制文件或目录

    常用选项

    -a:该选项通常在拷贝目录时使用。它保留链接、文件属性,并递归地拷贝目录,其作用等于dpR选项的组合。
    -d:拷贝时保留链接。
    -f:删除已经存在的目标文件而不提示。
    -i:和f选项相反,在覆盖目标文件之前将给出提示要求用户确认。回答y时目标文件将被覆盖,是交互式拷贝。
    -p:此时cp除复制源文件的内容外,还将把其修改时间和访问权限也复制到新文件中。
    -r:若给出的源文件是一目录文件,此时cp将递归复制该目录下所有的子目录和文件。此时目标文件必须为一个目录名。
    -l:不作拷贝,只是链接文件。

    [root@bluequark ~]# cp /etc/passwd /root/
    [root@bluequark ~]# ls -l
    total 12
    -rw-------. 1 root root 1654 Apr 20 23:21 anaconda-ks.cfg
    prw-r--r--  1 root root    0 May 14 00:43 named.pipe
    -rw-r--r--  1 root root 1214 May 14 01:45 passwd
    drwxr-xr-x  2 root root    6 May 14 01:33 path_demo
    -rw-------  1 root root 1654 May 14 01:44 pxe_config
    [root@bluequark ~]# 
    
    [root@bluequark ~]# cp pxe_config aaaa
    [root@bluequark ~]# ls 
    aaaa  anaconda-ks.cfg  named.pipe  passwd  path_demo  pxe_config
    
    [root@bluequark ~]# ls
    aaaa  anaconda-ks.cfg  dir1  dir2  named.pipe  path_demo  pxe_config
    [root@bluequark ~]# cp ./dir2/ dir1
    cp: omitting directory ‘./dir2/’
    [root@bluequark ~]# cp -r ./dir2/ dir1/
    
    
    

    2.12 mv命令

    mv命令用来对文件或目录重新命名,或者将文件从一个目录移到另一个目录中

    -b :若需覆盖文件,则覆盖前先行备份。

    -f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;

    -i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!

    -u :若目标文件已经存在,且 source 比较新,才会更新(update)

    -t : --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后。

    #改名
    [root@bluequark ~]# ls
    aaaa  anaconda-ks.cfg  dir1  dir2  named.pipe  path_demo  pxe_config
    [root@bluequark ~]# mv aaaa bbbb
    [root@bluequark ~]# ls
    anaconda-ks.cfg  bbbb  dir1  dir2  named.pipe  path_demo  
    
    #移动目录dir1中的文件到dir2中
    [root@bluequark ~]# mv dir1/* dir2/
    [root@bluequark ~]# tree dir2
    dir2
    ├── dir2
    └── passwd
    
    1 directory, 1 file
    
    
    

    2.13 文档查看cat/more/less/head/tail

    cat用于连接多个文件并且打印到屏幕标准输出或重定向到指定文件中。

    常用选项

    -A 显示所有,包括特殊符号

    -n 打印行号

    -s 压缩空行

    [terry@CS68 tt]$ cat employees 
    Tom Jones       4424    5/12/66         543354
    Mary Adams      5364    11/4/63         28765
    Sally Chang     1654    7/22/54         650000
    Billy Black     1683    9/23/44         336500
    [terry@CS68 tt]$ cat -n employees 
         1  Tom Jones       4424    5/12/66         543354
         2  Mary Adams      5364    11/4/63         28765
         3  Sally Chang     1654    7/22/54         650000
         4  Billy Black     1683    9/23/44         336500
    [terry@CS68 tt]$ cat -A employees 
    Tom Jones^I4424^I5/12/66^I^I543354$
    Mary Adams^I5364^I11/4/63^I^I28765$
    Sally Chang^I1654^I7/22/54^I^I650000$
    Billy Black^I1683^I9/23/44^I^I336500$
    
    

    more

    分页显示文件内容

    more显示完文件内容会自动退出,现在也可以向前和向后翻页

    ctrl+b往上翻,空格往下翻

    less也是分页显示文件内容,但是功能比more强大。不会自动退出,退出命令q

    ctrl+b上翻,ctrl+f或者空格下翻

    head显示文件内容的头部。默认显示前10行

    -n 指定显示头几行

    [root@bluequark ~]# head anaconda-ks.cfg 
    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    graphical
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda
    
    [root@bluequark ~]# head -n 2 anaconda-ks.cfg 
    #version=DEVEL
    # System authorization information
    

    tail 显示文件内容的尾部。默认尾部10行

    -n显示尾部n行,默认10行

    -f 实时监控文件尾部内容变化

    [root@bluequark ~]# tail anaconda-ks.cfg 
    
    %addon com_redhat_kdump --enable --reserve-mb='auto'
    
    %end
    
    %anaconda
    pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    %end
    
    [root@bluequark ~]# tail -n 2 anaconda-ks.cfg 
    pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    %end
    

    2.14 文件和目录权限chmod

    改变文件或目录权限

    [root@bluequark ~]# ls -l anaconda-ks.cfg 
    - rw- --- ---.       1 root root 1654 Apr 20 23:21 anaconda-ks.cfg
    

    目录或文件权限位为9位,三位为一组,共3组

    从左至右

    • 1-3位代表文件所有者的权限(U)
    • 4-6位代表文件所属组的权限(G)
    • 7-9位其他用户的权限(O)

    r:表示可读(4)

    w:表示可写(2,文件是否能删除取决于上层目录的权限)

    x:(表示可执行,对于目录来说,x代表可以进入该目录)

    Linux系统默认新建的文件权限为666,没有执行权限

    Linux系统默认新建的目录权限为777

    [root@bluequark ~]# ls -l pxe_config 
    -rw------- 1 root root 1654 May 14 01:44 pxe_config
    [root@bluequark ~]# chmod 777 pxe_config 
    [root@bluequark ~]# ls -l !$
    ls -l pxe_config
    -rwxrwxrwx 1 root root 1654 May 14 01:44 pxe_config
    
    #-R递归修改目录文件权限
    [root@bluequark ~]# ls -lR dir2
    dir2:
    total 4
    drwxr-xr-x 2 root root    6 May 14 01:50 dir2
    -rw-r--r-- 1 root root 1214 May 14 01:45 passwd
    
    dir2/dir2:
    total 0
    [root@bluequark ~]# chmod -R 777 dir2/
    [root@bluequark ~]# ls -lR dir2
    dir2:
    total 4
    drwxrwxrwx 2 root root    6 May 14 01:50 dir2
    -rwxrwxrwx 1 root root 1214 May 14 01:45 passwd
    
    dir2/dir2:
    total 0
    [root@bluequark ~]# 
    
    [root@bluequark ~]# chmod a-x dir2/
    [root@bluequark ~]# ls -ld dir2/
    drw-rw-rw- 3 root root 32 May 14 01:53 dir2/
    
    [root@bluequark ~]# chmod a+x dir2
    [root@bluequark ~]# ls -ld dir2
    drwxrwxrwx 3 root root 32 May 14 01:53 dir2
    

    2.15 更改所有者(chown)和所属组(chgrp)

    chown命令可以更改文件的所有者

    chown [-R] 账户名(root) 文件名 更改文件所有者
    chown [-R] 账户名(root):组名(root,中间不加空格) 文件名
    -R 选项只适合于目录,作用是递归更改,不仅更改当前目录,连目录里的目录或文件也全部更改

    [root@bluequark ~]# ls -ld dir1
    drwxr-xr-x 2 root root 6 May 14 01:53 dir1
    [root@bluequark ~]# chown hjm dir1
    [root@bluequark ~]# ls -ld dir1
    drwxr-xr-x 2 hjm root 6 May 14 01:53 dir1
    
    [root@bluequark ~]# chown root:hjm dir1
    [root@bluequark ~]# ls -ld dir1
    drwxr-xr-x 2 root hjm 6 May 14 01:53 dir1
    
    
    [root@bluequark ~]# ls -lR dir2
    dir2:
    total 4
    drwxrwxrwx 2 root root    6 May 14 01:50 dir2
    -rwxrwxrwx 1 root root 1214 May 14 01:45 passwd
    dir2/dir2:
    total 0
    [root@bluequark ~]# chown -R hjm:hjm dir2/
    [root@bluequark ~]# ls -lR dir2
    dir2:
    total 4
    drwxrwxrwx 2 hjm hjm    6 May 14 01:50 dir2
    -rwxrwxrwx 1 hjm hjm 1214 May 14 01:45 passwd
    
    dir2/dir2:
    total 0
    

    2.16 umask

    查看umask值,

    umask值在/etc/profile中定义

    系统的umask值设置的已经很合理,没有特殊需求,不要去修改umask值。

    [root@bluequark ~]# umask
    0022
    
  • 相关阅读:
    memwatch使用简化
    memwatch检测内存泄露
    mtrace检测内存泄露
    2.14 环境变量及参数
    设计模式[索引]
    二叉树遍历
    AppCan 双击返回按钮退出应用
    MySQL语句相关经验总结
    mysql连接失败或出现“Too many connections”错误
    让IE的Button自适应文字宽度兼容
  • 原文地址:https://www.cnblogs.com/minn/p/9034290.html
Copyright © 2011-2022 走看看