linux只提取前两个目录名,Linux 文件和目录操作命令(17个)

文件和目录操作命令(17个)

1ls

ls命令的作用是以不同的方式,查看(列出)目录内的内容。

【功能说明】:list directory contents

【语法格式】:ls [OPTION]... [FILE]...

1.1选项参数-a※    --all            #<==以点开头的文件也显示出来,即显示隐藏的文件

-d※    --directory      #<==只列出目录

-l※    long             #<==以长格式显示

-F      --classify       #<==给不同的条目,加上不同的标识符,比如目录就加上/

-p                       #<==只给目录加斜线【slash:斜线】

-h※    --human-readable #<==以人类只读的方式显示,即显示出文件的大小(有单位)

-i※    --inode          #<==显示文件的inode号

-s      --size           #<==打印出文件被分配的block的大小

-r      reverse          #<==排序时逆向显示条目

-t      modification time#<==按照修改时间排序

--time-style=long        #<==以时间格式显示

--color=auto             #<==显示颜色

1.2实践操作

练习素材如下:mkdir /test

cd /test/

touch file{1..3}.txt

mkdir dir{1..3}

1.2.1-a:显示隐藏文件[root@s1 test]# ls -a    #<==以点开头的文件是隐藏文件

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

1.2.2-l:以长格式显示条目[root@s1 test]# ls -l

总用量 12

drwxr-xr-x 2 root root 4096 7月  24 02:30 dir1

drwxr-xr-x 2 root root 4096 7月  24 02:30 dir2

drwxr-xr-x 2 root root 4096 7月  24 02:30 dir3

-rw-r--r-- 1 root root    0 7月  24 02:30 file1.txt

-rw-r--r-- 1 root root    0 7月  24 02:30 file2.txt

-rw-r--r-- 1 root root    0 7月  24 02:30 file3.txt

1.2.3ll:(别名)以长格式显示条目,并且自动带颜色的区分[root@s1 test]# ll

#<==ll已设置了别名

#<==如何取消别名的作用昵?

#<==可以在命令的前面加反斜杠,或者写该命令的全路径

总用量 12

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir1

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir2

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir3

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file1.txt

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file2.txt

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file3.txt

[root@s1 test]# alias ll    #<==查看别名

alias ll='ls -l --color=auto --time-style=long'

[root@s1 test]# echo "alias ll='ls -l --color=auto --time-style=long'" >>/etc/bashrc

#<==使别名永久生效

#<==将配置好的别名命令,追加到/etc/bashrc的配置文件中

#<==单引号不具有变量置换功能,输出时所见即所得

#<==双引号有变量置换功能,解析变量后输出,不加引号相当于双引号,但一般常用双引号

#<==注意:定义别名永久生效:

/etc/profile                #<==全局生效

/etc/bashrc                 #<==全局生效

~/.bashrc                   #<==当前用户生效

/root/.bashrc               #<==当前用户生效

[root@s1 test]# which ls    #<==查看命令的全路径

alias ls='ls --color=auto'

/bin/ls

1.2.4-h:显示条目的单位(K或M)[root@s1 test]# ll -h

总用量 12K

drwxr-xr-x 2 root root 4.0K 2016-07-24 02:30 dir1

drwxr-xr-x 2 root root 4.0K 2016-07-24 02:30 dir2

drwxr-xr-x 2 root root 4.0K 2016-07-24 02:30 dir3

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file1.txt

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file2.txt

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file3.txt

1.2.5-d:只显示目录条目[root@s1 test]# ls -d dir1   #<==想显示指定的目录,后面必须加上目录名

dir1

[root@s1 test]# ls -d        #<==如果后面不加目录,则显示当前的目录(即一个点)

1.2.6-F:给不同的条目加不同的指示符[root@s1 test]# ll -F        #<==如果文件是目录,后面会自动加上提示符【/】

总用量 12

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir1/

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir2/

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir3/

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file1.txt

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file2.txt

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file3.txt

[root@s1 test]# ls -F /bin/  #<==还有可能会加上符号:@  *

alsaunmute*          rvi@

1.2.7-p:只给目录条目加指示符:斜线[root@s1 test]# ll -p        #<==只给目录加斜线,其他条目想加提示符,就用-F参数

总用量 12

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir1/

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir2/

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir3/

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file1.txt

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file2.txt

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file3.txt

1.2.8-r与-t _企业案例:查找最近更新的文件

解答思路:

先以长格式显示,再加-t参数,按照修改时间排序,这样最新修改的条目就会显示在最上方,但如果条目多,最上方看就不方便了;最新更新的文件应该放在最下方比较合理,所以再加-r参数,逆序排序,搞定。[root@s1 test]# mkdir dir10         #<==再创建一个目录,注意时间

[root@s1 test]# touch file20.txt    #<==再创建一个文件

[root@s1 test]# ll -tr

总用量 16

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file3.txt

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file2.txt

-rw-r--r-- 1 root root    0 2016-07-24 02:30 file1.txt

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir3

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir2

drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir1

drwxr-xr-x 2 root root 4096 2016-07-24 02:48 dir10

-rw-r--r-- 1 root root    0 2016-07-24 02:48 file20.txt

1.2.9-i:显示文件的inode[root@s1 test]# ll -i               #<==inode,相当于每个文件的×××号

总用量 16

141532 drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir1

141535 drwxr-xr-x 2 root root 4096 2016-07-24 02:48 dir10

141533 drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir2

141534 drwxr-xr-x 2 root root 4096 2016-07-24 02:30 dir3

141529 -rw-r--r-- 1 root root    0 2016-07-24 02:30 file1.txt

141536 -rw-r--r-- 1 root root    0 2016-07-24 02:48 file20.txt

141530 -rw-r--r-- 1 root root    0 2016-07-24 02:30 file2.txt

141531 -rw-r--r-- 1 root root    0 2016-07-24 02:30 file3.txt

inode  文件类型 权限 硬链接数 属主 属组 大小 修改日期 文件名

2cd

切换目录的命令。

【功能说明】:Change the shell working directory

【语法格式】:cd [dir]

2.1实践操作

2.1.1切换到家目录[root@oldboy mnt]# cd    #<==不管在哪个目录下,直接cd就可以切换回当前用户的家目录

[root@oldboy ~]#pwd

/root

2.1.2切换到网卡配置文件所在的目录[root@oldboy ~]# cd /etc/sysconfig/network-scripts/

#<==这里写的是绝对路径,必须以/开头,也可以一步一步切换进去(即可以用相对路径)

[root@oldboy network-scripts]# pwd

/etc/sysconfig/network-scripts

2.1.3切换到上一次所在的目录[root@oldboy network-scripts]# pwd             #<==先查看当前目录

/etc/sysconfig/network-scripts

[root@oldboy network-scripts]# cd -            #<==cd后加杠,代表切换回上一次所在的目录

/root

[root@oldboy ~]# cd -                          #<==cd后加杠,又回到上一次所在的目录了

/etc/sysconfig/network-scripts

[root@oldboy network-scripts]# echo $OLDPWD    #<==它的原理是由这个环境变量控制着

/root

2.1.4切换到上一级目录[root@oldboy network-scripts]# pwd             #<==先查看当前目录

/etc/sysconfig/network-scripts

[root@oldboy network-scripts]# cd ..           #<==也可以写成:cd ../

[root@oldboy sysconfig]# pwd

/etc/sysconfig

2.1.5切换到前两级目录[root@oldboy network-scripts]# pwd             #<==先查看当前目录

/etc/sysconfig/network-scripts

[root@oldboy network-scripts]# cd ../..        #<==也可以写成:cd ../../

[root@oldboy etc]# pwd

/etc

3cp

复制命令,用来复制文件或目录。

【功能说明】:copy files and directories

【语法格式】:cp [OPTION]... SOURCE... DIRECTORY

3.1选项参数-a※  -pdr              #<==递归复制,并且操持文件的属性:权限,属主和属组,时间戳……

-p    --preserve        #<==复制时,保持文件原有的属性

-r※  --recursive       #<==递归复制,即复制目录

-d                      #<==复制时,保持链接文件的属性

-i    --interactive     #<==交互式复制,如果文件已存在,覆盖前会有提示

3.2实践操作

3.2.1复制文件[root@oldboy test]# cp file3.txt file4.txt    #<==复制文件

[root@oldboy test]# ll file3.txt file4.txt    #<==可以看到文件file4.txt的创建时间改变了,也就是说,复制时如果不加参数,那么原文件的属性不会被继承

-rw-r--r--. 1 root root    0 May 11 15:16 file3.txt

-rw-r--r--. 1 root root    0 May 11 17:03 file4.txt

3.2.2-a:复制时保持属性[root@oldboy test]# cp -a file{1,5}.txt       #<==-a相当于-pdr,它的作用是复制时可以保持文件或目录的属性,复制时也可以用大括号来简写

[root@oldboy test]# ll file1.txt file5.txt    #<==这两个文件的属性是一样的

-rw-r--r--. 1 root root 0 May 11 15:16 file1.txt

-rw-r--r--. 1 root root 0 May 11 15:16 file5.txt

3.2.3-i:覆盖前会先提示,别名[root@oldboy test]# cp -a file{1,5}.txt          #<==覆盖前会先提示,别名已被定义

cp: overwrite `file5.txt'? y

[root@oldboy test]# alias cp                     #<==查看别名

alias cp='cp -i'

[root@oldboy test]# \cp -a file{1,5}.txt         #<==命令前加斜线,忽略别名

[root@oldboy test]# which cp                     #<==查看命令的全路径

alias cp='cp -i'

/bin/cp

[root@oldboy test]# /bin/cp -a file{1,5}.txt     #<==写命令的全路径,也能忽略别名

[root@oldboy test]# ll file1.txt file5.txt       #<==验证,因为加了-a,所以属性一样

-rw-r--r--. 1 root root 0 May 11 15:16 file1.txt

-rw-r--r--. 1 root root 0 May 11 15:16 file5.txt

3.2.4备份文件的简写(修改前必须先备份)[root@oldboy test]# cp /etc/ssh/ssh_config{,.ori}

#<==大括号里面的逗号一定不能忘记写,{}展开后,原文件什么都不加,复制的文件后加上(.ori)

[root@oldboy test]# ll /etc/ssh/ssh_config /etc/ssh/ssh_config.ori

-rw-r--r--. 1 root root 2047 Jul 24  2015 /etc/ssh/ssh_config

-rw-r--r--. 1 root root 2047 May 11 18:35 /etc/ssh/ssh_config.ori

3.2.5-p:保持属性[root@oldboy data]# chmod 777 oldboy.txt

#<==修改文件的属性,复制时看是否将属性也复制过去

[root@oldboy data]# ll oldboy.txt             #<==验证文件的属性是否修改了

-rwxrwxrwx. 1 root root 0 May 11 18:55 oldboy.txt

[root@oldboy data]# cp -p oldboy.txt /tmp/    #<==复制时加-p参数代表保持文件的属性

[root@oldboy data]# ll /tmp/oldboy.txt        #<==文件的属性也跟着过来了

-rwxrwxrwx. 1 root root 0 May 11 18:55 /tmp/oldboy.txt

3.2.6-r:复制目录[root@oldboy /]# cp data/ /tmp/

#<==将目录/data/复制到/tmp下,-r参数用来复制目录,必须指定,要不复制时会报错

cp: omitting directory `data/'                #<==报错,因为cp默认不能复制目录

[root@oldboy /]# cp -r data/ /tmp/            #<==递归复制,搞定

[root@oldboy /]# ll -d /tmp/data/

drwxr-xr-x. 2 root root 4096 May 11 19:05 /tmp/data/

4find

查找命令,参数特别多。

【功能说明】:search for files in a directory hierarchy#<==在一个目录层次中找出文件

【语法格式】:find [path] [expression]

4.1选项参数-type                              #<==文件类型

f    regular file              #<==普通文件

d    directory                 #<==目录

c    character special         #<==字符设备文件

b    block (buffered) special  #<==块设备文件

s    socket                    #<==socket文件

l    symbolic link             #<==软链接文件

-name                              #<==加上”文件名”,也可以用通配符来匹配

-mtime                             #<==修改文件内容的时间

-atime                             #<==访问文件内容的时间

-ctime                             #<==文件属性改变的时间

-perm                              #<==按照文件的权限掩码查找:4755

-maxdepth                          #<==查找深度(注意:这个参数最好放在最前面)

-size                              #<==指定大小:+50k(大于50k)    -100k(小于100k)

!         #<==取反

-a        #<==交集

-o        #<==并集

-exec     #<==后面加要处理的命令

如:find /var/ -type s -exec ls -l {} \;

{}代表find找到的内容

;是bash中有特殊含义的,必须用反斜杠转义

4.2实践操作

4.2.1-type和-mtime:查找最近7天内创建的文件[root@oldboy test]# find ./ -type f -mtime -7

#<==7代表第7天,减7代表最近7天,+7代表7天以前

4.2.2文件取反[root@oldboy test]# find ./ -type f ! -name "*.txt"

#<==!代表取反,只要后缀名是.txt的通通不要

4.2.3使用-exec移动文件

~方式一[root@oldboy test]# find ./ -type f -name "file1.txt" -exec mv {} /tmp/ \;

#<==-exec代表执行,后面加要执行的动作是mv,{}代表find找到的内容,把它移动到/tmp下,最后有个分号【;是bash中有特殊含义的】,所以必须要用反斜线来转义它。

~方式二[root@oldboy test]# mv `find ./ -type f -name "file2.txt"` /tmp/

#<==使用反引号将find命令给包起来,这样才能符合mv的格式

[root@oldboy data]# mv $(find ./ -type f -name "file2.txt")/tmp/

#<==这条命令也可以,【`命令`==$(命令)】

4.2.4和mv搭配使用[root@oldboy ~]# cd /data/

[root@oldboy data]# touch {1..10}.txt

#<==准备练习素材

[root@oldboy ~]# mv `find /data/ -type f -name "*.txt"` /tmp/

#<==使用反引号将find命令给包起来,这样才能符合mv的格式

[root@oldboy ~]# find /data/ -type f -name "*.txt" -exec mv {} /tmp/ \;

#<==这个写法和上面的写法是一样的

4.2.5-type,-mtime,-exec[root@oldboy ~]# find /logs -type f -mtime +7 -exec rm {} \;

#<==在/logs目录中查找更改时间在7日以前的文件并删除,-exec后加执行命令:rm命令,{}代表find找到的内容,加反斜线的作用是转义字符,转义分号

[root@oldboy ~]# rm `find /logs -type f -mtime +7`

#<==等价于上面那条命令

4.2.6和xargs搭配使用[root@oldboy ~]# find ./ -type f -name "*.log" -mtime +15 | xargs rm

#<==删15天以前的文件,在当前目录下找以.log结尾的文件,修改时间在15天之前的,找到后把结果放进管理内,出来后给xargs处理,xargs把这些结果全部放在一行来显示,最后再给rm命令把它们给删掉。

[root@oldboy ~]# find ./ -type f -name "*.log" -mtime +15 | xargs rm -rf

#<==删15天以前的目录,慎用。-r为递归删除。

4.2.7-maxdepth与-perm:指定查找深度和按文件的权限掩码查找[root@oldboy ~]# find / -maxdepth 2 -type f -perm 4755| xargs ls -l

-rwsr-xr-x. 1 root root 77336 Oct 15  2014 /bin/mount

-rwsr-xr-x. 1 root root 38200 Jul 24  2015 /bin/ping

-rwsr-xr-x. 1 root root 36488 Jul 24  2015 /bin/ping6

-rwsr-xr-x. 1 root root 34904 Oct 15  2014 /bin/su

-rwsr-xr-x. 1 root root 53472 Oct 15  2014 /bin/umount

-rwsr-xr-x. 1 root root 10272 Oct 15  2014 /sbin/pam_timestamp_check

-rwsr-xr-x. 1 root root 34840 Oct 15  2014 /sbin/unix_chkpwd

5mkdir

创建目录的命令。为了保持好的习惯,只要创建目录,无论它是否存在,一律加-p参数。

【功能说明】:make directories

【语法格式】:mkdir [OPTION]... DIRECTORY...

5.1选项参数-p    make parent directories as needed

#<==递归创建目录

-v    print a message for each created directory

#<==创建目录时显示详细过程

5.2实践操作

5.2.1在根下创建/data目录

~1、绝对路径:[root@oldboy ~]# mkdir /data

#<==这里创建目录时,用的是绝对路径,需在前面加/

[root@oldboy ~]# ls -ld /data/

#<==验证,只看/data这个目录的信息,-d参数代表只看目录,后面必须加上要查看的目录名,否则只显示当前的目录(即一个点)

drwxr-xr-x. 2 root root 4096 May  9 14:59 /data/

~2、相对路径:[root@oldboy ~]# cd /

#<==先切换到根目录

[root@oldboy /]# mkdir data

#<==再创建目录,写相对路径(注:无论处在哪个目录,都可以写绝对路径)

5.2.2-p:递归创建多个目录

要求:在根下创建一个a目录,再在a目录下创建b目录[root@oldboy ~]# mkdir /a/b -p

#<==先创建目录/a,再在这个基础上创建目录b

[root@oldboy ~]# ls -ld /a/b/

#<==验证,只看/a/b/这个目录的信息

drwxr-xr-x. 2 root root 4096 May  9 15:03 /a/b/

[root@oldboy ~]# yum install tree -y

#<==先安装tree命令,虚拟机必须能上网噢

[root@oldboy ~]# LANG=en

#<==修改字符集,防止乱码的干扰

[root@oldboy ~]# tree /a

#<==用tree命令来验证,查看/a目录的目录树

/a

`-- b

1 directory, 0 files

5.2.3-pv:递归创建更多目录[root@oldboy ~]# mkdir -pv /a/{a1,a2,a3} /b/{b1,b2,b3}

#<== -v看到详细的创建过程,但这个参数一般不用

mkdir: created directory `/a'

mkdir: created directory `/a/a1'

mkdir: created directory `/a/a2'

mkdir: created directory `/a/a3'

mkdir: created directory `/b'

mkdir: created directory `/b/b1'

mkdir: created directory `/b/b2'

mkdir: created directory `/b/b3'

[root@oldboy ~]# tree /a /b

#<==tree命令后可加多个目录

/a

|-- a1

|-- a2

`-- a3

/b

|-- b1

|-- b2

`-- b3

6 directories, 0 files

6mv

移动或重命名,尽量用mv命令代替rm命令,即把不需要的文件,移到/tmp目录下,再统一删除。注意:mv命令用来移动文件或目录时,后面一定是目录。

【功能说明】:move (rename) files

【语法格式】:mv [OPTION]... SOURCE... DIRECTORY

6.1选项参数-f    --force          #<==强制移动

-i    --interactive    #<==交互式移动

6.2实践操作

6.2.1移动文件[root@oldboy test]# mv file1.txt /tmp/     #<==将文件file1.txt移动到/tmp目录下

[root@oldboy test]# ll /tmp/file1.txt      #<==验证

-rw-r--r--. 1 root root 0 May 11 15:16 /tmp/file1.txt

6.2.2移动目录[root@oldboy test]# mv dir1/ /tmp/         #<==将dir1目录移动到/tmp目录下

[root@oldboy test]# ll -d /tmp/dir1/       #<==验证

drwxr-xr-x. 2 root root 4096 May 11 15:17 /tmp/dir1/

6.2.3重命名[root@oldboy test]# mv file{4,40}.txt      #<==将文件file4.txt重命名为file40.txt

[root@oldboy test]# ll file40.txt          #<==验证

-rw-r--r--. 1 root root 0 May 11 17:03 file40.txt

6.2.4和find搭配使用,批量移动文件

~问题:将以.txt结尾的文件,移动到/tmp目录下[root@oldboy ~]# cd /data/

[root@oldboy data]# touch {1..10}.txt      #<==准备练习素材

[root@oldboy ~]# mv `find /data/ -type f -name "*.txt"` /tmp/

#<==使用反引号将find命令给包起来,这样才能符合mv的格式

[root@oldboy data]# mv $(find ./ -type f -name "*.txt") /tmp/

#<==这条命令也可以,【`命令`==$(命令)】

[root@oldboy ~]# ll /data/                 #<==/data目录下空了

total 0

[root@oldboy ~]# ll /tmp/                  #<==检查

total 8

-rw-r--r--. 1 root root    0 May 11 22:52 10.txt

-rw-r--r--. 1 root root    0 May 11 22:52 1.txt

省略……

-rw-r--r--. 1 root root    0 May 11 22:52 9.txt

7pwd

显示当前路径。

【功能说明】:Print the name of the current working directory.

【语法格式】:pwd [-P]

7.1选项参数-P    print the physical directory, without any symbolic links

#<==打印物理目录,而不是符号(软)链接的目录

7.2实践操作

7.2.1查看当前所在的目录[root@oldboy ~]# echo $PWD        #<==显示当前的路径,是从这个变量中获取到的

/root

[root@oldboy ~]# pwd              #<==查看当前的目录

/root

7.2.2-P:显示物理路径[root@oldboy ~]# ll /etc/init.d

lrwxrwxrwx. 1 root root 11 May 11 13:59 /etc/init.d -> rc.d/init.d

#<==这是一个软链接文件,文件链接到这个目录了/etc/rc.d/init.d

[root@oldboy ~]# cd /etc/init.d/

[root@oldboy init.d]# pwd         #<==pwd显示的是链接路径

/etc/init.d

[root@oldboy init.d]# pwd -P

#<==目录连接链接时,pwd -P 显示出实际路径,而非使用链接(link)路径

/etc/rc.d/init.d

8rename

文件重命名。

【功能说明】:Rename files

【语法格式】:rename[被替换的字符] [替换的字符] [文件名]

8.1实践操作

8.1.1批量重命名

~删除关键字[root@S1 test]# touch stu_102999_{1..5}_finished.jpg

[root@S1 test]# rename "_finished" "" *

[root@S1 test]# ll

total 0

-rw-r--r-- 1 root root 0 May  2 20:23 stu_102999_1.jpg

-rw-r--r-- 1 root root 0 May  2 20:23 stu_102999_2.jpg

-rw-r--r-- 1 root root 0 May  2 20:23 stu_102999_3.jpg

-rw-r--r-- 1 root root 0 May  2 20:23 stu_102999_4.jpg

-rw-r--r-- 1 root root 0 May  2 20:23 stu_102999_5.jpg

~替换关键字[root@S1 test]# rename "jpg" "JPG" *

[root@S1 test]# ll

total 0

-rw-r--r-- 1 root root 0 May  2 20:23 stu_102999_1.JPG

-rw-r--r-- 1 root root 0 May  2 20:23 stu_102999_2.JPG

-rw-r--r-- 1 root root 0 May  2 20:23 stu_102999_3.JPG

-rw-r--r-- 1 root root 0 May  2 20:23 stu_102999_4.JPG

-rw-r--r-- 1 root root 0 May  2 20:23 stu_102999_5.JPG

9rm

r删除文件或目录。

r注意:删普通文件时,不能加-r参数;删除目录则需要-r。

r强调:删除命令要慎用,非常危险!工作中可以给它设置别名,不要使用它。

r正确删除方法:

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

(2)cd到目的目录中;find ./ -type [f|d] -name "文件名" | xagrgs rm

一定要去特定的目录里删;这里的rm,如果设置了别名,它是不会生效的,别名只在当前的命令行中生效,通过管道处理结果后传递给rm来处理,或者别的命令处理后再给rm来处理,别名就会失效。

【功能说明】:remove files or directories

【语法格式】:rm [OPTION]... FILE...

9.1选项参数-f    --force        #<==强制删除

-r    --recursive    #<==用来递归删除目录

9.2实践操作

9.2.1-f:强制删除一个文件[root@oldboy a]# rm -f 1.txt

9.2.2-r:删除一个目录[root@oldboy a]# rm -fr a2/

9.2.3用mv替代rm[root@oldboy test]# mv file3.txt /tmp/

[root@oldboy test]# ll /tmp/file3.txt

-rw-r--r--. 1 root root 0 May 11 15:16 /tmp/file3.txt

9.2.4用find来删除[root@oldboy test]# find ./ -type f -name "file2.txt" |xargs rm

[root@oldboy test]# rm `find ./ -type f -name "file2.txt"`

[root@oldboy test]# rm $(find ./ -type f -name "file2.txt")

10touch

创建文件或更改文件的时间戳。

【功能说明】:change file timestamps

【语法格式】:touch [OPTION]... FILE...

10.1选项参数-a              #<==只改文件的访问时间

-m              #<==只改文件的修改时间

~访问时间的说明access-atime    #<==访问文件内容

modify-mtime    #<==修改文件内容

change-ctime    #<==文件属性改变

10.2实践操作

10.2.1创建一个文件oldboy.txt[root@oldboy ~]# touch oldboy.txt

10.2.2同时创建多个文件[root@oldboy ~]# touch stu0{1..5}.txt

[root@oldboy ~]# ll

-rw-r--r--. 1 root root     0 May 12 00:58 stu01.txt

-rw-r--r--. 1 root root     0 May 12 00:58 stu02.txt

-rw-r--r--. 1 root root     0 May 12 00:58 stu03.txt

-rw-r--r--. 1 root root     0 May 12 00:58 stu04.txt

-rw-r--r--. 1 root root     0 May 12 00:58 stu05.txt

10.2.3-a:改变文件的访问时间[root@oldboy ~]# stat oldboy.txt             #<==显示文件的详细属性

File: `oldboy.txt'

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

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

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

Access: 2016-05-12 01:01:24.180645668 +0800

Modify: 2016-05-12 01:01:24.180645668 +0800

Change: 2016-05-12 01:01:24.180645668 +0800

[root@oldboy ~]# touch -a oldboy.txt         #<==改变访问时间的时间戳

[root@oldboy ~]# stat oldboy.txt             #<==显示文件的详细属性

File: `oldboy.txt'

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

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

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

Access: 2016-05-12 01:05:50.675629932 +0800  #<==因为文件的属性改变了

Modify: 2016-05-12 01:01:24.180645668 +0800

Change: 2016-05-12 01:05:50.675629932 +0800  #<==所以change的时间也改变

10.2.4-m:改变文件的修改时间[root@oldboy ~]# touch -m oldboy.txt         #<==只改变修改时间的时间戳

[root@oldboy ~]# stat oldboy.txt             #<==显示文件的详细属性

File: `oldboy.txt'

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

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

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

Access: 2016-05-12 01:05:50.675629932 +0800

Modify: 2016-05-12 01:10:30.740644963 +0800  #<==因为文件的属性改变了

Change: 2016-05-12 01:10:30.740644963 +0800  #<==所以change的时间也改变

11tree

以目录树的格式显示目录的结构。

【功能说明】:list contents of directories in a tree-like format.

【语法格式】:tree [参数] [目录]

11.1选项参数-a        #<==显示隐藏文件

-d        #<==只显示目录

-L※      #<==指定目录树的层次

-h        #<==人类可读的方式显示,即显示文件的单位大小

-f        #<==显示目录的完整路径

-i        #<==indentation(缩进)经常和-f参数配合,不打印树枝符号

-F        #<==给目录加斜线

11.2实践操作

11.2.1-a:显示隐藏文件[root@oldboy ~]# tree -a            #<==以点开头的都会列出来

.

|-- .bash_history

|-- .bash_logout

|-- .bash_profile

|-- .bashrc

|-- .cshrc

|-- anaconda-ks.cfg

|-- install.log

`-- install.log.syslog

0 directories, 8 files

11.2.2-L:指定目录树的层次[root@oldboy ~]# tree /chen/        #<==可以看到默认是多层目录树结构

/chen/

`-- a

`-- b

`-- c

3 directories, 0 files

[root@oldboy ~]# tree -L 2 /chen/   #<==只看两层目录树结构

/chen/

`-- a

`-- b

2 directories, 0 files

11.2.3-d与-h:只看目录,并显示大小[root@oldboy ~]# tree -hd ./        #<==只看当前目录树的目录并且显示单位

./

|-- [4.0K]  -

|-- [4.0K]  a

`-- [4.0K]  b

3 directories

11.2.4-f:看目录完整路径[root@oldboy ~]# tree -f            #<==看完整的目录结构

.

|-- ./aa

|   `-- ./aa/a1.txt

|-- ./anaconda-ks.cfg

|-- ./install.log

`-- ./install.log.syslog

1 directory, 4 files

11.2.5-i:不打印树枝符号[root@oldboy ~]# tree -if          #<==前面的缩进符号不看

.

./aa

./aa/a1.txt

./anaconda-ks.cfg

./install.log

./install.log.syslog

1 directory, 4 files

11.2.6-F:区分目录和文件[root@oldboy ~]# tree -Fif         #<==目录后会加上斜线

.

./aa/

./install.log.syslog

1 directory, 4 files

12basename

只显示文件的文件名(无目录前缀)

【功能说明】:strip directory and suffix from filenames#<==把目录前缀(suffix)剥离掉

【语法格式】:basename [文件名]

12.1只显示全路径文件的文件名[root@S1 test]# basename /test/stu_102999_1.JPG

stu_102999_1.JPG

[root@S1 test]# basename /test/stu_102999_1.JPG .JPG    #<==不要后缀名

stu_102999_1

[root@S1 test]# basename /etc/sysconfig/network-scripts/ifcfg-eth0

ifcfg-eth0

13dirname

只显示文件的目录前缀名(无文件名)

【功能说明】:strip non-directory suffix from file name#<==剥离非目录前缀(suffix),即文件名

【语法格式】:driname [文件名]

basefname与dirname命令的主要用途:ssh批量分发脚本

13.1只显示全路径文件所在的目录[root@S1 test]# dirname /test/stu_102999_1.JPG

/test

[root@S1 test]# dirname /etc/sysconfig/network-scripts/ifcfg-eth0

/etc/sysconfig/network-scripts

14chattr与lsattr

改变与显示文件的扩展属性;最大的用途是用给文件加锁。

【功能说明】chattr - change file attributes on a Linux file system

lsattr - list file attributes on a Linux second extended file system

【语法格式】:chattr [选项][文件]

14.1选项参数i    immutable(不可变的)    #<==给文件加锁

a    append only              #<==只能向文件追加数据

14.2实践操作

14.2.1-i:给文件加锁,防止创建用户[root@oldboy ~] chattr +i /etc/passwd             #<==+i为加锁,-i为解锁

[root@oldboy ~] lsattr /etc/passwd                #<==检查,有i代表加锁了

----i--------e- /etc/passwd

[root@oldboy ~] useradd li                        #<==创建用户会报错

useradd: cannot open /etc/passwd

[root@oldboy ~] which chattr                      #<==查看这个命令的全路径

/usr/bin/chattr

[root@oldboy ~] mv /usr/bin/chattr /etc/oldboy    #<==给这个命令改名,并且移动位置

[root@oldboy ~] chattr -i /etc/passwd             #<==无法解锁了,因为命令文件不存在

-bash: /usr/bin/chattr: No such file or directory

[root@oldboy ~] mv /etc/oldboy /usr/bin/chattr    #<==把文件的名字和位置改回来

[root@oldboy ~] chattr -i /etc/passwd             #<==给这个文件解锁,-i为解锁

[root@oldboy ~] lsattr /etc/passwd                #<==看不到i了

-------------e- /etc/passwd

[root@oldboy ~] useradd li                        #<==可以创建用户了

[root@oldboy ~] id li

uid=512(li) gid=512(li) groups=512(li)

14.2.2-a:只能给文件追加内容[root@oldboy ~] cd /home/chen/

[root@oldboy chen] ll .bash_history               #<==查看这个文件的属性

-rw-------. 1 chen chen   40 May 30 18:25 .bash_history

[root@oldboy chen] chown root.root .bash_history  #<==修改这个文件的属主和属组

[root@oldboy chen] chmod 606 .bash_history        #<==修改这个文件的属性

[root@oldboy chen] ll .bash_history               #<==检查

-rw----rw-. 1 root root 40 May 30 18:25 .bash_history

[root@oldboy chen] chattr +a .bash_history        #<==只能向文件追加数据

[root@oldboy chen] lsattr .bash_history           #<==检查

-----a-------e- .bash_history append only (a),extent format (e)

~验证[chen@oldboy ~]$ whoami                           #<==切换到用户chen

chen

[chen@oldboy ~]$ >.bash_history                   #<==不能覆盖原有的内容

-bash: .bash_history: Operation not permitted

[chen@oldboy ~]$ rm -f .bash_history              #<==也无法删除这个文件

rm: cannot remove `.bash_history': Operation not permitted

[chen@oldboy ~]$ echo 222 >> .bash_history        #<==只能追加内容到文件里

[chen@oldboy ~]$ cat .bash_history

222

14.2.3系统优化[root@oldboy ~] chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab

#<==给文件/etc/passwd,/etc/shadow,/etc/group,/etc/gshadow,/etc/inittab都统一加锁

[root@oldboy ~] lsattr /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab

----i--------e- /etc/passwd

----i--------e- /etc/shadow

----i--------e- /etc/group

----i--------e- /etc/gshadow

----i--------e- /etc/inittab

[root@oldboy ~] cd /usr/bin/                #<==切换到命令chattr所在的路径

[root@oldboy bin] mv chattr /etc/oldboy     #<==将这个文件移走,这样就无法解锁了,如果还是怕别人找到这个文件,就直接把它给删了,要再用到时,再从别的机器copy过来。

15file

查看文件类型

【功能说明】:determine file type

【语法格式】:file [文件名]

15.1查看文件是哪种类型[root@oldboy ~]# file /etc/sysconfig/network    #<==ASCII纯文本文件

/etc/sysconfig/network: ASCII text

[root@oldboy ~]# file dir1/                     #<==目录

dir1/: directory

[root@oldboy ~]# ll /etc/init.d

lrwxrwxrwx. 1 root root 11 May 11 13:59 /etc/init.d -> rc.d/init.d

[root@oldboy ~]# file /etc/init.d               #<==软链接

/etc/init.d: symbolic link to `rc.d/init.d'

16md5sum

计算和校验文件的md5值,主要用来防止文件在传递的过程中被修改。

【功能说明】:compute and check MD5 message digest#<==计算和校验文件的md5值

【语法格式】:md5sum [OPTION]... [FILE]...

【选项参数】:-c    --checkread MD5 sums from the FILEs and check them

#<==从文件中读取MD5值,并校验该文件是否被修改过

16.1-c:校验文件的md5值

~文件的硬链接和软链接的MD5值都是一样的,因为它们的内容都是一样的[root@S1 ~]# md5sum oldboy.log

f691ab361815e9c9c2ffd8237aeb02b9  oldboy.log

[root@S1 ~]# md5sum oldboy.log_hard_link

f691ab361815e9c9c2ffd8237aeb02b9  oldboy.log_hard_link

[root@S1 ~]# md5sum oldboy.log_soft_link

f691ab361815e9c9c2ffd8237aeb02b9  oldboy.log_softlink

~主要作用是防止文件在传递的过程中被修改[root@S1 ~]# md5sum oldboy.log >md5.log    #<==将计算结果导出到新文件中

[root@S1 ~]# md5sum -c md5.log             #<==校验成功

oldboy.log: OK

[root@S1 ~]# echo 111 >> oldboy.log        #<==修改源文件的内容

[root@S1 ~]# md5sum oldboy.log             #<==md5值改变了

490624261f1370026507061cb908d3cf  oldboy.log

[root@S1 ~]# md5sum -c md5.log             #<==校验失败,因为和原来的md5值不一样了

oldboy.log: FAILED

md5sum: WARNING: 1 of 1 computed checksum did NOT match

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值