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

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

1 ls

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:显示隐藏文件

[[email protected] test]# ls -a    #<==以点开头的文件是隐藏文件
.  ..  dir1  dir2  dir3  file1.txt  file2.txt  file3.txt

1.2.2 -l:以长格式显示条目

[[email protected] 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.3 ll:(别名)以长格式显示条目,并且自动带颜色的区分

[[email protected] 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
[[email protected] test]# alias ll    #<==查看别名
alias ll=‘ls -l --color=auto --time-style=long‘
[[email protected] test]# echo "alias ll=‘ls -l --color=auto --time-style=long‘" >>/etc/bashrc
#<==使别名永久生效
#<==将配置好的别名命令,追加到/etc/bashrc的配置文件中
#<==单引号不具有变量置换功能,输出时所见即所得
#<==双引号有变量置换功能,解析变量后输出,不加引号相当于双引号,但一般常用双引号
#<==注意:定义别名永久生效:
/etc/profile                #<==全局生效
/etc/bashrc                 #<==全局生效
~/.bashrc                   #<==当前用户生效
/root/.bashrc               #<==当前用户生效
[[email protected] test]# which ls    #<==查看命令的全路径
alias ls=‘ls --color=auto‘
        /bin/ls

1.2.4 -h:显示条目的单位(K或M)

[[email protected] 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:只显示目录条目

[[email protected] test]# ls -d dir1   #<==想显示指定的目录,后面必须加上目录名
dir1
[[email protected] test]# ls -d        #<==如果后面不加目录,则显示当前的目录(即一个点)

1.2.6 -F:给不同的条目加不同的指示符

[[email protected] 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
[[email protected] test]# ls -F /bin/  #<==还有可能会加上符号:@  *
alsaunmute*          [email protected]

1.2.7 -p:只给目录条目加指示符:斜线

[[email protected] 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参数,逆序排序,搞定。

[[email protected] test]# mkdir dir10         #<==再创建一个目录,注意时间
[[email protected] test]# touch file20.txt    #<==再创建一个文件
[[email protected] 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

[[email protected] 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  文件类型 权限 硬链接数 属主 属组 大小 修改日期 文件名

2 cd

切换目录的命令。

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

【语法格式】:cd [dir]

2.1 实践操作

2.1.1 切换到家目录

[[email protected] mnt]# cd    #<==不管在哪个目录下,直接cd就可以切换回当前用户的家目录
[[email protected] ~]#pwd
/root

2.1.2 切换到网卡配置文件所在的目录

[[email protected] ~]# cd /etc/sysconfig/network-scripts/
#<==这里写的是绝对路径,必须以/开头,也可以一步一步切换进去(即可以用相对路径)
[[email protected] network-scripts]# pwd
/etc/sysconfig/network-scripts

2.1.3 切换到上一次所在的目录

[[email protected] network-scripts]# pwd             #<==先查看当前目录
/etc/sysconfig/network-scripts
[[email protected] network-scripts]# cd -            #<==cd后加杠,代表切换回上一次所在的目录
/root
[[email protected] ~]# cd -                          #<==cd后加杠,又回到上一次所在的目录了
/etc/sysconfig/network-scripts
[[email protected] network-scripts]# echo $OLDPWD    #<==它的原理是由这个环境变量控制着
/root

2.1.4 切换到上一级目录

[[email protected] network-scripts]# pwd             #<==先查看当前目录
/etc/sysconfig/network-scripts
[[email protected] network-scripts]# cd ..           #<==也可以写成:cd ../
[[email protected] sysconfig]# pwd
/etc/sysconfig

2.1.5 切换到前两级目录

[[email protected] network-scripts]# pwd             #<==先查看当前目录
/etc/sysconfig/network-scripts
[[email protected] network-scripts]# cd ../..        #<==也可以写成:cd ../../
[[email protected] etc]# pwd
/etc

3 cp

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

【功能说明】:copy files and directories

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

3.1 选项参数

-a※  -pdr              #<==递归复制,并且操持文件的属性:权限,属主和属组,时间戳……
-p    --preserve        #<==复制时,保持文件原有的属性
-r※  --recursive       #<==递归复制,即复制目录
-d                      #<==复制时,保持链接文件的属性
-i    --interactive     #<==交互式复制,如果文件已存在,覆盖前会有提示

3.2 实践操作

3.2.1 复制文件

[[email protected] test]# cp file3.txt file4.txt    #<==复制文件
[[email protected] 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:复制时保持属性

[[email protected] test]# cp -a file{1,5}.txt       #<==-a相当于-pdr,它的作用是复制时可以保持文件或目录的属性,复制时也可以用大括号来简写
[[email protected] 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:覆盖前会先提示,别名

[[email protected] test]# cp -a file{1,5}.txt          #<==覆盖前会先提示,别名已被定义
cp: overwrite `file5.txt‘? y
[[email protected] test]# alias cp                     #<==查看别名
alias cp=‘cp -i‘
[[email protected] test]# \cp -a file{1,5}.txt         #<==命令前加斜线,忽略别名
[[email protected] test]# which cp                     #<==查看命令的全路径
alias cp=‘cp -i‘
        /bin/cp
[[email protected] test]# /bin/cp -a file{1,5}.txt     #<==写命令的全路径,也能忽略别名
[[email protected] 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 备份文件的简写(修改前必须先备份)

[[email protected] test]# cp /etc/ssh/ssh_config{,.ori}
#<==大括号里面的逗号一定不能忘记写,{}展开后,原文件什么都不加,复制的文件后加上(.ori)
[[email protected] 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:保持属性

[[email protected] data]# chmod 777 oldboy.txt 
#<==修改文件的属性,复制时看是否将属性也复制过去
[[email protected] data]# ll oldboy.txt             #<==验证文件的属性是否修改了
-rwxrwxrwx. 1 root root 0 May 11 18:55 oldboy.txt
[[email protected] data]# cp -p oldboy.txt /tmp/    #<==复制时加-p参数代表保持文件的属性
[[email protected] data]# ll /tmp/oldboy.txt        #<==文件的属性也跟着过来了
-rwxrwxrwx. 1 root root 0 May 11 18:55 /tmp/oldboy.txt

3.2.6 -r:复制目录

[[email protected] /]# cp data/ /tmp/
#<==将目录/data/复制到/tmp下,-r参数用来复制目录,必须指定,要不复制时会报错
cp: omitting directory `data/‘                #<==报错,因为cp默认不能复制目录
[[email protected] /]# cp -r data/ /tmp/            #<==递归复制,搞定
[[email protected] /]# ll -d /tmp/data/
drwxr-xr-x. 2 root root 4096 May 11 19:05 /tmp/data/

4 find

查找命令,参数特别多。

【功能说明】: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天内创建的文件

[[email protected] test]# find ./ -type f -mtime -7
#<==7代表第7天,减7代表最近7天,+7代表7天以前

4.2.2 文件取反

[[email protected] test]# find ./ -type f ! -name "*.txt"
#<==!代表取反,只要后缀名是.txt的通通不要

4.2.3 使用-exec移动文件

~ 方式一

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

~ 方式二

[[email protected] test]# mv `find ./ -type f -name "file2.txt"` /tmp/ 
#<==使用反引号将find命令给包起来,这样才能符合mv的格式
[[email protected] data]# mv $(find ./ -type f -name "file2.txt")/tmp/ 
#<==这条命令也可以,【`命令`==$(命令)】

4.2.4 和mv搭配使用

[[email protected] ~]# cd /data/ 
[[email protected] data]# touch {1..10}.txt
#<==准备练习素材
[[email protected] ~]# mv `find /data/ -type f -name "*.txt"` /tmp/
#<==使用反引号将find命令给包起来,这样才能符合mv的格式
[[email protected] ~]# find /data/ -type f -name "*.txt" -exec mv {} /tmp/ \;
#<==这个写法和上面的写法是一样的

4.2.5 -type,-mtime,-exec

[[email protected] ~]# find /logs -type f -mtime +7 -exec rm {} \;  
#<==在/logs目录中查找更改时间在7日以前的文件并删除,-exec后加执行命令:rm命令,{}代表find找到的内容,加反斜线的作用是转义字符,转义分号
[[email protected] ~]# rm `find /logs -type f -mtime +7`
#<==等价于上面那条命令

4.2.6 和xargs搭配使用

[[email protected] ~]# find ./ -type f -name "*.log" -mtime +15 | xargs rm
#<==删15天以前的文件,在当前目录下找以.log结尾的文件,修改时间在15天之前的,找到后把结果放进管理内,出来后给xargs处理,xargs把这些结果全部放在一行来显示,最后再给rm命令把它们给删掉。
[[email protected] ~]# find ./ -type f -name "*.log" -mtime +15 | xargs rm -rf
#<==删15天以前的目录,慎用。-r为递归删除。

4.2.7 -maxdepth与-perm:指定查找深度和按文件的权限掩码查找

[[email protected] ~]# 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

5 mkdir

创建目录的命令。为了保持好的习惯,只要创建目录,无论它是否存在,一律加-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、绝对路径:

[[email protected] ~]# mkdir /data
#<==这里创建目录时,用的是绝对路径,需在前面加/
[[email protected] ~]# ls -ld /data/
#<==验证,只看/data这个目录的信息,-d参数代表只看目录,后面必须加上要查看的目录名,否则只显示当前的目录(即一个点)
drwxr-xr-x. 2 root root 4096 May  9 14:59 /data/

~ 2、相对路径:

[[email protected] ~]# cd /
#<==先切换到根目录
[[email protected] /]# mkdir data
#<==再创建目录,写相对路径(注:无论处在哪个目录,都可以写绝对路径)

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

要求:在根下创建一个a目录,再在a目录下创建b目录

[[email protected] ~]# mkdir /a/b -p
#<==先创建目录/a,再在这个基础上创建目录b
[[email protected] ~]# ls -ld /a/b/
#<==验证,只看/a/b/这个目录的信息
drwxr-xr-x. 2 root root 4096 May  9 15:03 /a/b/

[[email protected] ~]# yum install tree -y
#<==先安装tree命令,虚拟机必须能上网噢
[[email protected] ~]# LANG=en
#<==修改字符集,防止乱码的干扰
[[email protected] ~]# tree /a
#<==用tree命令来验证,查看/a目录的目录树
/a
`-- b
1 directory, 0 files

5.2.3 -pv:递归创建更多目录

[[email protected] ~]# 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‘
[[email protected] ~]# tree /a /b
#<==tree命令后可加多个目录
/a
|-- a1
|-- a2
`-- a3
/b
|-- b1
|-- b2
`-- b3
 
6 directories, 0 files

6 mv

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

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

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

6.1 选项参数

-f    --force          #<==强制移动
-i    --interactive    #<==交互式移动

6.2 实践操作

6.2.1 移动文件

[[email protected] test]# mv file1.txt /tmp/     #<==将文件file1.txt移动到/tmp目录下
[[email protected] test]# ll /tmp/file1.txt      #<==验证
-rw-r--r--. 1 root root 0 May 11 15:16 /tmp/file1.txt

6.2.2 移动目录

[[email protected] test]# mv dir1/ /tmp/         #<==将dir1目录移动到/tmp目录下
[[email protected] test]# ll -d /tmp/dir1/       #<==验证
drwxr-xr-x. 2 root root 4096 May 11 15:17 /tmp/dir1/

6.2.3 重命名

[[email protected] test]# mv file{4,40}.txt      #<==将文件file4.txt重命名为file40.txt
[[email protected] test]# ll file40.txt          #<==验证
-rw-r--r--. 1 root root 0 May 11 17:03 file40.txt

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

~ 问题:将以.txt结尾的文件,移动到/tmp目录下

[[email protected] ~]# cd /data/ 
[[email protected] data]# touch {1..10}.txt      #<==准备练习素材
[[email protected] ~]# mv `find /data/ -type f -name "*.txt"` /tmp/
#<==使用反引号将find命令给包起来,这样才能符合mv的格式
[[email protected] data]# mv $(find ./ -type f -name "*.txt") /tmp/
#<==这条命令也可以,【`命令`==$(命令)】
[[email protected] ~]# ll /data/                 #<==/data目录下空了
total 0
[[email protected] ~]# 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

7 pwd

显示当前路径。

【功能说明】: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 查看当前所在的目录

[[email protected] ~]# echo $PWD        #<==显示当前的路径,是从这个变量中获取到的
/root
[[email protected] ~]# pwd              #<==查看当前的目录
/root

7.2.2 -P:显示物理路径

[[email protected] ~]# 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
[[email protected] ~]# cd /etc/init.d/
[[email protected] init.d]# pwd         #<==pwd显示的是链接路径
/etc/init.d
[[email protected] init.d]# pwd -P
#<==目录连接链接时,pwd -P 显示出实际路径,而非使用链接(link)路径
/etc/rc.d/init.d

8 rename

文件重命名。

【功能说明】:Rename files

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

8.1 实践操作

8.1.1 批量重命名

~ 删除关键字

[[email protected] test]# touch stu_102999_{1..5}_finished.jpg
[[email protected] test]# rename "_finished" "" *
[[email protected] 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

~ 替换关键字

[[email protected] test]# rename "jpg" "JPG" *
[[email protected] 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

9 rm

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:强制删除一个文件

[[email protected] a]# rm -f 1.txt

9.2.2 -r:删除一个目录

[[email protected] a]# rm -fr a2/

9.2.3 用mv替代rm

[[email protected] test]# mv file3.txt /tmp/
[[email protected] test]# ll /tmp/file3.txt 
-rw-r--r--. 1 root root 0 May 11 15:16 /tmp/file3.txt

9.2.4 用find来删除

[[email protected] test]# find ./ -type f -name "file2.txt" |xargs rm
[[email protected] test]# rm `find ./ -type f -name "file2.txt"`
[[email protected] test]# rm $(find ./ -type f -name "file2.txt")

10 touch

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

【功能说明】:change file timestamps

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

10.1 选项参数

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

~ 访问时间的说明

access-atime    #<==访问文件内容
modify-mtime    #<==修改文件内容
change-ctime    #<==文件属性改变

10.2 实践操作

10.2.1 创建一个文件oldboy.txt

[[email protected] ~]# touch oldboy.txt

10.2.2 同时创建多个文件

[[email protected] ~]# touch stu0{1..5}.txt
[[email protected] ~]# 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:改变文件的访问时间

[[email protected] ~]# 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
[[email protected] ~]# touch -a oldboy.txt         #<==改变访问时间的时间戳
[[email protected] ~]# 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:改变文件的修改时间

[[email protected] ~]# touch -m oldboy.txt         #<==只改变修改时间的时间戳
[[email protected] ~]# 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的时间也改变

11 tree

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

【功能说明】: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:显示隐藏文件

[[email protected] ~]# 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:指定目录树的层次

[[email protected] ~]# tree /chen/        #<==可以看到默认是多层目录树结构
/chen/
`-- a
    `-- b
        `-- c
 
3 directories, 0 files
 
[[email protected] ~]# tree -L 2 /chen/   #<==只看两层目录树结构
/chen/
`-- a
    `-- b
 
2 directories, 0 files

11.2.3 -d与-h:只看目录,并显示大小

[[email protected] ~]# tree -hd ./        #<==只看当前目录树的目录并且显示单位
./
|-- [4.0K]  -
|-- [4.0K]  a
`-- [4.0K]  b
 
3 directories

11.2.4 -f:看目录完整路径

[[email protected] ~]# tree -f            #<==看完整的目录结构
.
|-- ./aa
|   `-- ./aa/a1.txt
|-- ./anaconda-ks.cfg
|-- ./install.log
`-- ./install.log.syslog
 
1 directory, 4 files

11.2.5 -i:不打印树枝符号

[[email protected] ~]# tree -if          #<==前面的缩进符号不看
.
./aa
./aa/a1.txt
./anaconda-ks.cfg
./install.log
./install.log.syslog
 
1 directory, 4 files

11.2.6 -F:区分目录和文件

[[email protected] ~]# tree -Fif         #<==目录后会加上斜线
.
./aa/
./install.log.syslog
1 directory, 4 files

12 basename

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

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

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

12.1 只显示全路径文件的文件名

[[email protected] test]# basename /test/stu_102999_1.JPG 
stu_102999_1.JPG
[[email protected] test]# basename /test/stu_102999_1.JPG .JPG    #<==不要后缀名
stu_102999_1
[[email protected] test]# basename /etc/sysconfig/network-scripts/ifcfg-eth0    
ifcfg-eth0

13 dirname

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

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

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

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

13.1 只显示全路径文件所在的目录

[[email protected] test]# dirname /test/stu_102999_1.JPG 
/test

[[email protected] test]# dirname /etc/sysconfig/network-scripts/ifcfg-eth0 
/etc/sysconfig/network-scripts

14 chattr与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:给文件加锁,防止创建用户

[[email protected] ~] chattr +i /etc/passwd             #<==+i为加锁,-i为解锁
[[email protected] ~] lsattr /etc/passwd                #<==检查,有i代表加锁了
----i--------e- /etc/passwd      
[[email protected] ~] useradd li                        #<==创建用户会报错
useradd: cannot open /etc/passwd
 
[[email protected] ~] which chattr                      #<==查看这个命令的全路径
/usr/bin/chattr
[[email protected] ~] mv /usr/bin/chattr /etc/oldboy    #<==给这个命令改名,并且移动位置
[[email protected] ~] chattr -i /etc/passwd             #<==无法解锁了,因为命令文件不存在
-bash: /usr/bin/chattr: No such file or directory
[[email protected] ~] mv /etc/oldboy /usr/bin/chattr    #<==把文件的名字和位置改回来
[[email protected] ~] chattr -i /etc/passwd             #<==给这个文件解锁,-i为解锁
[[email protected] ~] lsattr /etc/passwd                #<==看不到i了
-------------e- /etc/passwd
[[email protected] ~] useradd li                        #<==可以创建用户了
[[email protected] ~] id li
uid=512(li) gid=512(li) groups=512(li)

14.2.2 -a:只能给文件追加内容

[[email protected] ~] cd /home/chen/
[[email protected] chen] ll .bash_history               #<==查看这个文件的属性
-rw-------. 1 chen chen   40 May 30 18:25 .bash_history
[[email protected] chen] chown root.root .bash_history  #<==修改这个文件的属主和属组
[[email protected] chen] chmod 606 .bash_history        #<==修改这个文件的属性
[[email protected] chen] ll .bash_history               #<==检查
-rw----rw-. 1 root root 40 May 30 18:25 .bash_history
[[email protected] chen] chattr +a .bash_history        #<==只能向文件追加数据
[[email protected] chen] lsattr .bash_history           #<==检查
-----a-------e- .bash_history append only (a),extent format (e)

~ 验证

[[email protected] ~]$ whoami                           #<==切换到用户chen
chen
[[email protected] ~]$ >.bash_history                   #<==不能覆盖原有的内容
-bash: .bash_history: Operation not permitted
[[email protected] ~]$ rm -f .bash_history              #<==也无法删除这个文件
rm: cannot remove `.bash_history‘: Operation not permitted
[[email protected] ~]$ echo 222 >> .bash_history        #<==只能追加内容到文件里
[[email protected] ~]$ cat .bash_history 
222

14.2.3 系统优化

[[email protected] ~] chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab 
#<==给文件/etc/passwd,/etc/shadow,/etc/group,/etc/gshadow,/etc/inittab都统一加锁
[[email protected] ~] 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
[[email protected] ~] cd /usr/bin/                #<==切换到命令chattr所在的路径
[[email protected] bin] mv chattr /etc/oldboy     #<==将这个文件移走,这样就无法解锁了,如果还是怕别人找到这个文件,就直接把它给删了,要再用到时,再从别的机器copy过来。

15 file

查看文件类型

【功能说明】:determine file type

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

15.1 查看文件是哪种类型

[[email protected] ~]# file /etc/sysconfig/network    #<==ASCII纯文本文件
/etc/sysconfig/network: ASCII text
 
[[email protected] ~]# file dir1/                     #<==目录
dir1/: directory
 
[[email protected] ~]# ll /etc/init.d
lrwxrwxrwx. 1 root root 11 May 11 13:59 /etc/init.d -> rc.d/init.d
[[email protected] ~]# file /etc/init.d               #<==软链接
/etc/init.d: symbolic link to `rc.d/init.d‘

16 md5sum

计算和校验文件的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值都是一样的,因为它们的内容都是一样的

[[email protected] ~]# md5sum oldboy.log
f691ab361815e9c9c2ffd8237aeb02b9  oldboy.log
[[email protected] ~]# md5sum oldboy.log_hard_link 
f691ab361815e9c9c2ffd8237aeb02b9  oldboy.log_hard_link
[[email protected] ~]# md5sum oldboy.log_soft_link 
f691ab361815e9c9c2ffd8237aeb02b9  oldboy.log_softlink

~ 主要作用是防止文件在传递的过程中被修改

[[email protected] ~]# md5sum oldboy.log >md5.log    #<==将计算结果导出到新文件中
[[email protected] ~]# md5sum -c md5.log             #<==校验成功
oldboy.log: OK
 
[[email protected] ~]# echo 111 >> oldboy.log        #<==修改源文件的内容
[[email protected] ~]# md5sum oldboy.log             #<==md5值改变了
490624261f1370026507061cb908d3cf  oldboy.log
[[email protected] ~]# md5sum -c md5.log             #<==校验失败,因为和原来的md5值不一样了
oldboy.log: FAILED
md5sum: WARNING: 1 of 1 computed checksum did NOT match
时间: 2024-08-08 01:29:39

Linux 文件和目录操作命令(17个)的相关文章

Linux文件和目录操作命令01

2.1 pwd(5*****) (print work directory)打印工作目录(显示当前所在路径) 后面可以接 -L 默认情况下接的也是-L(logical)的 此种情况显示的是逻辑路径(相当于win的一样) -P(physical)的话,就会把真实的物理路径全部显示出来 [[email protected] local]# pwd /usr/local [[email protected] local]# cd - 返回上一次所在路径 /root [[email protected]

Linux常用命令之文件和目录操作命令

以下是linux操作系统常用的文件和目录操作命令: cd /home 进入 '/ home' 目录' cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd 进入个人的主目录 cd ~user1 进入个人的主目录 cd - 返回上次所在的目录 pwd 显示工作路径 ls 查看目录中的文件 ls -F 查看目录中的文件 ls -l 显示文件和目录的详细资料 ls -a 显示隐藏文件 ls *[0-9]* 显示包含数字的文件名和目录名 tree 显示文件和目录由根目录开始的树形结构(1)

Linux 150命令之 文件和目录操作命令 ls

文件和目录操作命令 ls 查看文件和目录查看显示详信息 ? ls 工具的参数 ls -l 查看文件详细信息 ls -h 查看文件的大小 ls -ld 只查看目录信息 ls –F 给不同文件加上不同标记 ls –a 查看隐藏文件 ls -lrt 逆序时间排序 -r 逆序 -t 文件目录按照时间排序 ls –i查看inode 号

Linux常用命令———文件和目录操作命令(18个)

文件和目录操作命令(18个) ls(列出目录内容和属性) 全拼list,功能是列出目录的内容及其内容属性信息. -l(long)              长格式  注:-l 显示的时间是mtime -d --directorys当遇到目录时列出目录本身而非目录内的文件 ls –d   #只显示. (.表示目录本身) ls –d */ #显示当前目录下的目录及其子目录 -r --reverse:            倒序显示(按字母) ls -lrt  #显示最近修改的文件 -R --recu

Linux 文件和目录的属性及权限

一.权限位说明 Linux 文件或目录的权限位是由9个权限位来控制,每三位为一组,它们分别是文件属主(Owner)的读.写.执行,用户组(Group)的读.写.执行以及(Other)其它用户的读.写.执行: 文件属主(Owner):读 read.写 write.执行 x 文件用户组(Group):读 r.写 w .执行 x 其它用户(Other):读 r. 写 w. 执行 x 如果权限位不可读.不可写.不可执行,则用|来表示. 后面还会有特殊的权限位:t T s S X + Linux 系统硬链

Linux温习(三)Linux文件和目录管理

关于Linux目录的几个常见概念 路径 对文件位置信息的描述机制,是指从树型目录中的某个目录层次到其内某个文件的一条通路:分为相对路径和绝对路径: 工作目录 登入系统后,用户始终处于某个目录中,此目录即为工作目录,或称作当前目录 根目录 Linux树型目录结构的最顶层目录 用户"家"目录 也称用户主目录,通常是位于/home目录中跟用户名称相同的一个目录,用户登录系统后默认处于此目录中 路径相关 描述:路径是从一个目录到另一个目录或文件的通路,由各级目录的名称来标识 绝对路径 从根目录

【转】第七章、Linux 文件与目录管理

原文网址:http://vbird.dic.ksu.edu.tw/linux_basic/0220filemanager.php 第七章.Linux 文件与目录管理 最近升级日期:2009/08/26 在第六章我们认识了Linux系统下的文件权限概念以及目录的配置说明. 在这个章节当中,我们就直接来进一步的操作与管理文件与目录吧!包括在不同的目录间变换. 创建与删除目录.创建与删除文件,还有寻找文件.查阅文件内容等等, 都会在这个章节作个简单的介绍啊! 1. 目录与路径 1.1 相对路径与绝对路

鸟哥的Linux私房菜_基础版_学习笔记3:第七章 Linux文件与目录管理

第七章 Linux文件与目录管理 7.1目录与路径: 7.1.1相对路径与绝对路径: 绝对路径:路径的写法『一定由根目录 / 写起』,例如: /usr/share/doc 这个目录. 相对路径:路径的写法『不是由 / 写起』,例如由 /usr/share/doc 要到 /usr/share/man 底下时,可以写成:『cd ../man』这就是相对路径的写法啦!相对路径意指『相对於目前工作目录的路径!』 7.1.2目录的相关操作: . 代表此层目录 .. 代表上一层目录 - 代表前一个工作目录

(转)Linux 文件和目录的属性

原文:https://www.cnblogs.com/kzloser/articles/2673790.html https://www.cnblogs.com/danh/archive/2011/01/11/1932975.html 内容源于: 鸟哥的linux私房菜 链接如下: Linux 的文件权限与目录配置 Linux 磁盘与文件系统管理 Linux 文件与目录管理 目录 Linux文件属性 [文件属性解析(SUID/SGID/SBIT)][隐藏属性]修改文件属性 [chgrp][cho