【Linux基础命令:mv、rm、which、whereis、cat、head、tail

139 阅读8分钟

Linux基础命令

一、文件管理命令

1. mv (move) 移动改名

mv命令的含义:重命名/移动

mv移动命令:mv [选项] … 源文件或目录… 目标文件或目录

mv重命名命令:mv 源文件 名字

  • -f:强制覆盖,如果目标文件已经存在,则不询问,直接强制覆盖;

  • -i:交互移动,如果目标文件已经存在,则询问用户是否覆盖(默认选项);

  • -n:如果目标文件已经存在,则不会覆盖移动,而且不询问用户;

  • -v:显示文件或目录的移动过程。

示例

[root@localhost test]# mv bbb aaa      //将目录bbb改名为aaa
[root@localhost test]# mv aaa /tmp     //将目录aaa移动到/tmp目录下

2. rm (remove) 删除文件

rm删除文件命令格式:rm [选项] 要删除的文件或目录

  • -f、强制
  • -i、交互
  • -r 递归(删除目录需要)
    例: rm -rf /data/* 递归删除data下所有文件

示例

[root@localhost ~]# rm -rf /     //操作十分危险,系统拒绝执行
rm: 在"/" 进行递归操作十分危险
rm: 使用 --no-preserve-root 选项跳过安全模式
[root@localhost ~]# rm -rf /data/*     //删除/data/目录下所有文件
[root@localhost ~]# rm -rf /data /*    //注意和上一步之间的区别误操作,这样操作等于删根

3. which 查找命令位置

which 用来查看命令的位置。 在PATH变量指定的路径中,搜索某个系统命令的位置 。

which命令格式:which 命令/程序名

示例:

[root@localhost boot]# which ls    //查看ls命令文件的位置
alias ls='ls --color=auto'
        /usr/bin/ls

4. whereis 查找命令具体位置

whereis 用来查找命令的具体位置。可以查看到二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

示例:

[root@localhost ~]# whereis ls       //whereis 可以查找命令具体的位置所在
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

5. find 查找

find命令的特点:

  • 精确查找
  • 实时查找
  • 支持查找条件多

find命令格式: find [查找路径] [OPTION]... [查找条件] [处理动作]

查找路径:指定具体目标路径;不指定则默认为当前目录。

查找条件:可以对文件名、大小、类型、权限等标准进行查找;默认为找出指定路径下的所有文件。

处理动作:对符合条件的文件做操作,默认输出至屏幕(print)。

处理动作:

  • -ls 对查找到的文件列出属性信息

  • -delete 对查找到的文件进行删除

  • -exec COMMAND {} \;

    对查找到的每个文件执行由COMMAND指定的命令,{}: 用于引用查找到的文件名称自身。

  • -ok COMMAND {} \;

    对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认。

常用可选项:

查找类型关键字说明
按名称查找-name根据目标文件的名称进行查找,允许使用“*”及“?”通配符; 如果名字使用通配符,需要加” “来查询
按文件大小查找-size根据目标文件的大小进行查找 一般使用“+”、“-”号设置超过或小于指定的大小作为查找条件 常用的容量单位包括 kB(注意 k 是小写)、MB、GB
按文件属主/属组查找-user/-group根据文件是否属于目标用户进行查找
查找无属主/属组的文件-nouser/-nogroup查找无属主/属组的文件
按文件类型查找-type根据文件的类型进行查找 文件类型包括普通文件(f)、目录(d)、块设备文件(b)、字符设备文件(c)等
按inode号查找-inum根据文件inode号查找
按权限查找-perm按文件权限查找
最大搜索目录深度-maxdepth将你的文件以分级的形式查找,,最多搜索到某级目录
最小搜索目录深度-mindepth将你的文件以分级的形式查找,最少搜索到某级目录
按三种时间查询-三种时间-atime、-mtime、-ctime

示例

1)按文件名称查找 -name

[root@test1 opt]# find /etc/ -name passwd     //在/etc/目录下查找名字叫passwd的文件
/etc/pam.d/passwd
/etc/passwd

2)按文件属主查找 -user、-nouser

[root@test1 opt]# find /mnt -user root    //查找属主为root的文件
/mnt
/mnt/abc
/mnt/ad
/mnt/ad/ad2
/mnt/ad/ad2/bbbbb.txt
/mnt/zhangsanlianjie

3)按文件类型查找 -type

[root@localhost opt]# find /boot -type d    //查找/boot下的目录文件
/boot
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/centos
/boot/grub2
/boot/grub2/i386-pc
/boot/grub2/locale
/boot/grub2/fonts
/boot/grub

4)按目录层级查找

[root@localhost boot]# find /tmp/test -maxdepth 2  -mindepth 2   //只向下看第二级目录
/tmp/test/aaa/1.txt
/tmp/test/aaa/2.txt
/tmp/test/bbb/3.txt

5)按文件权限查找 -perm

[root@localhost opt]# find -perm 644   //查找属主有读写权限、属组和其他人只有读权限的文件
./100.img
./99.img
./a.txt 

6)按文件大小查找 -size

[root@test1 opt]# find -size 1k     //查找(0,1]k的文件
./rh
./rh/aaa
[root@localhost boot]# find /boot/ -size -10M -a -name "*img"   //查找9M以内(含9M)、且名称结尾为img的文件
/boot/grub2/i386-pc/core.img
/boot/grub2/i386-pc/boot.img
[root@localhost opt]# find -size 1G    //查找1G的文件会把所有都显示出来,这是因为1G表示0G到1G之间,不含0G,即(0,1]。
.
./100.img
./99.img

注意:按文件大小查找时,大小范围需注意

  • find -size 1G:查找的是从0G到1G,不包括0G,即(0,1]。
  • find -size 2G:查找的是从1G到2G,不包括1G,即(1,2]。
  • find -size 1024M:查找的是从1023M到1024M,不包括1023M,即(1023,1024]。

7)查找到文件后并做其他处理

# 找到img结尾的文件并删除的三种方式:
[root@localhost opt]# find -name "*.img" -delete               
[root@localhost opt]# find -name "*.img" -exec rm -rf {} ;
[root@localhost opt]# find -name "*.img" | xargs rm -rf

​
[root@localhost opt]# find -name "*.txt"  -ok mv {} /mnt ;   //查找.txt的文件并移动到/mnt目录下,对于每个文件执行命令之前,都会交互式要求用户确认
[root@localhost opt]# find -name "*.txt"  -exec mv {} /mnt ;   //查找.txt的文件并移动到/mnt目录下

8)按时间查找:

#以“天”为单位:
find  -atime  [+/-]#      //按读取时间查找
#     //表示[#,#+1)
+#    //表示[#+1,+∞)
-#    //表示[0,#)
​
find  -mtime            //按文件内容改变时间查找
find  -ctime            //按元数据改变时间查找
​
#以“分钟”为单位:
-amin
-mmin
-cmin
​
find -mtime 10      //10天到11天,[10,11)
find -mtime +10     //代表11天以上,[11,+∞)
find -mtime -10     //10天以内,[0,10)

9)比较“或”与“且”的优先级

[root@localhost opt]# find /etc/ -type d -o -type l |wc -l    //查找/etc目录下的目录和链接文件并统计数量
1008
[root@localhost opt]# find /etc/ -type d -o -type l -ls |wc -l   //查找/etc目录下的目录和链接文件,并显示链接文件的详细信息后统计数量
274 
[root@localhost opt]# find /etc/ -type d -o -type l -a -ls |wc -l    //此条命令与上条命令对比实际隐藏了一个-a(且),先执行了-type 1 -a -ls,处理动作只显示了链接文件
274
[root@localhost opt]# find /etc/ ( -type d -o -type l ) -a -ls |wc -l  //如果“或”和“且”实在需要一起显示,可以使用括号和转义符
1008

10)反向查找,使用-not或!

[root@localhost d02]# ll      //查看目录下的文件及其属性
总用量 0
-rw-r--r--. 1 root root 0 3月  16 08:56 1.txt
-rw-r--r--. 1 root root 0 3月  16 08:56 2.txt
drwxr-xr-x. 2 root root 6 3月  16 08:55 dir0201
drwxr-xr-x. 2 root root 6 3月  16 08:55 dir0202
-rw-r--r--. 1 root root 0 3月  16 08:56 file01
-rw-r--r--. 1 root root 0 3月  16 08:56 file02
[root@localhost dir02]# find -not -type d   //使用-not,查找不是文件类型不是目录的文件
./1.txt
./2.txt
./file01
./file02
[root@localhost d02]# find ! -type d   //也可以使用!来代替-not,进行反向查找
./1.txt
./2.txt
./file01
./file02
[root@localhost d02]# find ! -type d ! -name "*.txt"   //查找不是目录且名称不以.txt结尾的文件
./file01
./file02

11)找到/mnt目录下以f开头的文件,并全部加上后缀.bak

 find /mnt -name f* -exec mv {} {}.bak ;

12)找到文件并删除的三种方式:

 find -name "*.img" -delete               
 find -name "*.img" -exec rm -rf {} ;
 find -name "*.img" | xargs rm -rf

6.cat 显示命令

cat命令格式:cat [选项] 文件名

tac:倒叙显示文本命令

选项效果
-n显示行号包括空行
-b跳过空白行编号
-s将所有的连续的多个空行替换为一个空行(压缩成一个空行)
-A显示隐藏字符
[root@localhost opt]# cat /etc/centos-release   //查看内核版本
[root@localhost opt]# cat /etc/sysconfig/network-scripts/ifcfg-ens33   //查看网络配置 

7. head 显示文本前内容

head命令格式:head -n 3 /etc/passwd

可以显示文件或标准输入的前面行,默认为十行

8. tail 显示文本后内容

tail命令格式:tail -n 10 /etc/passwd

tail 和head 相反,查看文件或标准输入的倒数行
-f 实时查看文件后十行

tail -f /var/log //查看日志