第二章 文件和目录操作命令

2.1 pwd

(print work directory)打印工作目录(显示当前所在路径)  后面可以接 -L  默认情况下接的也是-L(logical)的 此种情况显示的是逻辑路径(相当于win的一样) -P(physical)的话,就会把真实的物理路径全部显示出来

[root@oldbody local]# pwd

/usr/local

[root@oldbody local]# cd -

/root

[root@oldbody ~]#

[root@oldbody home]# pwd

/home

[root@oldbody home]# echo $PWD

/home

2.2 cd

(change directory)改变目录路径 例子:cd /etc 后面的参数-L和-P的话,一个是逻辑的,一个是物理的

绝对路径:从/开始的路径 /root/test/a/b    

相对路径:不从/开始的路径 test/a/b

cd ../..    上上级目录    cd ../../..  上上上级目录    cd /  直接到/目录下    cd -   上一次目录   其实 -   是因为有一个$OLDPWD这个环境变量  

cd ~  代表家目录  其实 因为有一个$HOME这个环境变量 

cd ..  退回上一级目录  如果是一个点的话代表当前目录

比如[root@oldbody oldboy]# pwd

/oldboy

[root@oldbody oldboy]# cd /tmp/

[root@oldbody tmp]# cd -

/oldboy

[root@oldbody oldboy]# env | grep -i oldpwd     因为你在oldboy目录下 所以你的环境变量OLDPWD=/tmp

OLDPWD=/tmp

[root@oldbody oldboy]#

[root@oldbody oldboy]# cd -

/tmp

[root@oldbody tmp]# env | grep -i oldpwd       因为你在tmp目录下 所以你的环境变量OLDPWD=/oldboy 其中-i就是不区分大小写的

OLDPWD=/oldboy

[root@oldbody tmp]#

2.3 tree

tree   树的意思  以树的形式来显示一个目录结构 如果tree不接任何参数的话,会显示所有的tree的目录结构 在Linux系统里面以 以.开头的都是隐藏文件 使用 ls命令查看不出来 必须使用ls -a参数进行查看

tree -a 查看隐藏文件

tree -d代表只显示目录     

tree -L其中L代表level层级的意思 显示指定的层级

tree -i 不显示树枝 常与-f参数配合使用

tree -f 显示每个文件的全路径

tree -F 用以区分文件和目录的方法 以/结尾的为目录

可以用rpm -qa tree命令查看下tree是否安装 如果tree没有安装,可以使用yum install tree -y 安装tree软件包  LANG=en   调整字符集 为英文  然后输入tree /liangli2/ 可以看到

[root@oldbody /]# tree /liangli2/

/liangli2/

`-- liangli3

 

1 directory, 0 files

 

[root@oldbody key]# tree

.

|-- 1

|-- key1

|   |-- 4

|   `-- 5

|-- key2

|   |-- 4

|   `-- 5

`-- key3

    |-- 4

    `-- 5

 

10 directories, 0 files

[root@oldbody key]# tree -L 1

.

|-- 1

|-- key1

|-- key2

`-- key3

 

4 directories, 0 files

[root@oldbody key]#

 

[root@oldbody key]# tree -dL 1

.

|-- 1

|-- key1

|-- key2

`-- key3

 

4 directories

[root@oldbody key]#

为每一个路径显示完整的路径   f    full   完整

[root@oldbody key]# tree -dLf 1

.

|-- ./1

|-- ./key1

|-- ./key2

`-- ./key3

 

4 directories

[root@oldbody key]#

 

[root@oldbody key]# tree -dLfi 1  indentation压痕 这个点的话,tree后面可以接路径点消失

不要前面的树枝

[root@oldbody sysconfig]# tree -L 1 -fi /boot/

/boot

/boot/System.map-2.6.32-573.el6.x86_64

/boot/config-2.6.32-573.el6.x86_64

/boot/efi

/boot/grub

/boot/initramfs-2.6.32-573.el6.x86_64.img

/boot/lost+found

/boot/symvers-2.6.32-573.el6.x86_64.gz

/boot/vmlinuz-2.6.32-573.el6.x86_64

 

3 directories, 5 files

[root@oldbody sysconfig]#

区分文件和目录

[root@oldbody key]# tree -F

.

|-- 1/

|-- key1/

|   |-- 4/

|   `-- 5/

|-- key2/

|   |-- 4/

|   `-- 5/

`-- key3/

    |-- 4/

    `-- 5/

 

10 directories, 0 files

[root@oldbody key]#

2.4 mkdir

windows下的路径样式为c:\data\test 而Linux下的路径样式为/data/test,不同的是windows系统下还有D,E等盘,Linux下就只有/,它是所有目录的顶点    (macke directorys)创建目录  例子:mkdir /data  在根/下创建data目录   

-p 参数  如果文件存在 在创建文件话,接-p参数不会报错的  另一个作用就是 可以连续创建多级目录   

-v 参数作用 就是可以看到创建目录的流程是怎么样的     

或者cd /;mkdir data  切换到根下,然后创建data目录 其中;就是命令的分隔符    

连续创建目录  mkdir -p /liangli/liangli1  创建2个目录 一个是liangli一个是liangli1  其中liangli1在liangli目录下

[root@oldbody /]# mkdir -pv Tech/{1..3}/{4..6}

mkdir: created directory `Tech/1'

mkdir: created directory `Tech/1/4'

mkdir: created directory `Tech/1/5'

mkdir: created directory `Tech/1/6'

mkdir: created directory `Tech/2'

mkdir: created directory `Tech/2/4'

mkdir: created directory `Tech/2/5'

mkdir: created directory `Tech/2/6'

mkdir: created directory `Tech/3'

mkdir: created directory `Tech/3/4'

mkdir: created directory `Tech/3/5'

mkdir: created directory `Tech/3/6'

 

[root@oldbody /]# tree /tmp

/tmp

`-- Tech

    |-- 1

    |   |-- 4

    |   |-- 5

    |   `-- 6

    |-- 2

    |   |-- 4

    |   |-- 5

    |   `-- 6

    `-- 3

        |-- 4

        |-- 5

        `-- 6

 

13 directories, 0 files

 

[root@oldbody a]# mkdir -pv  /e/b/c/d/e/f

mkdir: created directory `/e'

mkdir: created directory `/e/b'

mkdir: created directory `/e/b/c'

mkdir: created directory `/e/b/c/d'

mkdir: created directory `/e/b/c/d/e'

mkdir: created directory `/e/b/c/d/e/f'

[root@oldbody a]#      

 

一下子创建连续5个dir1  dir2  dir3  dir4  dir5目录了

[root@oldbody /]# mkdir /Tech/dir{1..5}

[root@oldbody /]#

[root@oldbody /]#

[root@oldbody /]# ls /Tech/

dir1  dir2  dir3  dir4  dir5  liangli.txt  oldboy.txt

同时创建多个目录

[root@oldbody tmp]# mkdir -p test/dir{1..5} oldboy/{a..g}

[root@oldbody tmp]# tree test oldboy/

test

|-- dir1

|-- dir2

|-- dir3

|-- dir4

`-- dir5

oldboy/

|-- a

|-- b

|-- c

|-- d

|-- e

|-- f

`-- g

 

12 directories, 0 files

[root@oldbody tmp]#

克隆目录结构    

[root@oldbody ~]# tree -if liangli2018/      

liangli2018

liangli2018/dir1

liangli2018/dir1/4

liangli2018/dir1/5

liangli2018/dir1/6

liangli2018/dir1/7

liangli2018/dir2

liangli2018/dir2/4

liangli2018/dir2/5

liangli2018/dir2/6

liangli2018/dir2/7

liangli2018/dir3

liangli2018/dir3/4

liangli2018/dir3/5

liangli2018/dir3/6

liangli2018/dir3/7

 

15 directories, 0 files

 

[root@oldbody ~]# tree -if liangli2018/ --noreport liangli2018   --noreport不显示最后一行统计信息

liangli2018

liangli2018/dir1

liangli2018/dir1/4

liangli2018/dir1/5

liangli2018/dir1/6

liangli2018/dir1/7

liangli2018/dir2

liangli2018/dir2/4

liangli2018/dir2/5

liangli2018/dir2/6

liangli2018/dir2/7

liangli2018/dir3

liangli2018/dir3/4

liangli2018/dir3/5

liangli2018/dir3/6

liangli2018/dir3/7

liangli2018

liangli2018/dir1

liangli2018/dir1/4

liangli2018/dir1/5

liangli2018/dir1/6

liangli2018/dir1/7

liangli2018/dir2

liangli2018/dir2/4

liangli2018/dir2/5

liangli2018/dir2/6

liangli2018/dir2/7

liangli2018/dir3

liangli2018/dir3/4

liangli2018/dir3/5

liangli2018/dir3/6

liangli2018/dir3/7

 

[root@oldbody ~]# tree -if liangli2018/ --noreport liangli2018 >oldboy.txt

[root@oldbody ~]#

[root@oldbody ~]#

[root@oldbody ~]# cat oldboy.txt

liangli2018          这个是必须存在的目录

liangli2018/dir1

liangli2018/dir1/4

liangli2018/dir1/5

liangli2018/dir1/6

liangli2018/dir1/7

liangli2018/dir2

liangli2018/dir2/4

liangli2018/dir2/5

liangli2018/dir2/6

liangli2018/dir2/7

liangli2018/dir3

liangli2018/dir3/4

liangli2018/dir3/5

liangli2018/dir3/6

liangli2018/dir3/7

liangli2018

liangli2018/dir1

liangli2018/dir1/4

liangli2018/dir1/5

liangli2018/dir1/6

liangli2018/dir1/7

liangli2018/dir2

liangli2018/dir2/4

liangli2018/dir2/5

liangli2018/dir2/6

liangli2018/dir2/7

liangli2018/dir3

liangli2018/dir3/4

liangli2018/dir3/5

liangli2018/dir3/6

liangli2018/dir3/7

[root@oldbody ~]# cd /tmp

[root@oldbody tmp]# mkdir -p `cat /root/oldboy.txt`    首先执行``反引号里面的内容 然后在执行mkdir命令

[root@oldbody tmp]# ll

total 4

drwxr-xr-x 5 root root 4096 Sep 29 10:42 liangli2018

[root@oldbody tmp]# tree

.

`-- liangli2018

    |-- dir1

    |   |-- 4

    |   |-- 5

    |   |-- 6

    |   `-- 7

    |-- dir2

    |   |-- 4

    |   |-- 5

    |   |-- 6

    |   `-- 7

    `-- dir3

        |-- 4

        |-- 5

        |-- 6

        `-- 7

 

16 directories, 0 files

[root@oldbody tmp]#

2.5 touch

创建文件或更新时间戳 查看ls /Tech可以看到自己创建的文件了,如果文件不存在,就建立新文件,如果存在,就改变文件的访问时间atime等时间戳

连续创建10000个文件  touch stu{1..10000}  

可以同时创建2个文件  touch a.txt b.txt 两个文件   也可以用touch {1..4}.txt

通过命令可以看到访问(-a) 修改(-m) 改变(-c)文件的时间状态

[root@oldbody 1]# stat 1.txt

  File: `1.txt'

  Size: 0               Blocks: 0          IO Block: 4096   regular empty file

Device: 803h/2051d      Inode: 784979      Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2018-06-21 20:16:43.546093627 +0800

Modify: 2018-06-21 20:16:43.546093627 +0800

Change: 2018-06-21 20:16:43.546093627 +0800

[root@oldbody 1]#

[root@oldbody 1]# touch -a 1.txt 的话,表示只改变访问和改变的时间戳,不改变修改的时间戳  其中Change时间不管任何情况都是会改变的

touch -m 1.txt   只改变修改时间戳

[root@oldboy liangli]# stat  -c  %a  a.txt

644

[root@oldboy liangli]#

-c    使用指定的格式而不是默认的格式  %a 八进制权限

 

指定时间属性创建/修改文件(了解)

[root@oldbody tmp]# touch -d 20201001 1.txt    d指定创建文件后文件修改时间

[root@oldbody tmp]# ll -h 1.txt

-rw-r--r-- 1 root root 11 Oct  1  2020 1.txt

[root@oldbody tmp]# ll -h 2.txt

-rw-r--r-- 1 root root 0 Sep 29 10:57 2.txt

 

[root@oldbody tmp]# touch -r 2.txt 1.txt         r让其和其他文件时间属性保持一致

[root@oldbody tmp]# ll -h 1.txt          

-rw-r--r-- 1 root root 11 Sep 29 10:57 1.txt

 

[root@oldbody tmp]# touch -t 201809291110.00 1.txt    t设置文件格式

[root@oldbody tmp]# ll -h 1.txt                   

-rw-r--r-- 1 root root 11 Sep 29  2018 1.txt

[root@oldbody tmp]# ll -h --full-time  1.txt

-rw-r--r-- 1 root root 11 2018-09-29 11:10:00.000000000 +0800 1.txt

[root@oldbody tmp]#

2.6 ls

list(列表)  列表目录文件  例子:ls / 列 根/目录下目录和文件 

-l  long  长格式(显示出来的时间为最后一次修改时间)  

-d directorys 查看目录   

-a  all的意思  显示所有文件,包括隐藏的文件(默认.开头文件都是隐藏的 不显示)

-A 列出所有文件,包括隐藏文件,但是不包括.和..这两个是目录  

-t根据最后修改时间排序,默认从大到小(最新到最老)  

-r反转排序

--time-style=long-iso  显示完整时间属性参数(年份)

ls -lrt /etc 执行这个命令后最新的文件会在最下面  这条命令依赖于当前系统正确的时间

-F 区分文件和目录的方式(在结尾处加上类型指示符号 * / @等)了解

   加上“*”代表可执行的普通文件

   加上“/”表示目录

   加上“=”表示套接字(sockets)

   加上“|”表示FIFOs

   加上“@”表示符号链接

-p 也是区分文件和目录的方式(在结尾处给目录加上/)

[root@oldbody oldboy]# touch oldboy.txt

[root@oldbody oldboy]# ls -lrt     r是倒叙的意思    t按修改时间排序

total 20

-rw-r--r-- 1 root root    0 Jun 26 21:53 yingsui.gz

-rw-r--r-- 1 root root    0 Jun 26 21:53 wodi.gz

-rw-r--r-- 1 root root    0 Jun 26 21:53 oldboy

-rw-r--r-- 1 root root    0 Jun 26 21:53 jeacen

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xingfujie

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xiaodong

drwxr-xr-x 2 root root 4096 Jun 26 21:56 test

drwxr-xr-x 3 root root 4096 Jun 26 21:56 ext

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xiaofan

-rw-r--r-- 1 root root    0 Jun 26 23:40 oldboy.txt

[root@oldbody oldboy]#

查看系统时间

[root@oldbody ~]# date

Wed Jun 27 20:28:45 CST 2018

[root@oldbody ~]#

ls -l --color=auto  显示颜色

[root@oldbody oldboy]# alias

alias cp='cp -i'

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@oldbody oldboy]# ls -l   目录会显示颜色的

total 20

drwxr-xr-x 3 root root 4096 Jun 26 21:56 ext

-rw-r--r-- 1 root root    0 Jun 26 21:53 jeacen

-rw-r--r-- 1 root root    0 Jun 26 21:53 oldboy

-rw-r--r-- 1 root root    0 Jun 26 23:40 oldboy.txt

drwxr-xr-x 2 root root 4096 Jun 26 21:56 test

-rw-r--r-- 1 root root    0 Jun 26 21:53 wodi.gz

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xiaodong

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xiaofan

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xingfujie

-rw-r--r-- 1 root root    0 Jun 26 21:53 yingsui.gz

[root@oldbody oldboy]# \ls -l     屏蔽掉别名

total 20

drwxr-xr-x 3 root root 4096 Jun 26 21:56 ext

-rw-r--r-- 1 root root    0 Jun 26 21:53 jeacen

-rw-r--r-- 1 root root    0 Jun 26 21:53 oldboy

-rw-r--r-- 1 root root    0 Jun 26 23:40 oldboy.txt

drwxr-xr-x 2 root root 4096 Jun 26 21:56 test

-rw-r--r-- 1 root root    0 Jun 26 21:53 wodi.gz

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xiaodong

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xiaofan

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xingfujie

-rw-r--r-- 1 root root    0 Jun 26 21:53 yingsui.gz

[root@oldbody oldboy]#

[root@oldbody oldboy]# \ls -l --color=auto   目录会显示颜色的

total 20

drwxr-xr-x 3 root root 4096 Jun 26 21:56 ext

-rw-r--r-- 1 root root    0 Jun 26 21:53 jeacen

-rw-r--r-- 1 root root    0 Jun 26 21:53 oldboy

-rw-r--r-- 1 root root    0 Jun 26 23:40 oldboy.txt

drwxr-xr-x 2 root root 4096 Jun 26 21:56 test

-rw-r--r-- 1 root root    0 Jun 26 21:53 wodi.gz

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xiaodong

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xiaofan

drwxr-xr-x 2 root root 4096 Jun 26 21:56 xingfujie

-rw-r--r-- 1 root root    0 Jun 26 21:53 yingsui.gz

[root@oldbody oldboy]#

 

我们也可以给过滤的内容加颜色

[root@oldbody oldboy]# cat 123.log

key

[root@oldbody oldboy]# grep --color=auto key 123.log

key

[root@oldbody oldboy]#

给3306这个端口加上颜色

[root@oldbody oldboy]# grep --color=auto 3306 /etc/services

mysql           3306/tcp                        # MySQL

mysql           3306/udp                        # MySQL

[root@oldbody oldboy]#

 

ls -a  可以显示隐藏的文件和不隐藏的文件 all

[root@oldbody test]# ls -a

.  ..  dir1  dir2  dir3  file1.txt  file2.txt  file3.txt

 

ls -l   将文件的属性和修改时间都可以显示出来

ll就相当于ls -l一样  系统自带的一个alias

[root@oldbody test]# ls -l

total 12

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

[root@oldbody test]# ll

total 12

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

[root@oldbody test]# alias

alias cp='cp -i'

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@oldbody test]#

 

-h   human(人类) 的作用就是可以很直观的现实文件的大小 

[root@oldbody test]# ls -lh

total 12K

drwxr-xr-x 2 root root 4.0K Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4.0K Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4.0K Jun 28 11:48 dir3

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

[root@oldbody test]#

 

-d    directory参数表示只显示目录

[root@oldbody test]# ls -l | grep dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

[root@oldbody test]# ll -d dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

[root@oldbody test]#

 

-F   classify 就是给目录后面加上/

[root@oldbody test]# ls -F

dir1/  dir2/  dir3/  file1.txt  file2.txt  file3.txt

[root@oldbody test]#

 

生产案例:查找最近更新的文件  ll -rt     等于ls -lrt

-r   reverse 倒叙排序或者反向排序

-t   按修改时间排序

默认情况下  最新创建的文件和目录放在最前面

[root@oldbody test]# ll

total 12

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

[root@oldbody test]#

 

[root@oldbody test]# ll -rt

total 12

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

[root@oldbody test]#

-i  参数  查看inode节点

[root@oldbody test]# ll

total 12

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

[root@oldbody test]# ll -i

total 12

915788 drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

915789 drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

915790 drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

915785 -rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

915786 -rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

915787 -rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

[root@oldbody test]#

 

--time-style=long-iso 参数使用

[root@oldbody tmp]# ll --time-style=long-iso

total 12

-rw-r--r-- 1 root root   21 2018-09-29 11:39 1.txt

-rw-r--r-- 1 root root    0 2018-09-29 11:21 123.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 2.txt

drwxr-xr-x 2 root root 4096 2018-09-29 11:19 key

-rw-r--r-- 1 root root    0 2018-09-29 11:18 key.txt

drwxr-xr-x 5 root root 4096 2018-09-29 10:42 liangli2018

-rw-r--r-- 1 root root    0 2018-09-29 11:49 nihao.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 test1.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 test10.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 test2.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 test3.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 test4.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 test5.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 test6.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 test7.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 test8.txt

-rw-r--r-- 1 root root    0 2018-09-29 10:57 test9.txt

[root@oldbody tmp]#

2.7 cp  

不加参数的时候 只能拷贝文件

-a 相当于-dpr   

-d 若源文件为链接文件(link file),则复制链接文件属性而非档案本身  

-p 连同档案的属性一起复制过去,而非使用默认属性    

-r 递归 用于复制目录

假如tmp目录下有一个test.txt文件   mnt目录下有一个test.txt文件,现在将tmp目录下的test.txt文件复制到mnt目录下  默认情况下 会进行相应的提示

输入命令 \cp /mnt/test.txt /tmp/   不提示 别名的原因 \就是屏蔽别名

或者 /bin/cp /mnt/test.txt /tmp/    补全cp命令的路径也是不提示的  

因为cp vm rm属于危险的操作命令,有可能会对文件造成损坏,我们可以查看下系统现有的别名alias  可以输入命令unalias cp 直接删除cp的别名,也可以达到其效果 (不推荐使用)

cp命令   复制文件或目录

[root@oldbody test]# ll

total 12

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

[root@oldbody test]# cp file3.txt file4.txt

[root@oldbody test]# ll

total 12

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

-rw-r--r-- 1 root root    0 Jun 28 12:17 file4.txt

[root@oldbody test]#

cp    -a      -a等于-pdr      作用,复制过去的时候可以保证属性不变化  复制文件或目录保持文件或目录所有属性均不变化

[root@oldbody test]# ll

total 12

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

-rw-r--r-- 1 root root    0 Jun 28 12:17 file4.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file5.txt

[root@oldbody test]#

-i   覆盖前会提示

[root@oldbody test]# alias cp

alias cp='cp -i'

[root@oldbody test]#

 

[root@oldbody test]# cp -a file3.txt file5.txt

cp: overwrite `file5.txt'? ^C

[root@oldbody test]# \cp -a file3.txt file5.txt

[root@oldbody test]#

 

 

[root@oldbody test]# cp -a dir3 dir4

[root@oldbody test]# ll

total 16

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir4

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

-rw-r--r-- 1 root root    0 Jun 28 12:17 file4.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file5.txt

[root@oldbody test]#

 

A{B,C}  等价于 AB  AC

[root@oldbody test]# cp -a dir{3,5}

[root@oldbody test]# ll

total 20

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

drwxr-xr-x 3 root root 4096 Jun 28 12:30 dir4

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir5

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

-rw-r--r-- 1 root root    0 Jun 28 12:17 file4.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file5.txt

[root@oldbody test]#

cp /etc/ssh/sshd_config {,.ori}  相当于

cp /etc/ssh/sshd_config cp /etc/ssh/sshd_config.ori   一样

[root@oldbody test]# cp /etc/sysconfig/network-scripts/ifcfg-eth0{,.ori}  备份网卡信息

2.8 mv      

move  移动文件或目录  移动就是剪切的意思  例子:mv /Tech /liangli    把Tech移动到liangli目录下  也有改名的功能

mv    移动文件和重命名   move(rename)files

[root@oldbody test]# mv file{4,6}.txt

[root@oldbody test]# ll

total 20

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir1

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir2

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir3

drwxr-xr-x 3 root root 4096 Jun 28 12:30 dir4

drwxr-xr-x 2 root root 4096 Jun 28 11:48 dir5

-rw-r--r-- 1 root root    0 Jun 28 11:47 file1.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file2.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file3.txt

-rw-r--r-- 1 root root    0 Jun 28 11:47 file5.txt

-rw-r--r-- 1 root root    0 Jun 28 12:17 file6.txt

 

No such file or directory  没有那个文件或目录

 

mv  系统别名  -i参数 在覆盖前需要提示

[root@oldbody test]# alias

alias cp='cp -i'

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@oldbody test]#

多个文件袋移动方式(必须得保证/dir1目录存在)

[root@oldbody test]# mv file1.txt  file2.txt  file3.txt  file5.txt  file6.txt dir1/t  file6.txt dir1/

[root@oldbody test]# ls

dir1  dir2  dir3  dir4  dir5

[root@oldbody test]# cd dir1/

[root@oldbody dir1]# ls

file1.txt  file2.txt  file3.txt  file5.txt  file6.txt

 

[root@oldbody test]# tree

.

|-- dir1

|   |-- file1.txt

|   |-- file2.txt

|   |-- file3.txt

|   |-- file5.txt

|   `-- file6.txt

|-- dir2

|-- dir3

|-- dir4

|   `-- dir3

`-- dir5

 

6 directories, 5 files

[root@oldbody test]#

使用file dir1  可以查看dir1是文件还是目录

[root@oldbody test]# file dir1

dir1: directory

 

移动目录

把前n-1个目录移动到最后一个目录(必须得保证dir5目录存在)

[root@oldbody test]# mv dir1  dir2  dir3  dir4  dir5

[root@oldbody test]# tree

.

`-- dir5

    |-- dir1

    |   |-- file1.txt

    |   |-- file2.txt

    |   |-- file3.txt

    |   |-- file5.txt

    |   `-- file6.txt

    |-- dir2

    |-- dir3

    `-- dir4

        `-- dir3

 

6 directories, 5 files

[root@oldbody test]#

2.9 rm

remove(移除)删除目录和文件 -f(force)强制,-r(递归,用于删除目录) 强调删除命令要慎用,非常危险,删除前一定要先备份一份  因为删除后不可恢复

正确删除文件的姿势     

1,使用mv命令移动到/tmp(回收站)替代删除动作

2、通过cd命令到达目的目录    

然后通过find -type f(d) -name ‘’ | xargs  rm -f

或者    find -type f(d)-name “*.txt” -mtime +7 -exec rm {} \;

别名在你敲这条命令的时候生效的额,通过一些管道符传送给它的是不会生效的

2.10 rmdir

用于删除空目录 当目录不为空时 命令不起作用

-p参数 递归删除空目录

[root@oldbody ~]#

[root@oldbody ~]# mkdir -p oldboy/oldboy1/oldboy2

[root@oldbody ~]# rmdir -p oldboy/oldboy1/oldboy2/

[root@oldbody ~]#

2.11 ln

link得缩写,可以创建硬链接与软连接

语法 ln 选项 源文件 目标文件

ln这个命令就是创建链接文件的,在默认不带参数的情况下,执行ln命令创建的链接是硬链接   如果使用ln -s创建链接则为软链接

硬链接    ln 源文件 目标文件  (硬链接生成时普通文件-字符)

软链接    ln -s 源文件  目标文件  (目标文件不能事先存在)(生成的符号链接l类型)

硬链接:

是通过inode来进行链接的   在linux文件系统中,多个文件名指向同一个inode是正常且允许的   硬链接文件就相当于文件的另外一个入口    硬链接的作用之一是允许一个文件拥有多个有效路径名(多个入口),这样用户就可以建立硬链接到重要的文件 ,以防止 误删  源数据 注意:目录不能作为硬链接

[root@oldbody ~]# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

[root@oldbody ~]# ln /etc/hosts hard_link

[root@oldbody ~]# ll -ih /etc/hosts hard_link

654109 -rw-r--r--. 3 root root 158 1月  12 2010 /etc/hosts

654109 -rw-r--r--. 3 root root 158 1月  12 2010 hard_link

[root@oldbody ~]# rm -f /etc/hosts

[root@oldbody ~]# cat /etc/hosts

cat: /etc/hosts: 没有那个文件或目录

[root@oldbody ~]# cat hard_link

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

[root@oldbody ~]# ln hard_link /etc/hosts

[root@oldbody ~]# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

[root@oldbody ~]# ll -ih /etc/hosts hard_link

654109 -rw-r--r--. 3 root root 158 1月  12 2010 /etc/hosts

654109 -rw-r--r--. 3 root root 158 1月  12 2010 hard_link

[root@oldbody ~]#

硬链接知识小结:

1、具有相同inode节点号的多个文件是互为硬链接文件

2、删除硬链接文件或者删除源文件任意之一,文件实体并未被删除

3、只有删除了源文件及所有对应的硬链接文件,文件实体才会被删除

4、当所有的硬链接文件及源文件被删除后,再存放新的数据会占用这个文件的空间,或者磁盘fsck检查的时候,删除的数据也会被系统回收(养成删除及多套环境测试的好习惯)

5、硬链接文件就是文件的另一个入口(相当于超市的前门,后门一样)

6、可以通过给文件设置硬链接文件,来防止重要文件被误删

7、通过执行命令 ln 源文件  硬链接文件  即可完成创建硬链接

8、硬链接文件是普通文件,因此可以用rm命令删除

9、对于静态文件(没有进程正在调用的文件)来讲,当对应硬链接数为0 (i_link)文件就被删除    i_link的查看方法(ls -l结果的第三列就是)

软连接  或者符号链接  相当于win的快捷方式 注意:软连接目录可以创建

ln -s 源文件 目标文件

软链接知识小结:

1、软链接类似win的快捷方式(可以通过readlink查看其指向)

2、软链接类似一个文本文件,里面存放的是源文件的路径,指向源文件实体

3、删除源文件,软链接文件依然存在,但是无法访问指向的源文件路径的内容了

4、失效的时候一般是白字红底闪烁提示 

5、执行命令 ln -s 源文件 软链接文件   即可完成创建软链接(目标不能存在)

6、软链接和源文件是不同类型的文件,也是不同的文件,inode号也不相同

7、软链接文件类型为(l),可以用rm命令

[root@oldbody ~]# ln -s /etc/hosts soft_link

[root@oldbody ~]# ll -ih /etc/hosts hard_link soft_link

654109 -rw-r--r--. 3 root root 158 1月  12 2010 /etc/hosts

654109 -rw-r--r--. 3 root root 158 1月  12 2010 hard_link

915785 lrwxrwxrwx  1 root root  10 9月  29 13:34 soft_link -> /etc/hosts

全局结论:

删除源文件 对硬链接没有影响   但是会导致软链接文件失效,白字红底闪烁

删除源文件和硬链接文件   整个文件会真正的被删除

很多硬件设备中的快照功能  就是利用了硬链接的原理

[root@oldbody liangli2018]# ll -ihd oldboy  oldboy/. oldboy/test/..

915805 drwxr-xr-x 3 root root 4.0K 9月  29 13:44 oldboy

915805 drwxr-xr-x 3 root root 4.0K 9月  29 13:44 oldboy/.

915805 drwxr-xr-x 3 root root 4.0K 9月  29 13:44 oldboy/test/..

目录的链接小结:

1、对于目录  不可以创建硬链接  但可以创建软链接(原因是目录可以跨文件系统的)

2、对于目录的软链接是生产场景运维中常用的技巧

3、目录的硬链接不能跨越文件系统(从硬链接原理可以理解 硬链接需要有相同的inode值)

4、每个目录下面都有一个硬链接 “.” 号和对应上级目录的硬链接“..”

5、再父目录里创建一个子目录,父目录的链接数增加1(每个子目录里都有..来指向父目录)但是再父目录里创建文件,父目录的链接数不会增加

2.12 readlink

查看符号链接文件的内容

[root@oldbody ~]# readlink soft_link

/etc/hosts

[root@oldbody ~]#

2.13 find

查找目录下的文件

语法格式 find  路径  操作语句

pathname 命令所查找的目录路径 如.表示当前目录,用/表示根目录

-maxdepth levels 查找的最大目录级数,levels为自然数

-type 文件类型(f(file),d(directory),c(character字母),b(block块)。s(socket),l(link),

-name “文件名” 支持通配符(* ? []等)

-mtime 后面接时间 按修改时间查找 +7 7天以前 、7 第七天、-7最近7天

-atime 和-ctime 按照访问时间和文件状态改变时间来查找文件

-exec  对匹配的文件执行该参数所给出的shell命令

! 取反   -a 取交集 -o 取并集

例子:find /Tech -type f   意思就是查找Tech下类型为文件的东西

find /Tech -type f -name 'oldboy.txt'   查找Tech下文件名为oldboy.txt的文件

find /Tech -type f -name ‘oldboy.txt’ -exec  rm {} \; 意思是对前面查找的文件进行删除的操作 {}表示find找到的内容 

find /Tech -type f -name '*.txt'      *号表示代表所有的文件   通配符

find /Tech -type f -name '*.txt' | xargs rm -f   把查找的内容通过管道   这个就相当于同时删除*.txt所有文件  等同于 rm -f /Tech/oldboy.txt /Tech/liangli.txt文件一样    其中xargs表示把*.txt做成一个文件,可以一起进行删除

find /log -type f -name '*.log' -mtime +15 | xargs rm -f    查找/log目录,删15天以前修改过的文件

find /log -type d -name '*oldboy' -mtime +30 | xargs rm -rf 查找/log目录,删修改日期在30天以前且以oldboy名称结尾的目录

[root@oldbody oldboy]# ls

oldboy.txt  passwd  test.txt

[root@oldbody oldboy]# find /tmp/oldboy/ -type f ! -name "passwd" | xargs rm -f

[root@oldbody oldboy]# ls

passwd

 

find的-delete参数

[root@oldbody key]# find . -type f -name "*.txt"

./file3.txt

./file1.txt

./file2.txt

./file4.txt

./file5.txt

[root@oldbody key]# find . -type f -name "*.txt" -delete

 

[root@oldbody data]# find /data/ -type f -name "*.txt" | xargs  sed -i 's#oldgirll#oldboy#g'

oldboyl

oldboyl

oldboyl

oldboyl

[root@oldbody data]#

方法二:

find  /data  -type  f  -name “*.txt” -exec  sed -i  's#oldgirll#oldboy#g' {} \;

方法三:

sed -i  's#oldgirll#oldboy#g'  `find  /data  -name “*.txt”`

最高效的处理方法 先执行反引号里面的东西

 

[root@oldbody key]# tar zcvf oldboy.gz.tar `find . -type d -name "oldboy"`

./oldboy/

[root@oldbody key]# LL

-bash: LL: command not found

[root@oldbody key]# ll

总用量 44

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir1.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir2.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir3.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir4.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir5.txt

-rw-r--r-- 1 root root   45 9月  29 18:35 file1.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file2.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file3.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file4.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file5.txt

drwxr-xr-x 2 root root 4096 9月  29 18:34 key

-rw-r--r-- 1 root root  109 9月  29 18:39 key.gz

drwxr-xr-x 2 root root 4096 9月  29 18:39 oldboy

-rw-r--r-- 1 root root  115 9月  29 18:49 oldboy.gz.tar

drwxr-xr-x 2 root root 4096 9月  29 18:40 oldgirl

[root@oldbody key]#

 

[root@oldbody key]# find . -type d -name "oldgirl" | xargs tar zcvf oldgirl.gz.tar

./oldgirl/

[root@oldbody key]# ll

总用量 48

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir1.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir2.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir3.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir4.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir5.txt

-rw-r--r-- 1 root root   45 9月  29 18:35 file1.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file2.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file3.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file4.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file5.txt

drwxr-xr-x 2 root root 4096 9月  29 18:34 key

-rw-r--r-- 1 root root  109 9月  29 18:39 key.gz

drwxr-xr-x 2 root root 4096 9月  29 18:39 oldboy

-rw-r--r-- 1 root root  115 9月  29 18:49 oldboy.gz.tar

drwxr-xr-x 2 root root 4096 9月  29 18:40 oldgirl

-rw-r--r-- 1 root root  115 9月  29 18:50 oldgirl.gz.tar

[root@oldbody key]#

 

[root@oldbody tmp]# find /data/ -type f -name "*.log" -size +1M -mtime +30 -exec mv {} /tmp \;

[root@oldbody tmp]#

[root@oldbody test]# find -type f -mtime -7

./dir5/dir1/file3.txt

./dir5/dir1/file6.txt

./dir5/dir1/file1.txt

./dir5/dir1/file2.txt

./dir5/dir1/file5.txt

./.ori

!还有取反的意思   把除了file1.txt文件以外的文件查找出来

[root@oldbody test]# find -type f -name 'file1.txt'

./dir5/dir1/file1.txt

[root@oldbody test]# find -type f ! -name 'file1.txt'

./dir5/dir1/file3.txt

./dir5/dir1/file6.txt

./dir5/dir1/file2.txt

./dir5/dir1/file5.txt

./.ori

[root@oldbody test]#

 

find /root -type f -exec ls -l {} \;

上面红色的部分可以是其他任何命令,例如:ls,mv,cp等命令

{}  代表前面找出的file1.txt文件(内容)   \;作为转义字符  后面的;作为结束标志

[root@oldbody test]# find . -type f -name 'file1.txt' -exec mv {} /tmp/ \;      是正确的

[root@oldbody key]# find . -type f -name "*.txt" -exec ls -l {} \;

-rw-r--r-- 1 root root 0 9月  29 17:38 ./file3.txt

-rw-r--r-- 1 root root 0 9月  29 17:38 ./file1.txt

-rw-r--r-- 1 root root 0 9月  29 17:38 ./file2.txt

-rw-r--r-- 1 root root 0 9月  29 17:38 ./file4.txt

-rw-r--r-- 1 root root 0 9月  29 17:38 ./file5.txt

[root@oldbody key]#

find还有一个参数-ok 其实和-exec用法一样,唯一区别就是使用-ok的话 在删除之前会给出提示 需要管理员确认后方可删除,计较安全

[root@oldbody key]# find . -type f -name "*.txt" -ok ls -l {} \;   

< ls ... ./file3.txt > ? y

-rw-r--r-- 1 root root 0 9月  29 17:38 ./file3.txt

< ls ... ./file1.txt > ? y

-rw-r--r-- 1 root root 0 9月  29 17:38 ./file1.txt

< ls ... ./file2.txt > ? y

-rw-r--r-- 1 root root 0 9月  29 17:38 ./file2.txt

< ls ... ./file4.txt > ? y

-rw-r--r-- 1 root root 0 9月  29 17:38 ./file4.txt

< ls ... ./file5.txt > ? y

-rw-r--r-- 1 root root 0 9月  29 17:38 ./file5.txt

[root@oldbody key]#

[root@oldbody test]# ls /tmp/

123.log  file1.txt  oldboy

[root@oldbody test]#

[root@oldbody test]# find -type f -name 'file2.txt' | xargs -i mv {} /tmp/

[root@oldbody test]# ls /tmp

123.log  file1.txt  file2.txt  oldboy

-i参数就是把前面查找的内容丢到{}中

 

[root@oldbody test]# mv `find -type f -name 'file3.txt'` /tmp/

[root@oldbody test]# ls /tmp

123.log  file1.txt  file2.txt  file3.txt  oldboy

[root@oldbody test]#

mv中-t参数将源和目标反转过来

find . -type f -name “file3.txt” | xargs mv -t dir2/

 

no such file or directory  没有这样的文件或目录

按照目录或者文件的权限来进行查找   -perm 644  参数

[root@oldbody key]# ll

总用量 20

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir1.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir2.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir3.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir4.txt

drwxr-xr-x 2 root root 4096 9月  29 17:38 dir5.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file1.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file2.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file3.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file4.txt

-rw-r--r-- 1 root root    0 9月  29 17:38 file5.txt

[root@oldbody key]#

[root@oldbody key]#

[root@oldbody key]# find . -type f -perm 644

./file3.txt

./file1.txt

./file2.txt

./file4.txt

./file5.txt

[root@oldbody key]#

参数-size 按大小查找文件

[root@oldbody key]# find . -type f -size -1M    小于1M

./file3.txt

./file1.txt

./file2.txt

./file4.txt

./file5.txt

[root@oldbody key]#

 

find中xargs和-exec的区别

-exec 该参数是将查找的结果文件名逐个传递给后面的命令执行,如果文件比较多则执行效率会比较低 文件名有空格等特殊字符照常处理

xargs 该命令是将查找的结果一次性传递给后面的命令执行,命令执行效率高,可以使用-n参数控制一次传递给文件的个数 处理特殊的文件名(如:文件名有空格)需要采用特殊的方式

[root@oldbody key]# find . -type f -name "*.txt"

./file3.txt

./file1.txt

./file2.txt

./file4.txt

./file5.txt

[root@oldbody key]# find . -type f -name "*.txt" -exec echo mingtiannihao {} \;

mingtiannihao ./file3.txt

mingtiannihao ./file1.txt

mingtiannihao ./file2.txt

mingtiannihao ./file4.txt

mingtiannihao ./file5.txt

[root@oldbody key]# find . -type f -name "*.txt"  | xargs echo mingtiannihao

mingtiannihao ./file3.txt ./file1.txt ./file2.txt ./file4.txt ./file5.txt

[root@oldbody key]# find . -type f -name "*.txt"  | xargs -n 3 echo mingtiannihao  

mingtiannihao ./file3.txt ./file1.txt ./file2.txt

mingtiannihao ./file4.txt ./file5.txt

[root@oldbody key]#

[root@oldbody key]# touch "hello word.txt"

[root@oldbody key]# touch mingtian\ nihao.txt

[root@oldbody key]# ll

总用量 0

-rw-r--r-- 1 root root 0 9月  29 14:23 hello word.txt

-rw-r--r-- 1 root root 0 9月  29 14:25 mingtian nihao.txt

[root@oldbody key]#

[root@oldbody key]# find . -type f -name "*.txt" |xargs rm

rm: 无法删除"./hello": 没有那个文件或目录

rm: 无法删除"word.txt": 没有那个文件或目录

rm: 无法删除"./mingtian": 没有那个文件或目录

rm: 无法删除"nihao.txt": 没有那个文件或目录

[root@oldbody key]#

[root@oldbody key]# find . -type f -name "*.txt" -print0|xargs -0 rm

[root@oldbody key]# ll

总用量 0

[root@oldbody key]#

2.14 xargs

将标准输入转换成命令行参数

三行文本变成一行文本了

[root@oldbody test]# cat test.txt

1 2 3 4 5 6

7 8 9

10 11

[root@oldbody test]# xargs < test.txt

1 2 3 4 5 6 7 8 9 10 11

[root@oldbody test]#

-n  参数  -n 4 以4个字符为一组

[root@oldbody test]# xargs -n 4 < test.txt

1 2 3 4

5 6 7 8

9 10 11

[root@oldbody test]#

自定义分隔符-d参数

[root@oldbody ~]# echo splitXsplitXsplitXsplitX |xargs -d X

split split split split

[root@oldbody ~]# echo splitXsplitXsplitXsplitX |xargs -d X -n 2

split split

split split

-i 参数 把前面找到的文件命令和 {} 进行关联  用参数i来实现

[root@oldbody key]# find . -type f -name "file*"

./file3.txt

./file1.txt

./file2.txt

[root@oldbody key]# find . -type f -name "file*" | xargs -i mv {} dir1/

[root@oldbody key]# ll

总用量 4

drwxr-xr-x 2 root root 4096 9月  29 14:14 dir1

[root@oldbody key]# tree

.

└── dir1

    ├── file1.txt

    ├── file2.txt

    └── file3.txt

 

1 directory, 3 files

[root@oldbody key]#

结合find使用xargs的特殊案例  xargs -0(数字0)

[root@oldbody key]# touch "hello word.txt"

[root@oldbody key]# touch mingtian\ nihao.txt

[root@oldbody key]# ll

总用量 0

-rw-r--r-- 1 root root 0 9月  29 14:23 hello word.txt

-rw-r--r-- 1 root root 0 9月  29 14:25 mingtian nihao.txt

[root@oldbody key]#

[root@oldbody key]# find . -type f -name "*.txt" |xargs rm

rm: 无法删除"./hello": 没有那个文件或目录

rm: 无法删除"word.txt": 没有那个文件或目录

rm: 无法删除"./mingtian": 没有那个文件或目录

rm: 无法删除"nihao.txt": 没有那个文件或目录

[root@oldbody key]#

[root@oldbody key]# find . -type f -name "*.txt" -print0|xargs -0 rm

[root@oldbody key]# ll

总用量 0

[root@oldbody key]#

2.15 rename

rename  from to file  重新命名文件

rename

from   代表需要替换或要处理的字符  一般是文件的一部分 或者是文件的扩展名

to      把前面form代表的内容替换为to代表的内容即重命名处理后的结果

file    就是我们需要处理的文件

[root@oldboy test]# touch shu_102999_{1..5}_finished.jpg

[root@oldboy test]#

[root@oldboy test]#

[root@oldboy test]# ll

总用量 0

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_1_finished.jpg

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_2_finished.jpg

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_3_finished.jpg

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_4_finished.jpg

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_5_finished.jpg

[root@oldboy test]#  rename "finished"  ""  *     把finished替换成空

[root@oldboy test]# ll

总用量 0

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_1_.jpg

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_2_.jpg

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_3_.jpg

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_4_.jpg

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_5_.jpg

[root@oldboy test]# rename  "jpg"  "JPG"  *

[root@oldboy test]# ll

总用量 0

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_1_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_2_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_3_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_4_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_102999_5_.JPG

 11:38 shu_102999_5_.JPG

[root@oldboy test]# rename  "102999"  "1314"  /root/test/*

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_1314_1_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_1314_2_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_1314_3_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_1314_4_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 shu_1314_5_.JPG

[root@oldboy test]#

2.16 basename

basename命令用于显示去除路径和文件后缀部分的文件名或目录名

[root@oldboy test]# ll

总用量 0

-rw-r--r-- 1 root root 0 9月  19 11:38 _1314_1_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 _1314_2_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 _1314_3_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 _1314_4_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 _1314_5_.JPG

[root@oldboy test]# basename /root/test/_1314_1_.JPG

_1314_1_.JPG

[root@oldboy test]# basename /root/test/_1314_1_.JPG   .JPG

_1314_1_

[root@oldboy test]# basename /root/test/_1314_1_.JPG   _.JPG

_1314_1

[root@oldboy test]#

2.17 dirname

显示文件或目录名

[root@oldboy test]# ll

总用量 0

-rw-r--r-- 1 root root 0 9月  19 11:38 _1314_1_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 _1314_2_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 _1314_3_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 _1314_4_.JPG

-rw-r--r-- 1 root root 0 9月  19 11:38 _1314_5_.JPG

[root@oldboy test]# dirname /root/test/_1314_1_.JPG

/root/test

[root@oldboy test]#

2.18 chattr

改变文件的扩展属性

chattr命令 只能是root用户用  其他用户使用不了

-a  只能向文件追加数据 你不能删除它  不能清空它

    用途:比如.bash_history文件中写入历史纪录,可以采用-i追加模式,只增不减

-i   设定文件不能被删除,改名,写入或新增内容

    用途:希望锁定的文件不能被删除或修改,比如/etc/passwd

-R  递归更改目录属性

+ 增加参数 – 移除参数 = 更新为指定参数

lsattr命令     所有用户都能够使用  显示文件的扩展属性

 [root@oldboy ~]# lsattr person.txt

-------------e- person.txt

[root@oldboy ~]#

[root@oldboy ~]#

[root@oldboy ~]# chattr +a person.txt

[root@oldboy ~]# lsattr person.txt   

-----a-------e- person.txt

[root@oldboy ~]#

 

[root@oldboy ~]# rm -f person.txt

rm: 无法删除"person.txt": 不允许的操作

[root@oldboy ~]# > person.txt

-bash: person.txt: 不允许的操作

[root@oldboy ~]# echo 111 >> person.txt

[root@oldboy ~]# cat person.txt

101,oldboy,CEO

102,zhangyao,CTO

103,Alex,COO

104,yy,CFO

105,feixue,CIO

011111111

0111111100

111

[root@oldboy ~]#

-i  就是加锁

[root@oldboy ~]# chattr +i .bash_history

[root@oldboy ~]# lsattr .bash_history

----i--------e- .bash_history

[root@oldboy ~]#

[root@oldboy ~]# >.bash_history

-bash: .bash_history: 权限不够

[root@oldboy ~]# echo 1111>>.bash_history

-bash: .bash_history: 权限不够

[root@oldboy ~]# rm -f .bash_history

rm: 无法删除".bash_history": 不允许的操作

[root@oldboy ~]#

 

[root@oldbody key]# tree

.

├── 123

│   └── mintain

├── 456

└── 789

 

4 directories, 0 files

[root@oldbody key]# chattr +a -R  123

[root@oldbody key]# lsattr

-------------e- ./456

-------------e- ./789

-----a-------e- ./123

[root@oldbody key]# cd 123

[root@oldbody 123]# ll

总用量 4

drwxr-xr-x 2 root root 4096 9月  29 14:57 mintain

[root@oldbody 123]# lsattr

-----a-------e- ./mintain

2.19 file

file  后面接文件或目录 查看文件类型

[root@oldboy ~]# file xiaomi.txt

xiaomi.txt: ASCII text

[root@oldbody ~]# file *

1.txt:              ASCII text

anaconda-ks.cfg:    ASCII English text

hard_link:          ASCII text

install.log:        UTF-8 Unicode text

install.log.syslog: ASCII text

key:                directory

key1:               symbolic link to `key'

liangli:            directory

liangli2018:        directory

oldboy.txt:         ASCII text

soft_link:          symbolic link to `/etc/hosts'

2.20 md5sum 

-c  从指定文件中读取MD5校验值,并进行校验

计算和校验文件的md5值 (md5值是唯一的 两个文件如果md5一样的话 证明是同一个文件)源文件和硬链接文件和软链接文件md5sum是一样的

[root@oldboy ~]# ln xiaomi.txt xiaomi2.txt

[root@oldboy ~]# md5sum xiaomi.txt 

d41d8cd98f00b204e9800998ecf8427e  xiaomi.txt

[root@oldboy ~]# md5sum xiaomi2.txt

d41d8cd98f00b204e9800998ecf8427e  xiaomi2.txt

[root@oldboy ~]# md5sum xiaomi3.txt 

d41d8cd98f00b204e9800998ecf8427e  xiaomi3.txt

[root@oldboy ~]#

[root@oldboy ~]# md5sum xiaomi.txt >md5.log

[root@oldboy ~]# cat md5.log

d41d8cd98f00b204e9800998ecf8427e  xiaomi.txt

[root@oldboy ~]# md5sum -c md5.log              显示校验结果是否成功

xiaomi.txt: 确定

[root@oldboy ~]#

模拟网络波动造成现象

[root@oldboy ~]# echo "1111">>xiaomi.txt

[root@oldboy ~]# md5sum xiaomi.txt

1f18348f32c9a4694f16426798937ae2  xiaomi.txt

[root@oldboy ~]# md5sum -c md5.log      

xiaomi.txt: 失败

md5sum: 警告:1/1 生成的校验和不匹配

[root@oldboy ~]#

2.21 chown

改变文件或目录的用户和用户组

chown命令  只能是root用户使用  改变文件的属主和属组(就是用户和用户组)

chown  用户  文件或目录

chown  :用户组   文件或目录

chown   用户:用户组   文件或目录   其中:可以用.号代替

-R  就是递归的意思 

[root@oldboy ~]# chown oldboy test.txt

[root@oldboy ~]# ll test.txt         

-rw-r--r-- 2 oldboy root 419 9月  19 10:01 test.txt

[root@oldboy ~]#

这个前提条件是oldboy用户必须在/etc/passed 和/etc/group中 存在

[root@oldboy ~]# chown :user1 test.txt

[root@oldboy ~]# ll test.txt

-rw-r--r-- 2 oldboy user1 419 9月  19 10:01 test.txt

[root@oldboy ~]#

[root@oldboy ~]# chown root.root test.txt

[root@oldboy ~]# ll test.txt

-rw-r--r-- 2 root root 419 9月  19 10:01 test.txt

[root@oldboy ~]#

-R  就是递归的意思 

[root@oldboy test]# pwd

/root/test

[root@oldboy test]# chown -R oldboy.oldboy /root/test

[root@oldboy test]# ll

总用量 0

-rw-r--r-- 1 oldboy oldboy 0 9月  19 11:38 _1314_1_.JPG

-rw-r--r-- 1 oldboy oldboy 0 9月  19 11:38 _1314_2_.JPG

-rw-r--r-- 1 oldboy oldboy 0 9月  19 11:38 _1314_3_.JPG

-rw-r--r-- 1 oldboy oldboy 0 9月  19 11:38 _1314_4_.JPG

-rw-r--r-- 1 oldboy oldboy 0 9月  19 11:38 _1314_5_.JPG

[root@oldboy test]#

2.22 chmod

改变文件或目录的权限

chmod命令   可以是root用户用  也可以是这个文件的属主使用  改变文件或目录权限

-R 递归参数

例如: rwxr-xr-x  9个字符  三个权限是一组

前三位叫属主权限位 /用户权限位    假如我root用户对oldboy具有读写执行权限位

r-xr  是用户组对应的权限   当有人加入到用户组的话,就对应这个权限位

r-w  其他用户权限位     

+        加入

-             减去

=        设置

r       4    读

w      2    写

x       1    执行

-       0    无

用户类型

u       user    用户

g       group   用户组

o       Other    其他

a        ALL    总和

[root@oldboy ~]# ll test.txt

-rw-r--r-- 2 root root 419 9月  19 10:01 test.txt

[root@oldboy ~]# chmod u=x,g=w,o=rwx test.txt

[root@oldboy ~]# ll test.txt                 

---x-w-rwx 2 root root 419 9月  19 10:01 test.txt

[root@oldboy ~]# chmod o=--- test.txt            

[root@oldboy ~]# ll test.txt         

---x-w---- 2 root root 419 9月  19 10:01 test.txt

[root@oldboy ~]# chmod o-rwx test.txt     

[root@oldboy ~]# ll test.txt         

---x-w---- 2 root root 419 9月  19 10:01 test.txt

[root@oldboy ~]#

[root@oldboy ~]# chmod 421 test.txt

[root@oldboy ~]# ll test.txt

-r---w---x 2 root root 419 9月  19 10:01 test.txt

[root@oldboy ~]#

[root@oldbody key]# tree

.

└── 123

    └── mintain

 

2 directories, 0 files

[root@oldbody key]# chmod 744 -R 123/

[root@oldbody key]# ll

总用量 4

drwxr--r-- 3 root root 4096 9月  29 14:57 123

[root@oldbody key]# ll 123

总用量 4

drwxr--r-- 2 root root 4096 9月  29 14:57 mintain

[root@oldbody key]#

2.23 chgrp  

更改文件的用户组   基本被淘汰

[root@oldboy ~]# ll test.txt

-r---w---x 2 root root 419 9月  19 10:01 test.txt

[root@oldboy ~]# chgrp user1 test.txt

[root@oldboy ~]# ll test.txt

-r---w---x 2 root user1 419 9月  19 10:01 test.txt

[root@oldboy ~]#

2.24 umask

显示或设置权限掩码

umask (默认权限分配的命令)为什么默认权限目录是755,文件权限是644  而不是其他的值呢

不管是操作系统还是网站,安全权限的临界点,目录755文件644 是相对安全的权限,并且用户为root 以及用户组root

以上权限兼顾了安全和使用,生产工作中一定要尽量要我们文件和目录达到以上默认的权限,包括用户和属组都是root,Linux系统默认权限的方针,允许浏览,查看,但是禁止创建和修改文件及内容以及执行

在linux下文件的默认权限是由umask值决定的,umask是通过八进制的数值来决定用户创建文件或目录的默认权限的,umask对应数值表示的是禁止的权限

超级用户

[root@oldboy ~]# umask

0022

[root@oldboy ~]#

普通用户

[test@oldboy ~]$ umask

0002

[test@oldboy ~]$

umask越小,权限越大,根据umask值计算文件权限3中方法

文件的权限从666开始算起,umask都为偶数,直接相减,如果有奇数对应位+1

比如:

6  6  6

0  2   2  -

---------------

6  4   4 

[root@oldboy ~]# ll xiaomi.txt

-rw-r--r-- 2 root root 5 9月  19 12:28 xiaomi.txt

[root@oldboy ~]#

[root@oldboy ~]#

[root@oldboy ~]# umask

0022

[root@oldboy ~]#

[root@oldboy ~]# umask 044

[root@oldboy ~]# touch f044

[root@oldboy ~]# ll f044

-rw--w--w- 1 root root 0 9月  20 11:54 f044

[root@oldboy ~]# umask 064

[root@oldboy ~]# touch liangli123.txt

[root@oldboy ~]# ll liangli123.txt

-rw-----w- 1 root root 0 9月  20 11:56 liangli123.txt

[root@oldboy ~]#

[root@oldboy ~]# umask 043

[root@oldboy ~]# touch f043

[root@oldboy ~]# ll f043

-rw--w-r-- 1 root root 0 9月  20 11:58 f043

[root@oldboy ~]# umask 055

[root@oldboy ~]# touch f055

[root@oldboy ~]# ll f055

-rw--w--w- 1 root root 0 9月  20 11:59 f055

[root@oldboy ~]#

对于目录的权限从777开始(不分奇偶)

[root@oldboy ~]# umask 065

[root@oldboy ~]# mkdir d065

[root@oldboy ~]# ll d065

总用量 0

[root@oldboy ~]# ll -d d065

drwx--x-w- 2 root root 4096 9月  20 12:02 d065

上面的修改都是临时生效的,想要永久生效就需要修改配置文件/etc/profile或者/etc/bashrc

[root@oldbody ~]# sed -n '61,69p' /etc/profile

if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then

    umask 002

else

    umask 022

fi

 

for i in /etc/profile.d/*.sh ; do

    if [ -r "$i" ]; then

        if [ "${-#*i}" != "$-" ]; then

[root@oldbody ~]# sed -n '57,68p' /etc/bashrc 

                fi

        esac

    }

 

    # By default, we want umask to get set. This sets it for non-login shell.

    # Current threshold for system reserved uid/gids is 200

    # You could check uidgid reservation validity in

    # /usr/share/doc/setup-*/uidgid file

    if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then

       umask 002

    else

       umask 022

[root@oldbody ~]#

几乎没有什么需求要修改必须要修改umask的,默认的umask是系统安全的临界点,是最合适的

若要修改的话,将umask 033放在配置文件的最后一行即可

猜你喜欢

转载自www.cnblogs.com/key-oldboy/p/9727217.html