10.2-linux文件与目录管理

 

1.1-目录的相关操作

1. rm
       Remove (unlink) the FILE(s).

       -f, --force    #强制删除
              ignore nonexistent files, never prompt

       -i     prompt before every removal    

       -I     prompt  once  before  removing more than three files, or when
              removing recursively.  Less intrusive than  -i,  while  still
              giving protection against most mistakes
       -r, -R, --recursive    连同目录和内容一块删除
              remove directories and their contents recursively


       By default, rm does not remove directories.  Use the --recursive (-r
       or  -R)  option to remove each listed directory, too, along with all
       of its contents.
            #删除名字带有 -
       To remove a file whose name starts with a ‘-’, for  example  ‘-foo’,
       use one of these commands:

              rm -- -foo

              rm ./-foo

       Note  that if you use rm to remove a file, it is usually possible to
       recover the contents of that file.  If you want more assurance  that
       the contents are truly unrecoverable, consider using shred.
------------
2. pwd
 Print the full filename of the current working directory.

       -L, --logical       #显示出当前路径,包含连接(link)路径
              use PWD from environment, even if it contains symlinks

       -P, --physical    #显示出当前路径,而非使用连接(link)路径
              avoid all symlinks
------------
3. mkdir
SYNOPSIS
       mkdir [OPTION]... DIRECTORY...
 Create the DIRECTORY(ies), if they do not already exist.
       -m, --mode=MODE    #设置配置文件的权限
              set file mode (as in chmod), not a=rwx - umask
       -p, --parents
              no error if existing, make parent directories as needed

------------
4. rmdir 
SYNOPSIS
       rmdir [OPTION]... DIRECTORY...

DESCRIPTION
       Remove the DIRECTORY(ies), if they are empty.

       --ignore-fail-on-non-empty

              ignore each failure that is solely because a directory

              is non-empty

       -p, --parents    #连同上层空目录一起删除
              remove DIRECTORY and its ancestors; e.g., ‘rmdir -p a/b/c’ is
              similar to ‘rmdir a/b/c a/b a’

       -v, --verbose
              output a diagnostic for every directory processed

--->rm -r test 将目录下的东西也删除
View Code

 1.2.1查看文件目录

ls [选项]... [文件]...
列出 FILE 的信息(默认为当前目录)。
  -a, --all            不隐藏任何以. 开始的项目
  -d, --directory        当遇到目录时列出目录本身而非目录内的文件
  -f                不进行排序,-aU 选项生效,-lst 选项失效
  -F, --classify        加上文件类型的指示符号(*/=@| 其中一个)
      --format=关键字        交错-x,逗号分隔-m,水平-x,长-l,
                单栏-1,详细-l,垂直-C
      --full-time        即-l --time-style=full-iso
   -h, --human-readable        与-l 一起,以易于阅读的格式输出文件大小
                (例如 1K 234M 2G)
        --si            同上面类似,但是使用1000 为基底而非1024
  -i, --inode            显示每个文件的inode 号
  -l                使用较长格式列出信息
  -n, --numeric-uid-gid        类似 -l,但列出UID 及GID 号
  -r, --reverse            排序时保留顺序
  -R, --recursive        递归显示子目录
  -S                根据文件大小排序
  -t                根据修改时间排序
-----------
ll ----> ls -l 一样
View Code

1.2.2复制,删除,与移动:cp ,rm, mv

1. cp
用法:cp [选项]... [-T] 源文件 目标文件
 或:cp [选项]... 源文件... 目录
 或:cp [选项]... -t 目录 源文件...
将源文件复制至目标文件,或将多个源文件复制至目标目录。

长选项必须使用的参数对于短选项时也是必需使用的。
* -a, --archive        相当于-pdr    等于-dR --preserve=all
      --backup[=CONTROL        为每个已存在的目标文件创建备份
  -d    复制连接文件属性而非文件本身,等于--no-dereference --preserve=links
  -f, --force            如果目标文件无法打开则将其移除并重试(当 -n 选项
                    存在时则不需再选此项)
*-i, --interactive        覆盖前询问(使前面的 -n 选项失效)
  -l, --link        进行硬连接,文件而不复制
*-p        连同文件的属性一起复制过去,等于--preserve=模式,所有权,时间戳
      --preserve[=属性列表    保持指定的属性(默认:模式,所有权,时间戳),如果
                    可能保持附加属性:环境、链接、xattr 等
  -s, --symbolic-link        只创建符号链接而不复制文件
  -u, --update               目标文件比源文件旧才更新目标文件
-------
使用范例一:
[root@wen tmp]# cp ~/.bashrc /tmp/bashrc
[root@wen tmp]# cp -i ~/.bashrc /tmp/bashrc
cp:是否覆盖"/tmp/bashrc"? 
[root@wen tmp]# cd /tmp
[root@wen tmp]# cp /var/log/wtmp .
[root@wen tmp]# ls -l /var/log/wtmp wtmp
-rw-rw-r--. 1 root utmp 208128 10月  2 00:05 /var/log/wtmp
-rw-r--r--  1 root root 207744 10月  3 2017 wtmp
[root@wen tmp]# cp -a /var/log/wtmp wtmp_2
[root@wen tmp]# ls -l /var/log/wtmp wtmp_2
-rw-rw-r--. 1 root utmp 208128 10月  2 00:05 /var/log/wtmp
-rw-rw-r--. 1 root utmp 207744 10月  3 2017 wtmp_2

范例二:
[root@wen tmp]# cp /etc/ /tmp
cp: 略过目录"/etc/"
[root@wen tmp]# cp -r /etc/ /tmp

[root@wen tmp]# ll bashrc
-rw-r--r-- 1 root root 176 10月  3 2017 bashrc
[root@wen tmp]# cp -s bashrc bashrc_slink    
[root@wen tmp]# cp -l bashrc bashrc_hlink
[root@wen tmp]# ll bashrc*
-rw-r--r-- 2 root root 176 10月  3 2017 bashrc
-rw-r--r-- 2 root root 176 10月  3 2017 bashrc_hlink  #硬连接
lrwxrwxrwx 1 root root   6 10月  2 00:43 bashrc_slink -> bashrc

[root@wen tmp]# cp -u ~/.bashrc /tmp/bashrc    #常用于备份

[root@wen tmp]# cp bashrc_slink bashrc_slink_1
[root@wen tmp]# cp -d bashrc_slink bashrc_slink_2  #复制连接文件属性而非本身
[root@wen tmp]# ll bashrc bashrc_slink*
-rw-r--r-- 2 root root 176 10月  3 2017 bashrc
lrwxrwxrwx 1 root root   6 10月  2 00:43 bashrc_slink -> bashrc
-rw-r--r-- 1 root root 176 10月  2 00:50 bashrc_slink_1
lrwxrwxrwx 1 root root   6 10月  2 00:50 bashrc_slink_2 -> bashrc
cp

 

rm
[root@wen ~]# cd /tmp
[root@wen tmp]# ls
bashrc        bashrc_slink    bashrc_slink_2  oldboy  wtmp_2
bashrc_hlink  bashrc_slink_1  etc             wtmp*
rm:是否删除普通文件 "bashrc"?y
已删除"bashrc"
rm:是否删除普通文件 "bashrc_hlink"?y
已删除"bashrc_hlink"
rm:是否删除符号链接 "bashrc_slink"?y
已删除"bashrc_slink"
rm:是否删除普通文件 "bashrc_slink_1"?y
已删除"bashrc_slink_1"
rm:是否删除符号链接 "bashrc_slink_2"?y
已删除"bashrc_slink_2"
[root@wen tmp]# ls
etc  oldboy  wtmp  wtmp_2
[root@wen tmp]# rm -fr /tmp/etc
[root@wen tmp]# ls
oldboy  wtmp  wtmp_2
[root@wen tmp]# \rm -r /tmp/wtmp*  #没有询问删除
[root@wen tmp]# ls
oldboy
[root@wen tmp]# touch ./-aaa-
[root@wen tmp]# ls
-aaa-  oldboy
[root@wen tmp]# rm ./-aaa-        #或者 rm -- -aaa-
rm:是否删除普通空文件 "./-aaa-"?y
[root@wen tmp]# ls
oldboy
rm
用法:mv [选项]... [-T] 源文件 目标文件
 或:mv [选项]... 源文件... 目录
 或:mv [选项]... -t 目录 源文件...
将源文件重命名为目标文件,或将源文件移动至指定目录。
 -f, --force                  覆盖前不询问
 -i, --interactive            覆盖前询问
 -u, --update            只在源文件文件比目标文件新,或目标文件
                不存在时才进行移动
[root@wen tmp]# cp ~/.bashrc bashrc
[root@wen tmp]# mkdir mvtest
[root@wen tmp]# mv bashrc mvtest 
[root@wen tmp]# mv mvtest mvtest2   #重命名
[root@wen tmp]# ls
mvtest2  oldboy
mv

1.3 文件内容查阅

 

posted @ 2017-10-02 18:50  元贞  阅读(152)  评论(0编辑  收藏  举报