Linux命令实践(四)

1、Linux上的文件管理类命令都有哪些,其常用的使用方法及相关示例演示。

  • 文件或目录的新建

    touch :将每个文件的访问时间和修改时间修改为当前时间。若文件不存在将会创建为空文件,除非使用-c或-h选项。

      用法:touch  [选项]... 文件...

      选项:

        -a            只更改访问时间

        -c,  --no-create    不创建任何文件 

        -d,  --dete=字符串   使用指定字符串表示时间而非当前时间 

        -f           (忽略)

        -h,  --no-dereference    会影响符号连接本身,而非符号连接所指示的目的地

                    (当前系统支持更改符号连接的所有者时,此选项才有用)

        -m           只更改修改时间

        -r,  --reference=文件  使用指定文件的时间属性而非当前时间

        -t  STAMP       使用[[CC]YY]MMDDhhmm[.ss]  格式的时间而非当前时间

        --time=WORD     使用WORD指定的时间:access、atime、use都等价于-a选项的效果

                    而modify、mtime等价于-m选项的效果

          --help       显示此帮助信息并退出

          --version       显示版本信息并退出

      请注意,-d和-t选项可接受不同的日期/时间格式。

常用演示:

[root@centos6_test tmp]#touch xxx
[root@centos6_test tmp]#stat xxx
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:28:38.382002766 +0800
Modify: 2019-10-10 13:28:38.382002766 +0800
Change: 2019-10-10 13:28:38.382002766 +0800
[root@centos6_test tmp]#touch -a xxx
[root@centos6_test tmp]#stat xxx
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:28:57.982999292 +0800
Modify: 2019-10-10 13:28:38.382002766 +0800
Change: 2019-10-10 13:28:57.982999292 +0800
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 13:28 xxx
[root@centos6_test tmp]#touch -c aaa
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 13:28 xxx
[root@centos6_test tmp]#date 
2019年 10月 10日 星期四 13:29:55 CST
[root@centos6_test tmp]#touch -d '2019-11-11 11:30:10' abc
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 11月 11 2019 abc
-rw-r--r--. 1 root root 0 10月 10 13:28 xxx
[root@centos6_test tmp]#pwd
/tmp
[root@centos6_test tmp]#cd /root
[root@centos6_test ~]#ls
scripts
[root@centos6_test ~]#touch test.txt
[root@centos6_test ~]#cd /tmp
[root@centos6_test tmp]#ln -s ../root/test.txt test.txt
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root  0 11月 11 2019 abc
lrwxrwxrwx. 1 root root 16 10月 10 13:38 test.txt -> ../root/test.txt
-rw-r--r--. 1 root root  0 10月 10 13:28 xxx
[root@centos6_test tmp]#stat /root/test.txt 
  File: "/root/test.txt"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 793356      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:38:23.595005397 +0800
Modify: 2019-10-10 13:38:23.595005397 +0800
Change: 2019-10-10 13:38:23.595005397 +0800
[root@centos6_test tmp]#stat test.txt 
  File: "test.txt" -> "../root/test.txt"
  Size: 16        	Blocks: 0          IO Block: 4096   符号链接
Device: fd00h/64768d	Inode: 1316027     Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:38:52.785002202 +0800
Modify: 2019-10-10 13:38:50.867001705 +0800
Change: 2019-10-10 13:38:50.867001705 +0800
[root@centos6_test tmp]#touch -h test.txt 
[root@centos6_test tmp]#stat test.txt 
  File: "test.txt" -> "../root/test.txt"
  Size: 16        	Blocks: 0          IO Block: 4096   符号链接
Device: fd00h/64768d	Inode: 1316027     Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:40:16.896011892 +0800
Modify: 2019-10-10 13:40:13.286992548 +0800
Change: 2019-10-10 13:40:13.286992548 +0800
[root@centos6_test tmp]#stat /root/test.txt 
  File: "/root/test.txt"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 793356      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:38:23.595005397 +0800
Modify: 2019-10-10 13:38:23.595005397 +0800
Change: 2019-10-10 13:38:23.595005397 +0800
[root@centos6_test tmp]#
[root@centos6_test tmp]#ll /root/test.txt 
-rw-r--r--. 1 root root 0 10月 10 13:38 /root/test.txt
[root@centos6_test tmp]#date
2019年 10月 10日 星期四 13:53:13 CST
[root@centos6_test tmp]#touch -r /root/test.txt  ccc
[root@centos6_test tmp]#ll ccc 
-rw-r--r--. 1 root root 0 10月 10 13:38 ccc
[root@centos6_test tmp]#stat ccc
  File: "ccc"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316034     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:38:23.595005397 +0800
Modify: 2019-10-10 13:38:23.595005397 +0800
Change: 2019-10-10 13:53:31.925000824 +0800
[root@centos6_test tmp]#stat /root/test.txt 
  File: "/root/test.txt"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 793356      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:38:23.595005397 +0800
Modify: 2019-10-10 13:38:23.595005397 +0800
Change: 2019-10-10 13:38:23.595005397 +0800  
[root@centos6_test tmp]#stat xxx
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:28:57.982999292 +0800
Modify: 2019-10-10 13:28:38.382002766 +0800
Change: 2019-10-10 13:28:57.982999292 +0800
[root@centos6_test tmp]#touch -m xxx 
[root@centos6_test tmp]#stat xxx 
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:28:57.982999292 +0800
Modify: 2019-10-10 13:47:39.699998443 +0800
Change: 2019-10-10 13:47:39.699998443 +0800
[root@centos6_test tmp]#touch -t '201912121034.21' abc
[root@centos6_test tmp]#stat abc
  File: "abc"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316033     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-12 10:34:21.000000000 +0800
Modify: 2019-12-12 10:34:21.000000000 +0800
Change: 2019-10-10 13:48:39.258004301 +0800 
[root@centos6_test tmp]#date
2019年 10月 10日 星期四 14:02:27 CST
[root@centos6_test tmp]#stat abc
  File: "abc"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316033     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 14:00:52.147000137 +0800
Modify: 2019-12-12 10:34:21.000000000 +0800
Change: 2019-10-10 14:00:52.147000137 +0800
[root@centos6_test tmp]#touch --time=access abc
[root@centos6_test tmp]#stat abc
  File: "abc"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316033     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 14:02:49.266000652 +0800
Modify: 2019-12-12 10:34:21.000000000 +0800
Change: 2019-10-10 14:02:49.266000652 +0800
[root@centos6_test tmp]#touch --time=use abc
[root@centos6_test tmp]#stat abc
  File: "abc"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316033     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 14:03:14.579006906 +0800
Modify: 2019-12-12 10:34:21.000000000 +0800
Change: 2019-10-10 14:03:14.579006906 +0800
[root@centos6_test tmp]#stat xxx
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 14:01:12.818003178 +0800
Modify: 2019-10-10 13:59:38.505997486 +0800
Change: 2019-10-10 14:01:12.818003178 +0800
[root@centos6_test tmp]#touch --time=mtim xxx
[root@centos6_test tmp]#stat xxx
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 14:01:12.818003178 +0800
Modify: 2019-10-10 14:03:57.191991478 +0800
Change: 2019-10-10 14:03:57.191991478 +0800

    mkdir:若指定目录不存在则创建目录。

      用法:mkdir  [选项]...  目录名...

      选项:

        -m, --mode=模式    设置权限模式(类似chmod),而不是rwxrwxrwx减umask

        -p, --parents      需要时创建目标目录的上层目录,但即使这些目录已存在也不当错误处理

        -v, --verbose      每次创建目录都显示其过程信息

        --help          显示此帮助信息并退出

        --version        显示版本信息并退出

      请注意,长选项必须使用的参数对于短选项时也是必须使用的。

常用演示:

[root@centos6_test tmp]#mkdir test
[root@centos6_test tmp]#ll
总用量 4
drwxr-xr-x. 2 root root 4096 10月 10 13:22 test
[root@centos6_test tmp]#mkdir -m 700 abc
[root@centos6_test tmp]#ll
总用量 8
drwx------. 2 root root 4096 10月 10 13:22 abc
drwxr-xr-x. 2 root root 4096 10月 10 13:22 test
[root@centos6_test tmp]#mkdir -p xxx/aaa/bbb/ccc
[root@centos6_test tmp]#tree
.
├── abc
├── test
└── xxx
    └── aaa
        └── bbb
            └── ccc

6 directories, 0 files
[root@centos6_test tmp]#mkdir -v ddd
mkdir: 已创建目录 "ddd"
[root@centos6_test tmp]#mkdir -pv ddd/aaa/ccc/eee
mkdir: 已创建目录 "ddd/aaa"
mkdir: 已创建目录 "ddd/aaa/ccc"
mkdir: 已创建目录 "ddd/aaa/ccc/eee"
[root@centos6_test tmp]#

    重定向 >    >> 

常用演示:

[root@centos6_test tmp]#ls 
[root@centos6_test tmp]#> xxxj.sh
[root@centos6_test tmp]#ls
xxxj.sh
[root@centos6_test tmp]#echo xxx >abc.txt
[root@centos6_test tmp]#ls
abc.txt  xxxj.sh
[root@centos6_test tmp]#>> aa
[root@centos6_test tmp]#echo bbb >> bb
[root@centos6_test tmp]#ls
aa  abc.txt  bb  xxxj.sh
[root@centos6_test tmp]#cat >> aabbcc <<EOF
> 12424
> EOF
[root@centos6_test tmp]#ls
aa  aabbcc  abc.txt  bb  xxxj.sh
[root@centos6_test tmp]#ls
aa  aabbcc  abc.txt  bb  xxxj.sh
[root@centos6_test tmp]#cat > 12aa <<EOF
> sdlfjasjdfoaj
> EOF
[root@centos6_test tmp]#ls
12aa  aa  aabbcc  abc.txt  bb  xxxj.sh

说明:echo "字符串" > 文件  或者 echo "字符串" >> 文件 是把字符串重定向到文件里,如果文件不存在则创建,然后把字符串写进文件里,两者的区别是'>'是输出重定向,它会清空原有文件内容,然后再把字符串内容写进文件,而'>>'是追加输出重定向,它不会清空原有的文件内容,只在原有文件的内容最后把字符串内容写进文件。  > 文件 表示清空文件,如果文件存在则清空,如果不存在则创建。>> 文件 表示把空字符追加到文件,如果文件存在,则不改变文件内容,如果文件不存在,则创建。当然vim也是可以创建新文件的,如果文件存在则编辑文件,不存在则新建空文件,然后编辑文件,但要注意vim不是说打开一个不存在的文件就创建了一个空文件,我们要保存才会创建文件,不保存退出则不会创建新文件。

  • 文件或目录的删除

    rmdir:删除指定的空目录。

      用法:rmdir  [选项]...  目录...

      选项:

        --ignore-fail-on-non-empty       忽略仅由目录非空产生的所有错误

        -p, --parents              删除指定目录及其上级目录,例如"rmdir -p a/b/c" 与"rmdir a/b/c a/b a"基本相同

        -v, --verbose              输出处理的目录详情

        --help                  显示此帮助信息并退出

        --version                  显示版本信息并退出

常用演示:

[root@centos6_test tmp]#tree
.
├── aa
│   └── bb
│       └── cc
│           └── dd
├── abc
├── ccc
├── test.txt -> ../root/test.txt
└── xxx

4 directories, 4 files
[root@centos6_test tmp]#rmdir --ignore-fail-on-non-empty aa
[root@centos6_test tmp]#tree
.
├── aa
│   └── bb
│       └── cc
│           └── dd
├── abc
├── ccc
├── test.txt -> ../root/test.txt
└── xxx

4 directories, 4 files
[root@centos6_test tmp]#rmdir -p aa
rmdir: 删除 "aa" 失败: 目录非空
[root@centos6_test tmp]#rmdir -p aa/bb/cc/dd/
[root@centos6_test tmp]#tree
.
├── abc
├── ccc
├── test.txt -> ../root/test.txt
└── xxx

0 directories, 4 files
[root@centos6_test tmp]#

    rm:删除(unlink)文件

      用法:rm  [选项]...  文件...

      选项:

        -f,--force          强制删除。忽略不存在的文件,不提示确认

        -i              在删除前需要确认

        -I(大写i)           在删除超过三个文件或者递归删除前要求确认。此选项比-i提示内容更少,但同样可以阻止大多数错误发生

        --interactive[=WHEN]     根据指定WHEN进行确认提示:never,once(-I),或者always(-i)。如果此选项不加WHEN则总是提示

        --one-file-system      递归删除一个层级时,跳过所有不符合命令行参数的文件

        --no-preserve-roo       不特殊对待"/"

        --preserve-root       不允许删除"/"(默认)

        -r, -R, --recursive      递归删除目录及其内容

        -v,--verbose          详细显示进行的步骤

        --help             显示此帮助信息并退出

        --version           显示版本信息并退出

      默认时,rm不会删除目录。使用--recursive(-r或-R)选项可删除每个给定的目录,以及其下所有的内容。要删除第一个字符为"-"的文件(例如"-foo"),请使用以下方法之一:

        rm -- -foo

        rm ./-foo  

常用演示:

[root@centos6_test tmp]#ll
总用量 4
drwxr-xr-x. 3 root root 4096 10月 10 14:54 aa
-rw-r--r--. 1 root root    0 10月 10 14:54 ab
-rw-r--r--. 1 root root    0 10月 10 14:54 ac
-rw-r--r--. 1 root root    0 10月 10 14:54 ba
-rw-r--r--. 1 root root    0 10月 10 14:54 bb
-rw-r--r--. 1 root root    0 10月 10 14:54 bc
-rw-r--r--. 1 root root    0 10月 10 14:54 ca
-rw-r--r--. 1 root root    0 10月 10 14:54 cb
-rw-r--r--. 1 root root    0 10月 10 13:38 ccc
lrwxrwxrwx. 1 root root   16 10月 10 13:40 test.txt -> ../root/test.txt
-rw-r--r--. 1 root root    0 10月 10 14:03 xxx
[root@centos6_test tmp]#rm -f ab ac ddddd
[root@centos6_test tmp]#ll
总用量 4
drwxr-xr-x. 3 root root 4096 10月 10 14:54 aa
-rw-r--r--. 1 root root    0 10月 10 14:54 ba
-rw-r--r--. 1 root root    0 10月 10 14:54 bb
-rw-r--r--. 1 root root    0 10月 10 14:54 bc
-rw-r--r--. 1 root root    0 10月 10 14:54 ca
-rw-r--r--. 1 root root    0 10月 10 14:54 cb
-rw-r--r--. 1 root root    0 10月 10 13:38 ccc
lrwxrwxrwx. 1 root root   16 10月 10 13:40 test.txt -> ../root/test.txt
-rw-r--r--. 1 root root    0 10月 10 14:03 xxx
[root@centos6_test tmp]#rm -i bb
rm:是否删除普通空文件 "bb"?y
[root@centos6_test tmp]#ll
总用量 4
drwxr-xr-x. 3 root root 4096 10月 10 14:54 aa
-rw-r--r--. 1 root root    0 10月 10 14:54 ba
-rw-r--r--. 1 root root    0 10月 10 14:54 bc
-rw-r--r--. 1 root root    0 10月 10 14:54 ca
-rw-r--r--. 1 root root    0 10月 10 14:54 cb
-rw-r--r--. 1 root root    0 10月 10 13:38 ccc
lrwxrwxrwx. 1 root root   16 10月 10 13:40 test.txt -> ../root/test.txt
-rw-r--r--. 1 root root    0 10月 10 14:03 xxx
[root@centos6_test tmp]#rm -I ba bc ca 
[root@centos6_test tmp]#ll
总用量 4
drwxr-xr-x. 3 root root 4096 10月 10 14:54 aa
-rw-r--r--. 1 root root    0 10月 10 14:54 cb
-rw-r--r--. 1 root root    0 10月 10 13:38 ccc
lrwxrwxrwx. 1 root root   16 10月 10 13:40 test.txt -> ../root/test.txt
-rw-r--r--. 1 root root    0 10月 10 14:03 xxx
[root@centos6_test tmp]#rm -I cb ccc  xxx  test.txt 
rm:删除所有参数?y
[root@centos6_test tmp]#ll
总用量 4
drwxr-xr-x. 3 root root 4096 10月 10 14:54 aa
[root@centos6_test tmp]#tree 
.
└── aa
    └── bb
        └── cc
            └── dd

4 directories, 0 files
[root@centos6_test tmp]#rm -r aa
rm:是否进入目录"aa"? n
[root@centos6_test tmp]#rm -rf aa
[root@centos6_test tmp]#tree
.

0 directories, 0 files
[root@centos6_test tmp]#touch {a..d}{a..d}
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 14:57 aa
-rw-r--r--. 1 root root 0 10月 10 14:57 ab
-rw-r--r--. 1 root root 0 10月 10 14:57 ac
-rw-r--r--. 1 root root 0 10月 10 14:57 ad
-rw-r--r--. 1 root root 0 10月 10 14:57 ba
-rw-r--r--. 1 root root 0 10月 10 14:57 bb
-rw-r--r--. 1 root root 0 10月 10 14:57 bc
-rw-r--r--. 1 root root 0 10月 10 14:57 bd
-rw-r--r--. 1 root root 0 10月 10 14:57 ca
-rw-r--r--. 1 root root 0 10月 10 14:57 cb
-rw-r--r--. 1 root root 0 10月 10 14:57 cc
-rw-r--r--. 1 root root 0 10月 10 14:57 cd
-rw-r--r--. 1 root root 0 10月 10 14:57 da
-rw-r--r--. 1 root root 0 10月 10 14:57 db
-rw-r--r--. 1 root root 0 10月 10 14:57 dc
-rw-r--r--. 1 root root 0 10月 10 14:57 dd
[root@centos6_test tmp]#rm --interactive=once *
rm:删除所有参数?y
[root@centos6_test tmp]#ll
总用量 0
[root@centos6_test tmp]#touch {a..d}
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 14:58 a
-rw-r--r--. 1 root root 0 10月 10 14:58 b
-rw-r--r--. 1 root root 0 10月 10 14:58 c
-rw-r--r--. 1 root root 0 10月 10 14:58 d
[root@centos6_test tmp]#rm --interactive=never *
[root@centos6_test tmp]#ll
总用量 0
[root@centos6_test tmp]#touch {a..f}
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 15:00 a
-rw-r--r--. 1 root root 0 10月 10 15:00 b
-rw-r--r--. 1 root root 0 10月 10 15:00 c
-rw-r--r--. 1 root root 0 10月 10 15:00 d
-rw-r--r--. 1 root root 0 10月 10 15:00 e
-rw-r--r--. 1 root root 0 10月 10 15:00 f
[root@centos6_test tmp]#rm -v *
rm:是否删除普通空文件 "a"?y
已删除"a"
rm:是否删除普通空文件 "b"?y
已删除"b"
rm:是否删除普通空文件 "c"?y
已删除"c"
rm:是否删除普通空文件 "d"?
rm:是否删除普通空文件 "e"?
rm:是否删除普通空文件 "f"?
[root@centos6_test tmp]#
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 15:00 d
-rw-r--r--. 1 root root 0 10月 10 15:00 e
-rw-r--r--. 1 root root 0 10月 10 15:00 f
[root@centos6_test tmp]#rm -fv *
已删除"d"
已删除"e"
已删除"f"
[root@centos6_test tmp]#touch -- -abc
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 15:03 -abc
[root@centos6_test tmp]#rm -f -abc 
rm:无效选项 -- a
尝试使用"rm ./-abc"删除文件"-abc"。
请尝试执行"rm --help"来获取更多信息。
[root@centos6_test tmp]#rm -- -abc 
rm:是否删除普通空文件 "-abc"?y
[root@centos6_test tmp]#ll
总用量 0
[root@centos6_test tmp]#touch -- -abc
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 15:03 -abc
[root@centos6_test tmp]#rm -f ./-abc 
[root@centos6_test tmp]#ll
总用量 0
[root@centos6_test tmp]#
  • 文件或目录的修改

    rename:文件重命名

      用法:rename  要修改的字符  修改成的字符  文件...

      选项:无

常用演示:       

[root@centos6_test tmp]#touch {a..d}.txt
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 15:09 a.txt
-rw-r--r--. 1 root root 0 10月 10 15:09 b.txt
-rw-r--r--. 1 root root 0 10月 10 15:09 c.txt
-rw-r--r--. 1 root root 0 10月 10 15:09 d.txt
[root@centos6_test tmp]#rename .txt .sh *.txt
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 15:09 a.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 b.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 c.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 d.sh
[root@centos6_test tmp]#rename a xx a.sh
[root@centos6_test tmp]#ll
总用量 0
-rw-r--r--. 1 root root 0 10月 10 15:09 b.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 c.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 d.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 xx.sh
[root@centos6_test tmp]#
[root@centos6_test tmp]#mkdir testdir
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 testdir
[root@centos6_test tmp]#rename test abcd testdir
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
[root@centos6_test tmp]#

说明:rename支持批量改文件名,后面可以支持通配符

  • 文件或目录的移动

    mv:将源文件重命名为目标文件,或将源文件移动至指定目录。

      用法:mv  [选项]...  [-T] 源文件  目标文件

       或:mv  [选项]...  源文件...  目录

       或:mv  [选项]...  -t  目录  源文件...

      选项:

        --backup[=CONTROL]        为每个已经存在的目标文件创建备份

        -b                 类似--backup,但不接受参数

        -f,--force              覆盖前不询问

        -i,--interactive           覆盖前询问

        -n,--no-clobber           不覆盖已存在的文件

      如果指定了-i、-f、-n中的多个,仅最后一个生效

        --strip-trailing-slashes 去掉每个源文件参数尾部的斜线

        -S, --suffix=SUFFIX 替换常用的备份文件后缀

        -t, --target-directory=DIRECTORY 将所有参数指定的源文件或目录移动至 指定目录

        -T, --no-target-directory 将目标文件视作普通文件处理

        -u, --update 只在源文件文件比目标文件新,或目标文件不存在时才进行移动

        -v, --verbose 详细显示进行的步骤

        --help 显示此帮助信息并退出

        --version 显示版本信息并退出

      备份文件的后缀为"~",除非以--suffix 选项或是SIMPLE_BACKUP_SUFFIX环境变量指定。版本控制的方式可通过--backup 选项或VERSION_CONTROL 环境变量来选择。

       以下是可用的变量值:

              none, off 不进行备份(即使使用了--backup 选项)

              numbered, t 备份文件加上数字进行排序

              existing, nil 若有数字的备份文件已经存在则使用数字,否则使用普通方式备份

              simple, never 永远使用普通方式备份

常用演示:

[root@centos6_test tmp]#ll
总用量 8
-rw-r--r--  1 root root    0 10月 11 10:41 a
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 b
-rw-r--r--  1 root root    0 10月 11 10:41 c
-rw-r--r--  1 root root    0 10月 11 10:41 d
[root@centos6_test tmp]#mv --backup a d
mv:是否覆盖"d"? y
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 b
-rw-r--r--  1 root root    0 10月 11 10:41 c
-rw-r--r--  1 root root    0 10月 11 10:41 d
-rw-r--r--  1 root root    0 10月 11 10:41 d~
[root@centos6_test tmp]#mv -b b c
mv:是否覆盖"c"? y
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 c
-rw-r--r--  1 root root    0 10月 11 10:41 c~
-rw-r--r--  1 root root    0 10月 11 10:41 d
-rw-r--r--  1 root root    0 10月 11 10:41 d~
[root@centos6_test tmp]#mv -f c d
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 c~
-rw-r--r--  1 root root    0 10月 11 10:41 d
-rw-r--r--  1 root root    0 10月 11 10:41 d~
[root@centos6_test tmp]#mv -i c~ d
d   d~  
[root@centos6_test tmp]#mv -i c~ d~ 
mv:是否覆盖"d~"? y
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 d
-rw-r--r--  1 root root    0 10月 11 10:41 d~
[root@centos6_test tmp]#mv -n d d~
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 d
-rw-r--r--  1 root root    0 10月 11 10:41 d~
[root@centos6_test tmp]#mv -S .bak d d~
mv:是否覆盖"d~"? y 
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 d~
-rw-r--r--  1 root root    0 10月 11 10:41 d~.bak
[root@centos6_test tmp]#mv -t aaa/ d~ d~.bak 
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 11 10:46 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
[root@centos6_test tmp]#ll aaa/
总用量 0
-rw-r--r-- 1 root root 0 10月 11 10:41 d~
-rw-r--r-- 1 root root 0 10月 11 10:41 d~.bak
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 11 10:48 aaa
-rw-r--r--  1 root root    0 10月 11 10:48 aac
-rw-r--r--  1 root root    0 10月 11 10:48 aad
-rw-r--r--  1 root root    0 10月 11 10:48 aae
-rw-r--r--  1 root root    0 10月 11 10:48 aaf
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
[root@centos6_test tmp]#mv -T aac aaa/
mv:是否覆盖"aaa/"? y
mv: 无法以非目录来覆盖目录"aaa/"
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 11 10:48 aaa
-rw-r--r--  1 root root    0 10月 11 10:48 aac
-rw-r--r--  1 root root    0 10月 11 10:48 aad
-rw-r--r--  1 root root    0 10月 11 10:48 aae
-rw-r--r--  1 root root    0 10月 11 10:48 aaf
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
[root@centos6_test tmp]#mv -T aac aad 
mv:是否覆盖"aad"? y
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x. 2 root root 4096 10月 11 10:48 aaa
-rw-r--r--  1 root root    0 10月 11 10:48 aad
-rw-r--r--  1 root root    0 10月 11 10:48 aae
-rw-r--r--  1 root root    0 10月 11 10:48 aaf
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
[root@centos6_test tmp]#mv -T aaa/ abcddir/
mv:是否覆盖"abcddir/"? y
[root@centos6_test tmp]#ll
总用量 4
-rw-r--r--  1 root root    0 10月 11 10:48 aad
-rw-r--r--  1 root root    0 10月 11 10:48 aae
-rw-r--r--  1 root root    0 10月 11 10:48 aaf
drwxr-xr-x. 2 root root 4096 10月 11 10:48 abcddir
[root@centos6_test tmp]#ll abcddir/
总用量 0
-rw-r--r-- 1 root root 0 10月 11 10:41 d~
-rw-r--r-- 1 root root 0 10月 11 10:41 d~.bak
[root@centos6_test tmp]#
[root@centos6_test tmp]#echo aaa >> aa.txt
[root@centos6_test tmp]#echo bbb >> bb.txt
[root@centos6_test tmp]#ll
总用量 8
-rw-r--r-- 1 root root 4 10月 11 11:10 aa.txt
-rw-r--r-- 1 root root 4 10月 11 11:10 bb.txt
[root@centos6_test tmp]#mv -u aa.txt bb.txt 
[root@centos6_test tmp]#ll
总用量 8
-rw-r--r-- 1 root root 4 10月 11 11:10 aa.txt
-rw-r--r-- 1 root root 4 10月 11 11:10 bb.txt
[root@centos6_test tmp]#cat aa.txt 
aaa
[root@centos6_test tmp]#cat bb.txt 
bbb
[root@centos6_test tmp]#touch aa.txt 
[root@centos6_test tmp]#ll aa.txt 
-rw-r--r-- 1 root root 4 10月 11 11:11 aa.txt
[root@centos6_test tmp]#mv -u aa.txt bb.txt 
mv:是否覆盖"bb.txt"? y
[root@centos6_test tmp]#ll
总用量 4
-rw-r--r-- 1 root root 4 10月 11 11:11 bb.txt
[root@centos6_test tmp]#cat bb.txt 
aaa
[root@centos6_test tmp]#
  • 文件或目录的复制

    cp:将源文件复制至目标文件,或将多个源文件复制至目标目录。

      用法:cp  [选项]... [-T] 源文件...  目录

         或:cp  [选项]... 源文件 目标文件

       或:cp  [选项]... -t 目录  源文件...

      选项:

        -a, --archive 等于-dR --preserve=all

        --backup[=CONTROL]  为每个已存在的目标文件创建备份

        -b 类似--backup 但不接受参数

        --copy-contents 在递归处理是复制特殊文件内容

        -d 等于--no-dereference --preserve=links

        -f, --force 如果目标文件无法打开则将其移除并重试(当 -n 选项存在时则不需再选此项)

        -i, --interactive 覆盖前询问(使前面的 -n 选项失效)

        -H 跟随源文件中的命令行符号链接

        -l, --link 链接文件而不复制(创建硬连接)

        -L, --dereference 总是跟随符号链接

        -n, --no-clobber 不要覆盖已存在的文件(使前面的 -i 选项失效)

        -P, --no-dereference 不跟随源文件中的符号链接

        -p 等于--preserve=模式,所有权,时间戳

        --preserve[=属性列表 保持指定的属性(默认:模式,所有权,时间戳),如果可能保持附加属性:环境、链接、xattr 等

        -c same as --preserve=context

        --sno-preserve=属性列表 不保留指定的文件属性

        --parents 复制前在目标目录创建来源文件路径中的所有目录

        -R, -r, --recursive 递归复制目录及其子目录内的所有内容

        --reflink[=WHEN] 控制克隆/CoW 副本。请查看下面的内如。

        --remove-destination 尝试打开目标文件前先删除已存在的目的地文件 (相对于 --force 选项)

        --sparse=WHEN 控制创建稀疏文件的方式

        --strip-trailing-slashes 删除参数中所有源文件/目录末端的斜杠

        -s, --symbolic-link 只创建符号链接而不复制文件

        -S, --suffix=后缀 自行指定备份文件的后缀

        -t, --target-directory=目录 将所有参数指定的源文件/目录复制至目标目录

        -T, --no-target-directory 将目标目录视作普通文件

        -u, --update 当源文件比目标文件新时或目标文件不存在时才复制

        -v, --verbose 显示详细进行步骤

        -x, --one-file-system stay on this file system

        -Z, --context=CONTEXT set security context of copy to CONTEXT

        --help 显示此帮助信息并退出

        --version 显示版本信息并退出

常用演示:

[root@centos6_test tmp]#echo aa > aa
[root@centos6_test tmp]#ll
总用量 4-rw-r--r-- 1 root root 3 10月 11 12:24 aa
[root@centos6_test tmp]#stat aa
  File: "aa"
  Size: 3         	Blocks: 8          IO Block: 4096   普通文件
Device: fd00h/64768d	Inode: 1316023     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-11 12:24:06.980002423 +0800
Modify: 2019-10-11 12:24:06.980002423 +0800
Change: 2019-10-11 12:24:06.980002423 +0800
[root@centos6_test tmp]#cp -a aa bb
[root@centos6_test tmp]#ll
总用量 8
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
[root@centos6_test tmp]#stat bb
  File: "bb"
  Size: 3         	Blocks: 8          IO Block: 4096   普通文件
Device: fd00h/64768d	Inode: 1316030     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-11 12:24:06.980002423 +0800
Modify: 2019-10-11 12:24:06.980002423 +0800
Change: 2019-10-11 12:24:21.380012785 +0800
[root@centos6_test tmp]#cat bb
aa
[root@centos6_test tmp]#cp --backup aa cc
[root@centos6_test tmp]#ll
总用量 12
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
-rw-r--r-- 1 root root 3 10月 11 12:25 cc
[root@centos6_test tmp]#cp --backup aa cc
cp:是否覆盖"cc"? y
[root@centos6_test tmp]#ll
总用量 16
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
-rw-r--r-- 1 root root 3 10月 11 12:25 cc
-rw-r--r-- 1 root root 3 10月 11 12:25 cc~
[root@centos6_test tmp]#cp --backup=numbered aa cc
cp:是否覆盖"cc"? y
[root@centos6_test tmp]#ll
总用量 20
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
-rw-r--r-- 1 root root 3 10月 11 12:25 cc
-rw-r--r-- 1 root root 3 10月 11 12:25 cc~
-rw-r--r-- 1 root root 3 10月 11 12:25 cc.~1~
[root@centos6_test tmp]#cp -b aa cc
cp:是否覆盖"cc"? y
[root@centos6_test tmp]#ll
总用量 24
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
-rw-r--r-- 1 root root 3 10月 11 12:25 cc
-rw-r--r-- 1 root root 3 10月 11 12:25 cc~
-rw-r--r-- 1 root root 3 10月 11 12:25 cc.~1~
-rw-r--r-- 1 root root 3 10月 11 12:25 cc.~2~
[root@centos6_test tmp]#ll
总用量 24
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
-rw-r--r-- 1 root root 3 10月 11 12:25 cc
-rw-r--r-- 1 root root 3 10月 11 12:25 cc~
-rw-r--r-- 1 root root 3 10月 11 12:25 cc.~1~
-rw-r--r-- 1 root root 3 10月 11 12:25 cc.~2~
[root@centos6_test tmp]#cp -d /etc/rc3.d/S01sysstat sys
[root@centos6_test tmp]#ll
总用量 24
-rw-r--r-- 1 root root  3 10月 11 12:24 aa
-rw-r--r-- 1 root root  3 10月 11 12:24 bb
-rw-r--r-- 1 root root  3 10月 11 12:25 cc
-rw-r--r-- 1 root root  3 10月 11 12:25 cc~
-rw-r--r-- 1 root root  3 10月 11 12:25 cc.~1~
-rw-r--r-- 1 root root  3 10月 11 12:25 cc.~2~
lrwxrwxrwx 1 root root 17 10月 11 12:30 sys -> ../init.d/sysstat
[root@centos6_test tmp]#cp -i aa bb
cp:是否覆盖"bb"? y
[root@centos6_test tmp]#cp -H /etc/rc3.d/S01sysstat xx
[root@centos6_test tmp]#ll
总用量 28
-rw-r--r-- 1 root root    3 10月 11 12:24 aa
-rw-r--r-- 1 root root    3 10月 11 12:32 bb
-rw-r--r-- 1 root root    3 10月 11 12:25 cc
-rw-r--r-- 1 root root    3 10月 11 12:25 cc~
-rw-r--r-- 1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r-- 1 root root    3 10月 11 12:25 cc.~2~
lrwxrwxrwx 1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x 1 root root 1144 10月 11 12:33 xx
[root@centos6_test tmp]#ls -i /etc/fstab 
654087 /etc/fstab
[root@centos6_test tmp]#cp -l /etc/fstab .
[root@centos6_test tmp]#ls -i .
654087 fstab
[root@centos6_test tmp]#cp -L /etc/rc3.d/S01sysstat ddd
[root@centos6_test tmp]#ll
总用量 32
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:32 bb
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
[root@centos6_test tmp]#cp -n aa bb
[root@centos6_test tmp]#ll
总用量 32
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:32 bb
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
[root@centos6_test tmp]#cp -P /etc/rc3.d/S01sysstat xxx
[root@centos6_test tmp]#ll
总用量 32
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:32 bb
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
lrwxrwxrwx  1 root root   17 10月 11 12:35 xxx -> ../init.d/sysstat
[root@centos6_test tmp]#ls /boot/
config-2.6.32-696.el6.x86_64  initramfs-2.6.32-696.el6.x86_64.img    symvers-2.6.32-696.el6.x86_64.gz
efi                           initrd-2.6.32-696.el6.x86_64kdump.img  System.map-2.6.32-696.el6.x86_64
grub                          lost+found                             vmlinuz-2.6.32-696.el6.x86_64
[root@centos6_test tmp]#cp -r /boot/ boot
[root@centos6_test tmp]#ll
总用量 40
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:32 bb
dr-xr-xr-x  5 root root 4096 10月 11 12:38 boot
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
-rwxr-xr-x  1 root root 1144 10月 11 12:36 fff
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
lrwxrwxrwx  1 root root   17 10月 11 12:35 xxx -> ../init.d/sysstat
[root@centos6_test tmp]#ls boot
config-2.6.32-696.el6.x86_64  initramfs-2.6.32-696.el6.x86_64.img    symvers-2.6.32-696.el6.x86_64.gz
efi                           initrd-2.6.32-696.el6.x86_64kdump.img  System.map-2.6.32-696.el6.x86_64
grub                          lost+found                             vmlinuz-2.6.32-696.el6.x86_64
[root@centos6_test tmp]#ll /boot
总用量 38874
-rw-r--r--. 1 root root   108164 3月  22 2017 config-2.6.32-696.el6.x86_64
drwxr-xr-x. 3 root root     1024 9月  19 08:55 efi
drwxr-xr-x. 2 root root     1024 9月  19 09:06 grub
-rw-------. 1 root root 26688788 9月  19 08:58 initramfs-2.6.32-696.el6.x86_64.img
-rw-------. 1 root root  5871604 9月  19 09:07 initrd-2.6.32-696.el6.x86_64kdump.img
drwx------. 2 root root    12288 9月  19 08:44 lost+found
-rw-r--r--. 1 root root   215634 3月  22 2017 symvers-2.6.32-696.el6.x86_64.gz
-rw-r--r--. 1 root root  2622364 3月  22 2017 System.map-2.6.32-696.el6.x86_64
-rwxr-xr-x. 1 root root  4274992 3月  22 2017 vmlinuz-2.6.32-696.el6.x86_64
[root@centos6_test tmp]#cp -s ddd ddd.link
[root@centos6_test tmp]#ll
总用量 40
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:32 bb
dr-xr-xr-x  5 root root 4096 10月 11 12:38 boot
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root    3 10月 11 12:39 ddd.link -> ddd
-rwxr-xr-x  1 root root 1144 10月 11 12:36 fff
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
lrwxrwxrwx  1 root root   17 10月 11 12:35 xxx -> ../init.d/sysstat
[root@centos6_test tmp]#cp -S .bak aa bb
cp:是否覆盖"bb"? y
[root@centos6_test tmp]#ll
总用量 44
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:39 bb
-rw-r--r--  1 root root    3 10月 11 12:32 bb.bak
dr-xr-xr-x  5 root root 4096 10月 11 12:38 boot
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root    3 10月 11 12:39 ddd.link -> ddd
-rwxr-xr-x  1 root root 1144 10月 11 12:36 fff
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
lrwxrwxrwx  1 root root   17 10月 11 12:35 xxx -> ../init.d/sysstat
[root@centos6_test tmp]#mkdir test
[root@centos6_test tmp]#ll
总用量 48
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:39 bb
-rw-r--r--  1 root root    3 10月 11 12:32 bb.bak
dr-xr-xr-x  5 root root 4096 10月 11 12:38 boot
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root    3 10月 11 12:39 ddd.link -> ddd
-rwxr-xr-x  1 root root 1144 10月 11 12:36 fff
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
drwxr-xr-x  2 root root 4096 10月 11 12:41 test
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
lrwxrwxrwx  1 root root   17 10月 11 12:35 xxx -> ../init.d/sysstat
[root@centos6_test tmp]#cp -t test aa bb bb.bak cc cc~ ddd fff xx
[root@centos6_test tmp]#ll test/
总用量 32
-rw-r--r-- 1 root root    3 10月 11 12:41 aa
-rw-r--r-- 1 root root    3 10月 11 12:41 bb
-rw-r--r-- 1 root root    3 10月 11 12:41 bb.bak
-rw-r--r-- 1 root root    3 10月 11 12:41 cc
-rw-r--r-- 1 root root    3 10月 11 12:41 cc~
-rwxr-xr-x 1 root root 1144 10月 11 12:41 ddd
-rwxr-xr-x 1 root root 1144 10月 11 12:41 fff
-rwxr-xr-x 1 root root 1144 10月 11 12:41 xx

    install:将文件复制至目标文件,或将多个源文件复制至目标目录。

用法:

用法:install [选项]... [-T] 源文件 目标文件
 或:install [选项]... 源文件... 目录
 或:install [选项]... -t 目录 源文件...
 或:install [选项]... -d 目录...

在前三种形式中,将源文件复制到目标文件或将多个源文件复制到一个已存在的目录
中同时设置其所有权和权限模式。在第四种形式中,创建给出目标目录中的所有组件。

选项:

长选项必须使用的参数对于短选项时也是必需使用的。
      --backup[=CONTROL]	为每个已存在的文件创建备份
  -b			类似--backup 但不接受参数
  -c			(忽略)
  -C, --compare		比较每组源文件和目标文件,在一些情况下不修改目标文件
  -d, --directory	将所有参数视为目录名称;为指定的目录创建所有组件
  -D			创建目标目录的所有必要的父目录,然后将源文件复制至该目录
  -g, --group=组	自行设置所属组,而不是进程目前的所属组
  -m, --mode=模式	自行设置权限模式(像chmod),而不是rwxr-xr-x
  -o, --owner=所有者	自行设置所有者(只适用于超级用户)
  -p, --preserve-timestamps	修改源文件的访问/修改时间以与目标文件保持一致
  -s, --strip			拆解符号表
      --strip-program=程序	指定拆解二进制文件的程序
  -S, --suffix=后缀		覆盖常用备份文件后缀
  -t, --target-directory=目录	将源文件所有参数复制到指定目录
  -T, --no-target-directory	将目标文件视为普通文件
  -v, --verbose			创建目录时显示其名称
  -P, --preserve-context (SELinux) preserve security context
  -Z, --context=CONTEXT  (SELinux) set security context of files and directories
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出  

说明:install命令和cp命令类似,都可以将指定文件或目录拷贝到指定路径,但install可以控制目标文件的属性。

常用演示:

[root@centos6_test tmp]#install /etc/fstab ./xx.tab
[root@centos6_test tmp]#ll
总用量 4
-rwxr-xr-x 1 root root 779 10月 11 13:30 xx.tab
[root@centos6_test tmp]#cat xx.tab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#install -d test
[root@centos6_test tmp]#ll
总用量 8
drwxr-xr-x 2 root root 4096 10月 11 13:31 test
-rwxr-xr-x 1 root root  779 10月 11 13:30 xx.tab
[root@centos6_test tmp]#install -t test/ /etc/fstab xx.tab /etc/issue.net
[root@centos6_test tmp]#ll test/
总用量 8
-rwxr-xr-x 1 root root 779 10月 11 13:32 fstab
-rwxr-xr-x 1 root root   0 10月 11 13:32 issue.net
-rwxr-xr-x 1 root root 779 10月 11 13:32 xx.tab
[root@centos6_test tmp]#install -g 'qiuhom' -o 'qiuhom' -m 600 xx.tab aaa
[root@centos6_test tmp]#ll
总用量 12
-rw------- 1 qiuhom qiuhom  779 10月 11 13:34 aaa
drwxr-xr-x 2 root   root   4096 10月 11 13:32 test
-rwxr-xr-x 1 root   root    779 10月 11 13:30 xx.tab
[root@centos6_test tmp]#
  • 切换目录

    cd:change directory 切换目录

      用法:cd 目录名

      选项:无

cd . 进入当前目录

cd .. 进入上一级目录

cd - 进入上一次所在目录

cd ~ 进入当前用户家目录

cd ~USERName 进入到指定用户家目录

cd 不加任何内容 进到当前用户家目录

常用演示:

[root@centos6_test rc3.d]#cd .
[root@centos6_test rc3.d]#cd ..
[root@centos6_test etc]#cd -
/etc/rc3.d[root@centos6_test init.d]#pwd
/etc/init.d
[root@centos6_test init.d]#cd ~qiuhom
[root@centos6_test qiuhom]#pwd
/home/qiuhom
[root@centos6_test qiuhom]#cd ~
[root@centos6_test ~]#pwd
/root
[root@centos6_test ~]#cd /etc/init.d/
[root@centos6_test init.d]#cd
[root@centos6_test ~]#pwd
/root
[root@centos6_test ~]#
  • 文件或目录的查看

     ls:列出文件的信息(默认是当前目录)。

用法:

用法:ls [选项]... [文件]...

选项:

长选项必须使用的参数对于短选项时也是必需使用的。
  -a, --all			不隐藏任何以. 开始的项目
  -A, --almost-all		列出除. 及.. 以外的任何项目
      --author			与-l 同时使用时列出每个文件的作者
  -b, --escape			以八进制溢出序列表示不可打印的字符
      --block-size=大小		块以指定大小的字节为单位
  -B, --ignore-backups		不列出任何以"~"字符结束的项目
  -c				配合-lt:根据ctime 排序并显示ctime(文件
				状态最后更改的时间)
				配合-l:显示ctime 但根据名称排序				其他情况:按ctime 排序
  -C				每栏由上至下列出项目
      --color[=WHEN]		控制是否使用色彩分辨文件。WHEN 可以是
				"never"(默认)、"always"或"auto"其中之一
  -d, --directory		当遇到目录时列出目录本身而非目录内的文件
  -D, --dired			产生适合Emacs 的dired 模式使用的结果
  -f				不进行排序,-aU 选项生效,-lst 选项失效
  -F, --classify		加上文件类型的指示符号(*/=@| 其中一个)
      --format=关键字		交错-x,逗号分隔-m,水平-x,长-l,
				单栏-1,详细-l,垂直-C
      --full-time		即-l --time-style=full-iso
  -g				类似-l,但不列出所有者
      --group-directories-first
			在文件前分组目录。此选项可与--sort 一起使用,
			但是一旦使用--sort=none (-U)将禁用分组
  -G, --no-group		以一个长列表的形式,不输出组名
  -h, --human-readable		与-l 一起,以易于阅读的格式输出文件大小
				(例如 1K 234M 2G)
      --si			同上面类似,但是使用1000 为基底而非1024
  -H, --dereference-command-line
				跟随命令行列出的符号链接
      --dereference-command-line-symlink-to-dir
				跟随命令行列出的目录的符号链接
      --hide=PATTERN         	隐藏符合PATTERN 模式的项目
				(-a 或 -A 将覆盖此选项)
      --indicator-style=方式	指定在每个项目名称后加上指示符号方式:
				none (默认),classify (-F),file-type (-p)
  -i, --inode			显示每个文件的inode 号
  -I, --ignore=PATTERN		不显示任何符合指定shell PATTERN 的项目
  -k				即--block-size=1K
  -l				使用较长格式列出信息
  -L, --dereference		当显示符号链接的文件信息时,显示符号链接所指示
				的对象而并非符号链接本身的信息
  -m				所有项目以逗号分隔,并填满整行行宽
  -n, --numeric-uid-gid		类似 -l,但列出UID 及GID 号
  -N, --literal			输出未经处理的项目名称 (如不特别处理控制字符)
  -o				类似 -l,但不列出有关组的信息
  -p,  --indicator-style=slash	对目录加上表示符号"/"
  -q, --hide-control-chars	以"?"字符代替无法打印的字符
      --show-control-chars	直接显示无法打印的字符 (这是默认方式,除非调用
				的程序名称是"ls"而且是在终端输出结果)
  -Q, --quote-name		将条目名称括上双引号
      --quoting-style=方式	使用指定的quoting 方式显示条目的名称:
				literal、locale、shell、shell-always、c、escape
  -r, --reverse			排序时保留顺序
  -R, --recursive		递归显示子目录
  -s, --size			以块数形式显示每个文件分配的尺寸
  -S				根据文件大小排序
      --sort=WORD		以下是可选用的WORD 和它们代表的相应选项:
				extension -X       status   -c
				none      -U       time     -t
				size      -S       atime    -u
				time      -t       access   -u
				version   -v       use      -u
      --time=WORD		和-l 同时使用时显示WORD 所代表的时间而非修改时
				间:atime、access、use、ctime 或status;加上
				--sort=time 选项时会以指定时间作为排序关键字
      --time-style=STYLE	和-l 同时使用时根据STYLE 代表的格式显示时间:
				full-iso、iso、locale、posix-iso、+FORMAT。
				FORMAT 即是"date"所用的时间格式;如果FORMAT
				是FORMAT1<换行>FORMAT2,FORMAT1 适用于较旧
				的文件而FORMAT2 适用于较新的文件;如果STYLE
				以"posix-"开头,则STYLE 仅在POSIX 语系之外
				生效。
  -t				根据修改时间排序
  -T, --tabsize=宽度	指定制表符(Tab)的宽度,而非8 个字符
  -t			按修改时间排序
  -T, --tabsize=COLS	指定制表符(Tab)的宽度,而非8个字符
  -u			同-lt 一起使用:按照访问时间排序并显示
			同-l一起使用:显示访问时间并按文件名排序
			其他:按照访问时间排序
  -U			不进行排序;按照目录顺序列出项目
  -v			在文本中进行数字(版本)的自然排序
  -w, --width=COLS           assume screen width instead of current value
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -1                         list one file per line

常用演示:

[root@centos6_test /]#ls
bin   dev  home  lib64       media  mnt  opt   root  selinux  sys  usr
boot  etc  lib   lost+found  misc   net  proc  sbin  srv      tmp  var
[root@centos6_test /]#ls -l
总用量 102
dr-xr-xr-x.   2 root root  4096 9月  19 08:56 bin
dr-xr-xr-x.   5 root root  1024 9月  19 09:05 boot
drwxr-xr-x   21 root root  3800 10月 11 10:14 dev
drwxr-xr-x. 119 root root 12288 10月 11 13:38 etc
drwxr-xr-x.   3 root root  4096 10月  1 14:39 home
dr-xr-xr-x.  11 root root  4096 9月  19 08:52 lib
dr-xr-xr-x.   9 root root 12288 9月  19 08:52 lib64
drwx------.   2 root root 16384 9月  19 08:44 lost+found
drwxr-xr-x.   2 root root  4096 9月  23 2011 media
drwxr-xr-x    2 root root     0 10月 11 10:14 misc
drwxr-xr-x.   2 root root  4096 9月  23 2011 mnt
drwxr-xr-x    2 root root     0 10月 11 10:14 net
drwxr-xr-x.   3 root root  4096 9月  19 08:55 opt
dr-xr-xr-x  136 root root     0 10月 11 2019 proc
dr-xr-x---.  16 root root  4096 10月 11 13:54 root
dr-xr-xr-x.   2 root root 12288 9月  19 08:56 sbin
drwxr-xr-x.   2 root root  4096 9月  19 08:46 selinux
drwxr-xr-x.   2 root root  4096 9月  23 2011 srv
drwxr-xr-x   13 root root     0 10月 11 2019 sys
drwxrwxrwt.   5 root root  4096 10月 11 13:39 tmp
drwxr-xr-x.  13 root root  4096 9月  19 08:46 usr
drwxr-xr-x.  22 root root  4096 9月  19 08:55 var
[root@centos6_test /]#ls -a
.          .autorelabel  .dbus  home   lost+found  mnt  proc  selinux  tmp
..         bin           dev    lib    media       net  root  srv      usr
.autofsck  boot          etc    lib64  misc        opt  sbin  sys      var
[root@centos6_test /]#ls -A
.autofsck     bin   .dbus  etc   lib    lost+found  misc  net  proc  sbin     srv  tmp  var
.autorelabel  boot  dev    home  lib64  media       mnt   opt  root  selinux  sys  usr
[root@centos6_test /]#ls -G
bin   dev  home  lib64       media  mnt  opt   root  selinux  sys  usr
boot  etc  lib   lost+found  misc   net  proc  sbin  srv      tmp  var
[root@centos6_test /]#ls -lG
总用量 102
dr-xr-xr-x.   2 root  4096 9月  19 08:56 bin
dr-xr-xr-x.   5 root  1024 9月  19 09:05 boot
drwxr-xr-x   21 root  3800 10月 11 10:14 dev
drwxr-xr-x. 119 root 12288 10月 11 13:38 etc
drwxr-xr-x.   3 root  4096 10月  1 14:39 home
dr-xr-xr-x.  11 root  4096 9月  19 08:52 lib
dr-xr-xr-x.   9 root 12288 9月  19 08:52 lib64
drwx------.   2 root 16384 9月  19 08:44 lost+found
drwxr-xr-x.   2 root  4096 9月  23 2011 media
drwxr-xr-x    2 root     0 10月 11 10:14 misc
drwxr-xr-x.   2 root  4096 9月  23 2011 mnt
drwxr-xr-x    2 root     0 10月 11 10:14 net
drwxr-xr-x.   3 root  4096 9月  19 08:55 opt
dr-xr-xr-x  136 root     0 10月 11 2019 proc
dr-xr-x---.  16 root  4096 10月 11 13:54 root
dr-xr-xr-x.   2 root 12288 9月  19 08:56 sbin
drwxr-xr-x.   2 root  4096 9月  19 08:46 selinux
drwxr-xr-x.   2 root  4096 9月  23 2011 srv
drwxr-xr-x   13 root     0 10月 11 2019 sys
drwxrwxrwt.   5 root  4096 10月 11 13:39 tmp
drwxr-xr-x.  13 root  4096 9月  19 08:46 usr
drwxr-xr-x.  22 root  4096 9月  19 08:55 var
[root@centos6_test /]#ls -lh
总用量 102K
dr-xr-xr-x.   2 root root 4.0K 9月  19 08:56 bin
dr-xr-xr-x.   5 root root 1.0K 9月  19 09:05 boot
drwxr-xr-x   21 root root 3.8K 10月 11 10:14 dev
drwxr-xr-x. 119 root root  12K 10月 11 13:38 etc
drwxr-xr-x.   3 root root 4.0K 10月  1 14:39 home
dr-xr-xr-x.  11 root root 4.0K 9月  19 08:52 lib
dr-xr-xr-x.   9 root root  12K 9月  19 08:52 lib64
drwx------.   2 root root  16K 9月  19 08:44 lost+found
drwxr-xr-x.   2 root root 4.0K 9月  23 2011 media
drwxr-xr-x    2 root root    0 10月 11 10:14 misc
drwxr-xr-x.   2 root root 4.0K 9月  23 2011 mnt
drwxr-xr-x    2 root root    0 10月 11 10:14 net
drwxr-xr-x.   3 root root 4.0K 9月  19 08:55 opt
dr-xr-xr-x  136 root root    0 10月 11 2019 proc
dr-xr-x---.  16 root root 4.0K 10月 11 13:54 root
dr-xr-xr-x.   2 root root  12K 9月  19 08:56 sbin
drwxr-xr-x.   2 root root 4.0K 9月  19 08:46 selinux
drwxr-xr-x.   2 root root 4.0K 9月  23 2011 srv
drwxr-xr-x   13 root root    0 10月 11 2019 sys
drwxrwxrwt.   5 root root 4.0K 10月 11 13:39 tmp
drwxr-xr-x.  13 root root 4.0K 9月  19 08:46 usr
drwxr-xr-x.  22 root root 4.0K 9月  19 08:55 var
[root@centos6_test /]#ls -ls
总用量 102
 4 dr-xr-xr-x.   2 root root  4096 9月  19 08:56 bin
 2 dr-xr-xr-x.   5 root root  1024 9月  19 09:05 boot
 0 drwxr-xr-x   21 root root  3800 10月 11 10:14 dev
12 drwxr-xr-x. 119 root root 12288 10月 11 13:38 etc
 4 drwxr-xr-x.   3 root root  4096 10月  1 14:39 home
 4 dr-xr-xr-x.  11 root root  4096 9月  19 08:52 lib
12 dr-xr-xr-x.   9 root root 12288 9月  19 08:52 lib64
16 drwx------.   2 root root 16384 9月  19 08:44 lost+found
 4 drwxr-xr-x.   2 root root  4096 9月  23 2011 media
 0 drwxr-xr-x    2 root root     0 10月 11 10:14 misc
 4 drwxr-xr-x.   2 root root  4096 9月  23 2011 mnt
 0 drwxr-xr-x    2 root root     0 10月 11 10:14 net
 4 drwxr-xr-x.   3 root root  4096 9月  19 08:55 opt
 0 dr-xr-xr-x  136 root root     0 10月 11 2019 proc
 4 dr-xr-x---.  16 root root  4096 10月 11 13:54 root
12 dr-xr-xr-x.   2 root root 12288 9月  19 08:56 sbin
 4 drwxr-xr-x.   2 root root  4096 9月  19 08:46 selinux
 4 drwxr-xr-x.   2 root root  4096 9月  23 2011 srv
 0 drwxr-xr-x   13 root root     0 10月 11 2019 sys
 4 drwxrwxrwt.   5 root root  4096 10月 11 13:39 tmp
 4 drwxr-xr-x.  13 root root  4096 9月  19 08:46 usr
 4 drwxr-xr-x.  22 root root  4096 9月  19 08:55 var
[root@centos6_test /]#ls -lsh
总用量 102K
4.0K dr-xr-xr-x.   2 root root 4.0K 9月  19 08:56 bin
2.0K dr-xr-xr-x.   5 root root 1.0K 9月  19 09:05 boot
   0 drwxr-xr-x   21 root root 3.8K 10月 11 10:14 dev
 12K drwxr-xr-x. 119 root root  12K 10月 11 13:38 etc
4.0K drwxr-xr-x.   3 root root 4.0K 10月  1 14:39 home
4.0K dr-xr-xr-x.  11 root root 4.0K 9月  19 08:52 lib
 12K dr-xr-xr-x.   9 root root  12K 9月  19 08:52 lib64
 16K drwx------.   2 root root  16K 9月  19 08:44 lost+found
4.0K drwxr-xr-x.   2 root root 4.0K 9月  23 2011 media
   0 drwxr-xr-x    2 root root    0 10月 11 10:14 misc
4.0K drwxr-xr-x.   2 root root 4.0K 9月  23 2011 mnt
   0 drwxr-xr-x    2 root root    0 10月 11 10:14 net
4.0K drwxr-xr-x.   3 root root 4.0K 9月  19 08:55 opt
   0 dr-xr-xr-x  136 root root    0 10月 11 2019 proc
4.0K dr-xr-x---.  16 root root 4.0K 10月 11 13:54 root
 12K dr-xr-xr-x.   2 root root  12K 9月  19 08:56 sbin
4.0K drwxr-xr-x.   2 root root 4.0K 9月  19 08:46 selinux
4.0K drwxr-xr-x.   2 root root 4.0K 9月  23 2011 srv
   0 drwxr-xr-x   13 root root    0 10月 11 2019 sys
4.0K drwxrwxrwt.   5 root root 4.0K 10月 11 13:39 tmp
4.0K drwxr-xr-x.  13 root root 4.0K 9月  19 08:46 usr
4.0K drwxr-xr-x.  22 root root 4.0K 9月  19 08:55 var
[root@centos6_test ~]#cd /tmp/
[root@centos6_test tmp]#ls -lR
.:
总用量 12
-rw------- 1 qiuhom qiuhom    4 10月 11 13:57 aaa
drwxr-xr-x 2 root   root   4096 10月 11 13:32 test
-rwxr-xr-x 1 root   root    779 10月 11 13:30 xx.tab

./test:
总用量 8
-rwxr-xr-x 1 root root 779 10月 11 13:32 fstab
-rwxr-xr-x 1 root root   0 10月 11 13:32 issue.net
-rwxr-xr-x 1 root root 779 10月 11 13:32 xx.tab
[root@centos6_test tmp]#ls -lr
总用量 12
-rwxr-xr-x 1 root   root    779 10月 11 13:30 xx.tab
drwxr-xr-x 2 root   root   4096 10月 11 13:32 test
-rw------- 1 qiuhom qiuhom    4 10月 11 13:57 aaa
[root@centos6_test tmp]#ls -lS
总用量 12
drwxr-xr-x 2 root   root   4096 10月 11 13:32 test
-rwxr-xr-x 1 root   root    779 10月 11 13:30 xx.tab
-rw------- 1 qiuhom qiuhom    4 10月 11 13:57 aaa
[root@centos6_test tmp]#ls -i
1316038 aaa  1316024 test  1316023 xx.tab
[root@centos6_test tmp]# 

    cat:将文件或标准输入组合输出到标准输出。

用法:

用法:cat [选项]... [文件]...

选项:

-A, --show-all           等于-vET
  -b, --number-nonblank    对非空输出行编号
  -e                       等于-vE
  -E, --show-ends          在每行结束处显示"$"
  -n, --number             对输出的所有行编号
  -s, --squeeze-blank      不输出多行空行
  -t                       与-vT 等价
  -T, --show-tabs          将跳格字符显示为^I
  -u                       (被忽略)
  -v, --show-nonprinting   使用^ 和M- 引用,除了LFD和 TAB 之外
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

常用演示:

[root@centos6_test tmp]#cat xx.tab 
     
				
		    #


# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
	
     	     	   	

#
# Accessible filesystems, b
# y reference, are maint
# ained under '/dev/disk'
[root@centos6_test tmp]#cat -A xx.tab 
     $
^I^I^I^I$
^I^I    #$
$
$
# /etc/fstab$
# Created by anaconda on Thu Sep 19 08:46:05 2019$
^I$
     ^I     ^I   ^I$
$
#$
# Accessible filesystems, b$
# y reference, are maint$
# ained under '/dev/disk'$
[root@centos6_test tmp]#cat -e xx.tab 
     $
				$
		    #$
$
$
# /etc/fstab$
# Created by anaconda on Thu Sep 19 08:46:05 2019$
	$
     	     	   	$
$
#$
# Accessible filesystems, b$
# y reference, are maint$
# ained under '/dev/disk'$
[root@centos6_test tmp]#cat -n xx.tab 
     1	     
     2					
     3			    #
     4	
     5	
     6	# /etc/fstab
     7	# Created by anaconda on Thu Sep 19 08:46:05 2019
     8		
     9	     	     	   	
    10	
    11	#
    12	# Accessible filesystems, b
    13	# y reference, are maint
    14	# ained under '/dev/disk'
[root@centos6_test tmp]#cat -b xx.tab 
     1	     
     2					
     3			    #


     4	# /etc/fstab
     5	# Created by anaconda on Thu Sep 19 08:46:05 2019
     6		
     7	     	     	   	

     8	#
     9	# Accessible filesystems, b
    10	# y reference, are maint
    11	# ained under '/dev/disk'
[root@centos6_test tmp]#cat -T xx.tab 
     
^I^I^I^I
^I^I    #


# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
^I
     ^I     ^I   ^I

#
# Accessible filesystems, b
# y reference, are maint
# ained under '/dev/disk'

cat除了上面的用法还有一个比较常用的用法,就是利用cat的标准输出,重定向到文件如下:

[root@centos6_test tmp]#cat >> test << EOF
> aaaaaa
> gggggg
> bbbbbb
> cccccc
> dddddd
> EOF
[root@centos6_test tmp]#cat test 
aaaaaa
gggggg
bbbbbb
cccccc
dddddd
[root@centos6_test tmp]#

说明:上面这种用法需要注意EOF结束的时候前后不能有任何字符,包括空格。

    tac:将指定每个文件按行倒置并写到标准输出,如果不指定文件,或文件为"-",则从标准输入读取数据。

用法:

用法:tac [选项]... [文件]...

选项:

长选项必须使用的参数对于短选项时也是必需使用的。
  -b, --before			在行前而非行尾添加分隔标志
  -r, --regex			将分隔标志视作正则表达式来解析
  -s, --separator=字符串	使用指定字符串代替换行作为分隔标志
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

常用演示:

[root@centos6_test tmp]#cat test 
1111111
2222222
3333333
4444444
5555555
[root@centos6_test tmp]#tac test 
5555555
4444444
3333333
2222222
1111111

    more

用法:

usage: more [-dflpcsu] [+linenum | +/pattern] name1 name2 ...

选项:

-num 一次显示的行数
-d 提示使用者,在画面下方显示 [Press space to continue, 'q' to quit.] ,如果使用者按错键,则会显示 [Press 'h' for instructions.] 而不是 '哔' 声
-l 取消遇见特殊字元 ^L(送纸字元)时会暂停的功能
-f 计算行数时,以实际上的行数,而非自动换行过后的行数(有些单行字数太长的会被扩展为两行或两行以上)
-p 不以卷动的方式显示每一页,而是先清除萤幕后再显示内容
-c 跟 -p 相似,不同的是先显示内容再清除其他旧资料
-s 当遇到有连续两行以上的空白行,就代换为一行的空白行
-u 不显示下引号 (根据环境变数 TERM 指定的 terminal 而有所不同)
+/pattern 在每个文档显示前搜寻该字串(pattern),然后从该字串之后开始显示
+num 从第 num 行开始显示
fileNames 欲显示内容的文档,可为复数个数

常用操作:

Enter 向下n行,需要定义。默认为1行
Ctrl+F 向下滚动一屏
空格键 向下滚动一屏
Ctrl+B 返回上一屏
= 输出当前行的行号
:f 输出文件名和当前行的行号
V 调用vi编辑器
!命令 调用Shell,并执行命令
q 退出more

常用演示:

[root@centos6_test tmp]#cat fstab 





#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019




#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#more -s fstab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019

#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#more +15 fstab 
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#

    less:分页显示文件内容,或分页显示标准输出。

用法:

less [参数] 文件 

选项:

-b <缓冲区大小> 设置缓冲区的大小
-e 当文件显示结束后,自动离开
-f 强迫打开特殊文件,例如外围设备代号、目录和二进制文件
-g 只标志最后搜索的关键词
-i 忽略搜索时的大小写
-m 显示类似more命令的百分比
-N 显示每行的行号
-o <文件名> 将less 输出的内容在指定文件中保存起来
-Q 不使用警告音
-s 显示连续空行为一行
-S 行过长时间将超出部分舍弃
-x <数字> 将"tab"键显示为规定的数字

常用操作:

/字符串:向下搜索"字符串"的功能
?字符串:向上搜索"字符串"的功能
n:重复前一个搜索(与 / 或 ? 有关)
N:反向重复前一个搜索(与 / 或 ? 有关)
b 向后翻一页
d 向后翻半页
h 显示帮助界面
Q 退出less 命令
u 向前滚动半页
y 向前滚动一行
空格键 滚动一页
回车键 滚动一行
[pagedown]: 向下翻动一页
[pageup]: 向上翻动一页
全屏导航

ctrl + F - 向前移动一屏
ctrl + B - 向后移动一屏
ctrl + D - 向前移动半屏
ctrl + U - 向后移动半屏
2.单行导航

j - 向前移动一行
k - 向后移动一行
3.其它导航

G - 移动到最后一行
g - 移动到第一行
q / ZZ - 退出 less 命令
4.其它有用的命令

v - 使用配置的编辑器编辑当前文件
h - 显示 less 的帮助文档
&pattern - 仅显示匹配模式的行,而不是整个文件

常用演示:

1、查看文件

less /etc/fstab

2、命令输出结果太多可以用less分页显示

ps -ef |less

3、浏览多个文件

less /etc/passwd /etc/fstab /etc/inittab

    head:将指定的文件的头10行显示到标准输出。

用法:

head [选项]... [文件]...

选项:

长选项必须使用的参数对于短选项时也是必需使用的。
  -c,  --bytes=[-]K	显示每个文件的前K 字节内容;
			如果附加"-"参数,则除了每个文件的最后K字节数据外
			显示剩余全部内容
  -n, --lines=[-]K	显示每个文件的前K 行内容;
			如果附加"-"参数,则除了每个文件的最后K 行外显示
			剩余全部内容
  -q, --quiet, --silent	不显示包含给定文件名的文件头
  -v, --verbose		总是显示包含给定文件名的文件头
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

常用演示:

[root@centos6_test tmp]#head /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
[root@centos6_test tmp]#head -n 4 /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
[root@centos6_test tmp]#head -n -4 /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
[root@centos6_test tmp]#cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#

    tail:将指定文件的最后10行显示到标准输出。

用法:

tail [选项]... [文件]...

选项:

长选项必须使用的参数对于短选项时也是必需使用的。
  -c, --bytes=K		输出最后K 字节;另外,使用-c +K 从每个文件的
			第K 字节输出
  -f, --follow[={name|descriptor}]
		即时输出文件变化后追加的数据。
			-f, --follow 等于--follow=descriptor 
  -F		即--follow=name --retry
  -n, --lines=K            output the last K lines, instead of the last 10;
                           or use -n +K to output lines starting with the Kth
      --max-unchanged-stats=N
                           with --follow=name, reopen a FILE which has not
                           changed size after N (default 5) iterations
                           to see if it has been unlinked or renamed
                           (this is the usual case of rotated log files).
                           With inotify, this option is rarely useful.
      --pid=PID		同 -f 一起使用,当 PID 所对应的进程死去后终止
  -q, --quiet, --silent	不输出给出文件名的头
      --retry		即使目标文件不可访问依然试图打开;在与参数
			--follow=name 同时使用时常常有用。
  -s, --sleep-interval=N   with -f, sleep for approximately N seconds
                             (default 1.0) between iterations.
                           With inotify and --pid=P, check process P at
                           least once every N seconds.
  -v, --verbose            always output headers giving file names
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

常用演示:

[root@centos6_test tmp]#tail fstab 
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#tail -n 3 fstab 
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

说明:tail -f 可以监控某个文件的内容变化,通常我们用来监控某些日志文件的变化,和它类似的命令有tailf

    stat:查看文件或文件系统的状态信息。

用法:

stat [选项]... 文件...

选项:

-L, --dereference     follow links
  -Z, --context         print the SELinux security context 
  -f, --file-system     display file system status instead of file status
  -c --format=格式	使用指定输出格式代替默认值,每用一次指定格式换一新行
      --printf=格式	类似 --format,但是会解释反斜杠转义符,不使用换行作
				输出结尾。如果您仍希望使用换行,可以在格式中
				加入"\n"
  -t, --terse		使用简洁格式输出
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

常用演示:

[root@centos6_test tmp]#stat /etc/fstab 
  File: "/etc/fstab"
  Size: 779       	Blocks: 8          IO Block: 4096   普通文件
Device: fd00h/64768d	Inode: 654087      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-11 12:37:07.236000094 +0800
Modify: 2019-09-19 08:46:05.221999997 +0800
Change: 2019-09-19 08:58:21.402999996 +0800

2、使用命令行展开功能,创建/tmp/a1,/tmp/a2,/tmp/a1/a,/tmp/a1/b,在/tmp目录下创建目录:x_y,x_z,q_y,q_z

[root@centos6_test ~]#mkdir -pv /tmp/a{1/{a,b},2}
mkdir: 已创建目录 "/tmp/a1"
mkdir: 已创建目录 "/tmp/a1/a"
mkdir: 已创建目录 "/tmp/a1/b"
mkdir: 已创建目录 "/tmp/a2"
[root@centos6_test ~]#mkdir -pv /tmp/{x_,q_}{y,z}
mkdir: 已创建目录 "/tmp/x_y"
mkdir: 已创建目录 "/tmp/x_z"
mkdir: 已创建目录 "/tmp/q_y"
mkdir: 已创建目录 "/tmp/q_z"
[root@centos6_test ~]#

3、文件的元数据信息都有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息。

     文件的数据分两种,一种是文件的元数据,一种是文件数据内容本身,查看文件的元数据可以用stat命令来查看。

比如查看 /etc/issue 这个文件的元数据,可以用stat /etc/issue命令来查看如下:

[root@centos6_test tmp]#stat /etc/issue
  File: "/etc/issue"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 659335      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 11:41:22.599000002 +0800
Modify: 2019-10-01 14:30:47.741993885 +0800
Change: 2019-10-01 14:30:47.741993885 +0800
[root@centos6_test tmp]#

说明:File:表示文件名

Size:文件的大小

Blocks:文件占用了多少数据块

IO Block:文件所占的数据块大小(一个数据块的大小)

Device:文件所在硬盘的那个柱面

Inode:文件的节点编号

Links:文件的连接数

Access:文件权限及最近访问时间

Uid:文件的所有者的UID

GId:文件的所属组的GID

Modify:文件最近修改时间

Change:文件最近变化时间(属性最近变化时间)

修改文件的时间戳信息用touch命令来修改

touch -a 修改文件的访问时间(atime)或者touch --time=atime 文件

touch -m 修改文件的mtime 或者 touch --time=mtime 文件

也可以用touch -t "时间字符串" 文件 或者touch -d "时间字符串" 文件 来修改,值得注意的是时间字符串的格式是[[CC]YY]MMDDhhmm[.ss],其中CC表示世纪,YY表示年分,MM表示月份,DD表示天,hh表示小时,mm表示分钟,ss表示秒钟。如果使用-d 来指定时间字符串,其格式可以写成 "YYYY-MM-DD hh:mm:ss" 就是我们通常看到的时间格式。

[root@centos6_test tmp]#date
2019年 10月 10日 星期四 15:43:49 CST
[root@centos6_test tmp]#touch -a /etc/issue
[root@centos6_test tmp]#stat /etc/issue
  File: "/etc/issue"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 659335      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 15:43:55.998999825 +0800
Modify: 2019-10-01 14:30:47.741993885 +0800
Change: 2019-10-10 15:43:55.998999825 +0800
[root@centos6_test tmp]#touch -m /etc/issue
[root@centos6_test tmp]#stat /etc/issue
  File: "/etc/issue"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 659335      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 15:43:55.998999825 +0800
Modify: 2019-10-10 15:44:17.930009460 +0800
Change: 2019-10-10 15:44:17.930009460 +0800
[root@centos6_test tmp]#touch -t '202112111354.30' /etc/issue
[root@centos6_test tmp]#stat /etc/issue
  File: "/etc/issue"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 659335      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-12-11 13:54:30.000000000 +0800
Modify: 2021-12-11 13:54:30.000000000 +0800
Change: 2019-10-10 15:45:23.832995022 +0800
[root@centos6_test tmp]#[root@centos6_test tmp]#touch -d '2011-10-18 16:43:23' /etc/issue
[root@centos6_test tmp]#stat /etc/issue
  File: "/etc/issue"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: fd00h/64768d	Inode: 659335      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2011-10-18 16:43:23.000000000 +0800
Modify: 2011-10-18 16:43:23.000000000 +0800
Change: 2019-10-10 15:58:22.092994693 +0800
[root@centos6_test tmp]#

  说明:用touch -t 或者-d指定时间来修改文件的时间戳信息,是把文件的atime和mtime修改成指定时间,而ctime不是修改成指定时间,ctime还是会记录当前文件属性发生变化的系统时间

4、在/tmp目录创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-05-27-09-32-22。

[root@centos6_test ~]#touch /tmp/tfile-$(date +%F-%H-%M-%S)
[root@centos6_test ~]#ls /tmp
tfile-2019-10-08-12-38-26
[root@centos6_test ~]#

5、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。

[root@centos6_test ~]#cp -a /etc/p*[^[:digit:]] /tmp/mytest1/
[root@centos6_test ~]#ll /tmp/mytest1/
总用量 104
drwxr-xr-x.  2 root root 4096 9月  19 08:58 pam.d
drwxr-xr-x.  3 root root 4096 9月  19 08:49 pango
-rw-r--r--.  1 root root 1615 10月  1 14:39 passwd
-rw-r--r--.  1 root root 1574 9月  19 08:58 passwd-
-rw-r--r--.  1 root root 1362 8月  23 2010 pbm2ppa.conf
drwxr-xr-x.  2 root root 4096 9月  19 08:53 pcmcia
-rw-r--r--.  1 root root 2872 8月  21 2010 pinforc
drwxr-xr-x. 10 root root 4096 9月  19 08:52 pki
drwxr-xr-x.  2 root root 4096 9月  19 08:50 plymouth
drwxr-xr-x.  5 root root 4096 9月  19 08:46 pm
-rw-r--r--.  1 root root  370 10月 15 2014 pm-utils-hd-apm-restore.conf
-rw-r--r--.  1 root root 6300 8月  23 2010 pnm2ppa.conf
drwxr-xr-x.  2 root root 4096 8月  21 2010 popt.d
drwxr-xr-x.  2 root root 4096 9月  19 08:50 portreserve
drwxr-xr-x.  2 root root 4096 9月  19 08:52 postfix
drwxr-xr-x.  3 root root 4096 9月  19 08:52 ppp
-rw-r--r--.  1 root root  789 7月  19 2013 prelink.conf
drwxr-xr-x.  2 root root 4096 8月  19 2013 prelink.conf.d
-rw-r--r--.  1 root root  233 1月  12 2010 printcap
-rw-r--r--.  1 root root 1841 3月  22 2017 profile
drwxr-xr-x.  2 root root 4096 10月  1 14:06 profile.d
-rw-r--r--.  1 root root 6455 1月  12 2010 protocols
drwxr-xr-x.  2 root root 4096 9月  19 08:52 pulse
drwxr-xr-x.  2 root root 4096 9月  19 08:49 purple
[root@centos6_test ~]#

6、创建用户tom,指定UID为5001,指定家目录为/tmp/tom,指定shell为/bin/zsh,指定基本组为tom,附加组为jack。

[root@centos6_test ~]#useradd -u5001 -d /tmp/tom -s /bin/zsh -g tom -G jack tom
[root@centos6_test ~]#getent passwd tom
tom:x:5001:501::/tmp/tom:/bin/zsh
[root@centos6_test ~]#id tom
uid=5001(tom) gid=501(tom) 组=501(tom),502(jack)
[root@centos6_test ~]#

7、常用的用户以及文件管理命令有哪些,并演示命令以及用法。

  • 用户的增加

    useradd:添加用户

      用法:useradd [选项] 用户名

      选项:

       -b, --base-dir BASE_DIR 新账户的主目录的基目录

       -c, --comment COMMENT 新账户的 GECOS 字段

       -d, --home-dir HOME_DIR 新账户的主目录

       -D, --defaults 显示或更改默认的 useradd 配置

       -e, --expiredate EXPIRE_DATE 新账户的过期日期 

       -f, --inactive INACTIVE 新账户的密码不活动期

       -g, --gid GROUP 新账户主组的名称或 ID

       -G, --groups GROUPS 新账户的附加组列表

       -h, --help 显示此帮助信息并推出

       -k, --skel SKEL_DIR 使用此目录作为骨架目录

       -K, --key KEY=VALUE 不使用 /etc/login.defs 中的默认值

       -l, --no-log-init 不要将此用户添加到最近登录和登录失败数据库

       -m, --create-home 创建用户的主目录

       -M, --no-create-home 不创建用户的主目录

       -N, --no-user-group 不创建同名的组

       -o, --non-unique 允许使用重复的 UID 创建用户

       -p, --password PASSWORD 加密后的新账户密码

       -r, --system 创建一个系统账户

       -R, --root CHROOT_DIR chroot 到的目录

       -s, --shell SHELL 新账户的登录 shell

       -u, --uid UID 新账户的用户 ID

       -U, --user-group 创建与用户同名的组

       -Z, --selinux-user SEUSER 为 SELinux 用户映射使用指定 SEUSER

常用演示:

[root@centos6_test ~]#useradd -b /tmp/test/ -c 'test' -d /tmp/test test
useradd:警告:此主目录已经存在。
不从 skel 目录里向其中复制任何文件。
[root@centos6_test ~]#getent passwd test
test:x:5002:5002:test:/tmp/test:/bin/bash
[root@centos6_test ~]#useradd -b /tmp/xxx -c 'xxxx' -d /tmp/xxx xxx
[root@centos6_test ~]#getent passwd xxx
xxx:x:5003:5003:xxxx:/tmp/xxx:/bin/bash
[root@centos6_test ~]#useradd -g 500 -u 901 -G 5002 -r -M -s /sbin/nologin ooo
[root@centos6_test ~]#getent passwd ooo
ooo:x:901:500::/home/ooo:/sbin/nologin
[root@centos6_test ~]#ll /home/
总用量 4
drwx------. 4 qiuhom qiuhom 4096 10月 12 00:15 qiuhom
[root@centos6_test ~]#id ooo
uid=901(ooo) gid=500(qiuhom) 组=500(qiuhom),5002(test)
[root@centos6_test ~]#
  • 用户的删除

    userdel:删除用户

      用法:userdel [选项] 用户名

      选项:

         -f, --force force some actions that would fail otherwise

                     e.g. removal of user still logged in 

                     or files, even if not owned by the user

         -h, --help 显示此帮助信息并推出

         -r, --remove 删除主目录和邮件池

         -R, --root CHROOT_DIR chroot 到的目录

         -Z, --selinux-user 为用户删除所有的 SELinux 用户映射

常用演示:

[root@centos6_test ~]#getent passwd xxx
xxx:x:902:902::/home/xxx:/bin/bash
[root@centos6_test ~]#ll /home
总用量 8
drwx------. 4 qiuhom qiuhom 4096 10月 12 00:15 qiuhom
drwx------  4 xxx    xxx    4096 10月 12 00:55 xxx
[root@centos6_test ~]#userdel -r xxx
userdel:/var/spool/mail/xxx 并不属于 xxx,所以不会删除
[root@centos6_test ~]#ll /home/
总用量 4
drwx------. 4 qiuhom qiuhom 4096 10月 12 00:15 qiuhom
[root@centos6_test ~]#id xxx
id: xxx:无此用户
[root@centos6_test ~]#

    chage:修改账号和密码都有效期限

      用法:chage [选项] 用户名

      选项:

        -d, --lastday 最近日期 将最近一次密码设置时间设为“最近日期”

        -E, --expiredate 过期日期 将帐户过期时间设为“过期日期”

        -h, --help 显示此帮助信息并推出

        -I, --inactive INACITVE 过期 INACTIVE 天数后,设定密码为失效状态(大写i)

        -l, --list 显示帐户年龄信息(小写L)

        -m, --mindays 最小天数 将两次改变密码之间相距的最小天数设为“最小天数”

        -M, --maxdays 最大天数 将两次改变密码之间相距的最大天数设为“最大天数”

        -R, --root CHROOT_DIR chroot 到的目录

        -W, --warndays 警告天数 将过期警告天数设为“警告天数”

常用演示:

[root@centos6_test ~]#chage -l qiuhom
最近一次密码修改时间					:10月 11, 2019
密码过期时间					:从不
密码失效时间					:从不
帐户过期时间						:从不
两次改变密码之间相距的最小天数		:0
两次改变密码之间相距的最大天数		:99999
在密码过期之前警告的天数	:7
[root@centos6_test ~]#chage -m 3 qiuhom
[root@centos6_test ~]#chage -l qiuhom
最近一次密码修改时间					:10月 11, 2019
密码过期时间					:从不
密码失效时间					:从不
帐户过期时间						:从不
两次改变密码之间相距的最小天数		:3
两次改变密码之间相距的最大天数		:99999
在密码过期之前警告的天数	:7
[root@centos6_test ~]#chage -M 42 qiuhom
[root@centos6_test ~]#chage -l qiuhom
最近一次密码修改时间					:10月 11, 2019
密码过期时间					:11月 22, 2019
密码失效时间					:从不
帐户过期时间						:从不
两次改变密码之间相距的最小天数		:3
两次改变密码之间相距的最大天数		:42
在密码过期之前警告的天数	:7
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:$6$xVEe.VPn$j0KZB5OJLuoI49C5kEbWpsW8Lr3y3uFBXt6JYfOj9iysl9rVN0ohG9aezKIpuhd6mv5fMij3qBksjytjbm35B/:18180:0:99999:7:::
root@centos6_test ~]#chage -I 5 qiuhom
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:$6$xVEe.VPn$j0KZB5OJLuoI49C5kEbWpsW8Lr3y3uFBXt6JYfOj9iysl9rVN0ohG9aezKIpuhd6mv5fMij3qBksjytjbm35B/:18180:3:42:2:5::
[root@centos6_test ~]#chage -W 3 qiuhom
[root@centos6_test ~]#chage -l qiuhom
最近一次密码修改时间					:10月 11, 2019
密码过期时间					:11月 22, 2019
密码失效时间					:11月 27, 2019
帐户过期时间						:从不
两次改变密码之间相距的最小天数		:3
两次改变密码之间相距的最大天数		:42
在密码过期之前警告的天数	:3
[root@centos6_test ~]#chage -d 2019-10-09 qiuhom
[root@centos6_test ~]#chage -l qiuhom
最近一次密码修改时间					:10月 09, 2019
密码过期时间					:11月 20, 2019
密码失效时间					:11月 25, 2019
帐户过期时间						:从不
两次改变密码之间相距的最小天数		:3
两次改变密码之间相距的最大天数		:42
在密码过期之前警告的天数	:3
[root@centos6_test ~]#chage -E 2022-12-12 qiuhom
[root@centos6_test ~]#chage -l qiuhom
最近一次密码修改时间					:10月 09, 2019
密码过期时间					:11月 20, 2019
密码失效时间					:11月 25, 2019
帐户过期时间						:12月 12, 2022
两次改变密码之间相距的最小天数		:3
两次改变密码之间相距的最大天数		:42
在密码过期之前警告的天数	:3
  • 用户的查看

    id:显示当前用户或指定用户的用户与组的信息。

用法:

id [选项]... [用户名]

选项:

-a			忽略,仅为与其他版本相兼容而设计
  -Z, --context		仅显示当前用户的安全环境
  -g, --group		仅显示有效的用户组ID
  -G, --groups		显示所有组的ID
  -n, --name		显示组名称而非数字,不与-ugG 一起使用
  -r, --real		显示真实ID 而非有效ID,与-ugG 一起使用
  -u, --user		仅显示有效用户ID
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

 常用演示:

[root@centos6_test ~]#id
uid=0(root) gid=0(root) 组=0(root)
[root@centos6_test ~]#id -u
0
[root@centos6_test ~]#id -g
0
[root@centos6_test ~]#id -G
0
[root@centos6_test ~]#id -un
root
[root@centos6_test ~]#id -gn
root
[root@centos6_test ~]#id -Gn
root
[root@centos6_test ~]#id -Gn tom
tom jack

    getent:查询数据库中的内容

用法:

getent [选项...] 数据库 [键 ...]

选项: 

-s, --service=CONFIG       要使用的服务配置
  -?, --help                 给出该系统求助列表
      --usage                给出简要的用法信息
  -V, --version              打印程序版本号

 常用演示:

[root@centos6_test ~]#getent passwd qiuhom
qiuhom:x:500:500::/home/qiuhom:/bin/bash
[root@centos6_test ~]#getent group qiuhom
qiuhom:x:500:
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:$6$epxVKdJN$OWp0tnT2zvFP26rMKuqEZizxUo3HaDqDTO.YHgCpoaIp7jqA4WtblmtH3.4UjICZFKIvJLQzxIB8t9lXoESdV1:18170:0:99999:7:::

说明:在Linux系统里,数据库默认就是以文本的形式展示给用户。如上演示,/etc/passwd、/etc/shadow 等都是一个个数据库

  • 用户的修改

    usermod:修改用户信息

用法:

usermod [选项] 用户

选项:

-c, --comment 注释            GECOS 字段的新值
  -d, --home HOME_DIR           用户的新主目录
  -e, --expiredate EXPIRE_DATE  设定帐户过期的日期为 EXPIRE_DATE
  -f, --inactive INACTIVE       过期 INACTIVE 天数后,设定密码为失效状态
  -g, --gid GROUP               强制使用 GROUP 为新主组
  -G, --groups GROUPS           新的附加组列表 GROUPS
  -a, --append GROUP            将用户追加至上边 -G 中提到的附加组中,
                                并不从其它组中删除此用户
  -h, --help                    显示此帮助信息并推出
  -l, --login LOGIN             新的登录名称
  -L, --lock                    锁定用户帐号
  -m, --move-home               将家目录内容移至新位置 (仅于 -d 一起使用)
  -o, --non-unique              允许使用重复的(非唯一的) UID
  -p, --password PASSWORD       将加密过的密码 (PASSWORD) 设为新密码
  -R, --root CHROOT_DIR         chroot 到的目录
  -s, --shell SHELL             该用户帐号的新登录 shell
  -u, --uid UID                 用户帐号的新 UID
  -U, --unlock                  解锁用户帐号
  -Z, --selinux-user  SEUSER       用户账户的新 SELinux 用户映射

 常用演示:

[root@centos6_test ~]#tail -4 /etc/passwd
tom:x:5001:501::/tmp/tom:/bin/zsh
test:x:5002:5002:test:/tmp/test:/bin/bash
xxx:x:5003:5003:xxxx:/tmp/xxx:/bin/bash
ooo:x:901:500::/home/ooo:/sbin/nologin
[root@centos6_test ~]#usermod -c "test usermod" -d /tmp/test/ -s /bin/csh test
[root@centos6_test ~]#getent passwd test
test:x:5002:5002:test usermod:/tmp/test/:/bin/csh
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:$6$xVEe.VPn$j0KZB5OJLuoI49C5kEbWpsW8Lr3y3uFBXt6JYfOj9iysl9rVN0ohG9aezKIpuhd6mv5fMij3qBksjytjbm35B/:18180:0:99999:7:::
[root@centos6_test ~]#usermod -L qiuhom
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:!$6$xVEe.VPn$j0KZB5OJLuoI49C5kEbWpsW8Lr3y3uFBXt6JYfOj9iysl9rVN0ohG9aezKIpuhd6mv5fMij3qBksjytjbm35B/:18180:0:99999:7:::
[root@centos6_test ~]#usermod -U qiuhom
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:$6$xVEe.VPn$j0KZB5OJLuoI49C5kEbWpsW8Lr3y3uFBXt6JYfOj9iysl9rVN0ohG9aezKIpuhd6mv5fMij3qBksjytjbm35B/:18180:0:99999:7:::
[root@centos6_test ~]#usermod -l 'abc' test
[root@centos6_test ~]#getent passwd testt
[root@centos6_test ~]#getent passwd test
[root@centos6_test ~]#tail -4 /etc/passwd
tom:x:5001:501::/tmp/tom:/bin/zsh
xxx:x:5003:5003:xxxx:/tmp/xxx:/bin/bash
ooo:x:901:500::/home/ooo:/sbin/nologin
abc:x:5002:5002:test usermod:/tmp/test/:/bin/csh
[root@centos6_test ~]#

    passwd:设置用户的认证信息,包括用户密码、密码过期时间等。 

用法:

passwd [选项...] <帐号名称>

选项:

-k, --keep-tokens       保持身份验证令牌不过期
  -d, --delete            删除已命名帐号的密码(只有根root才能进行此操作)
  -l, --lock              lock the password for the named account (root only)
  -u, --unlock            unlock the password for the named account (root only)
  -e, --expire            expire the password for the named account (root only)
  -f, --force             强制执行操作
  -x, --maximum=DAYS      密码的最长有效时限(只有root才能进行此操作)
  -n, --minimum=DAYS      密码的最短有效时限(只有root才能进行此操作)
  -w, --warning=DAYS      在密码过期前多少天开始提醒用户(只有root才能进行此操作)
  -i, --inactive=DAYS     当密码过期后经过多少天该帐号会被禁用(只有root才能进行此操作)
  -S, --status            报告已命名帐号的密码状态(只有root才能进行此操作)
  --stdin                 从标准输入读取令牌(只有root才能进行此操作)

 常用演示:

[root@centos6_test ~]#passwd
更改用户 root 的密码 。
新的 密码:
无效的密码: 过短
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[root@centos6_test ~]#passwd qiuhom
更改用户 qiuhom 的密码 。
新的 密码:
无效的密码: 过短
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[root@centos6_test ~]#echo 'admin'|passwd --stdin qiuhom
更改用户 qiuhom 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
[root@centos6_test ~]#passwd -S tom
tom LK 2019-10-10 0 99999 7 -1 (密码已被锁定。)
[root@centos6_test ~]#passwd -S qiuhom
qiuhom PS 2019-10-11 0 99999 7 -1 (密码已设置,使用 SHA512 加密。)
[root@centos6_test ~]#passwd -l qiuhom
锁定用户 qiuhom 的密码 。
passwd: 操作成功
[root@centos6_test ~]#passwd -u qiuhom
解锁用户 qiuhom 的密码 。
passwd: 操作成功
[root@centos6_test ~]#
  • 用户切换

    su:switch user 

用法:

su [选项]... [-] [用户 [参数]... ]

选项:

-f 或 --fast 不必读启动档(如 csh.cshrc 等),仅用于 csh 或 tcsh
-m -p 或 --preserve-environment 执行 su 时不改变环境变数
-c command 或 --command=command 变更为帐号为 USER 的使用者并执行指令(command)后再变回原来使用者
-s shell 或 --shell=shell 指定要执行的 shell (bash csh tcsh 等),预设值为 /etc/passwd 内的该使用者(USER) shell
--help 显示说明文件
--version 显示版本资讯
- -l 或 --login 这个参数加了之后,就好像是重新 login 为该使用者一样,大部份环境变数(HOME SHELL USER等等)都是以该使用者(USER)为主,并且工作目录也会改变,如果没有指定 USER ,内定是 root
USER 欲变更的使用者帐号
ARG 传入新的 shell 参数

 常用演示:

[root@centos6_test ~]#whoami
root
[root@centos6_test ~]#pwd
/root
[root@centos6_test ~]#su qiuhom
[qiuhom@centos6_test root]$whoami
qiuhom
[qiuhom@centos6_test root]$pwd
/root
[qiuhom@centos6_test root]$exit
exit
[root@centos6_test ~]#su - qiuhom
[qiuhom@centos6_test ~]$whoami
qiuhom
[qiuhom@centos6_test ~]$pwd
/home/qiuhom
[qiuhom@centos6_test ~]$exit
logout
[root@centos6_test ~]#su -s /bin/csh - qiuhom
[qiuhom@centos6_test ~]$ echo $SHELL
/bin/csh
[qiuhom@centos6_test ~]$ exit
logout
[root@centos6_test ~]#su -c 'ls' qiuhom
ls: 无法打开目录.: 权限不够
[root@centos6_test ~]#su -c 'ls' - qiuhom
[root@centos6_test ~]#cp /tmp/
.esd-0/    fstab      .ICE-unix/ 
[root@centos6_test ~]#cp /tmp/* /home/qiuhom/
[root@centos6_test ~]#su -c 'ls -al' - qiuhom
total 40
drwx------. 4 qiuhom qiuhom 4096 Oct 12 00:15 .
drwxr-xr-x. 3 root   root   4096 Oct  1 14:39 ..
-rw-------  1 qiuhom qiuhom   37 Oct 12 00:13 .bash_history
-rw-r--r--. 1 qiuhom qiuhom   18 Mar 23  2017 .bash_logout
-rw-r--r--. 1 qiuhom qiuhom  176 Mar 23  2017 .bash_profile
-rw-r--r--. 1 qiuhom qiuhom  124 Mar 23  2017 .bashrc
-rw-r--r--  1 root   root    779 Oct 12 00:15 fstab
drwxr-xr-x. 2 qiuhom qiuhom 4096 Nov 12  2010 .gnome2
-rw-------  1 qiuhom qiuhom   43 Oct 12 00:14 .history
drwxr-xr-x. 4 qiuhom qiuhom 4096 Sep 19 08:46 .mozilla
[root@centos6_test ~]#whoami
root
[root@centos6_test ~]#pwd
/root
[root@centos6_test ~]# 
  • 用户账户信息和密码信息配置文件

     /etc/passwd:存储用户都用户名、用户ID、用户密码标志、用户uid、用户gid、用户说明信息、用户家目录信息、用户shell类型;

    /etc/shadow:存储用户名、用户密码、用户最后一次修改密码都时间,这个时间是距离1970年1月1日经过都天数。接下来是最小修改密码时间间隔,表示从最近修改日期算起,至少要经过多少天才能更改密码,如果是0,表示随时可以修改。下一个字段是密码都有效期限,表示从最近一次修改密码算经过多长时间,密码过期,必须修改,否则账户会过期。接下来是密码需要变更前都警告天数,表示提前多少天告知用户需要更改密码了。下一个字段是密码过期后宽限天数,表示密码过期了,还可以有几天可以登陆系统。账号失效时间,表示到了这个时间账号将无法使用,不管密码是否过期。下一个字段保留。

    /etc/group:存储用户组名、组密码标志、组id、组中的用户

    /etc/gshadow:存储用户组名、组密码、用户管理员的名称、支持的账号名称

    /etc/default/useradd:定义用户默认创建用户时的默认配置,

      其中GROUP=100 表示如果useradd没有指定组,并且/etc/login.defs中的USERGROUPS_ENAB为no或者useradd使用了-N选项时,此时该参数生效。创建用户时使用此组ID。

          HOME=/home  主目录放在什么目录下

          INACTIVE=-1  帐号是否过期

          EXPIRE=  帐号终止日期

          SHELL=/bin/bash  默认使用哪个shell

          SKEL=/etc/skel  模板目录,骨架目录

          CREATE_MAIL_SPOOL=yes  是否创建邮箱文件

 

    /etc/login.defs:设置用户帐号限制的文件,在这里我们可配置密码的最大过期天数,密码的最大长度约束等内容。该文件里的配置对root用户 无效。如果/etc/shadow文件里有相同的选项,则以/etc/shadow里的设置为准,也就是说/etc/shadow的配置优先级高于 /etc/login.defs  

posted @ 2019-10-12 15:59  Linux-1874  阅读(937)  评论(0编辑  收藏  举报