Linux查找定位文件命令

1.pwd命令

pwd命令用于显示用户当前所处的工作目录,英文全称为"print working directory"。

[root@localhost etc]# pwd
/etc

2.cd 命令

cd命令用于切换当前的工作路径,英文全称为"change directory",语法格式为"cd [参数] [目录]"。

cd -:返回上一次所处的目录。

cd..:进入上级目录。

cd~:切换到当前用户的家目录。

cd~username:切换到其他用户的家目录。

[root@localhost ~]# cd /etc/
[root@localhost etc]# cd /bin
[root@localhost bin]# cd -
/etc
[root@localhost etc]# cd ~
[root@localhost ~]# 

 3.ls命令

.ls命令用于显示目录中的文件信息,英文全称为"list",语法格式为"ls [参数] [文件名称]"。

-a,--all:列出所有文件,包括以(.)开头的隐藏文件。

-l:单列输出(一行输出一个文件)。

-d:将目录名像其他文件一样列出,而不是列出它们的内容。

-t:按时间信息排序。

参数 作用
-a,-all 列出所有文件,包括以(.)开头的隐藏文件
-l 一行输出一个文件(单列输出)
-d 将目录名像其他文件一样列出,而不是列出它们的内容
-t 按时间信息排序

 单列输出当前目录的所有信息:

[root@localhost ~]# ls -al
total 22860
dr-xr-x---. 17 root root     4096 Oct 20 10:57 .
dr-xr-xr-x. 17 root root      224 Oct 18 23:55 ..
-rw-------.  1 root root     1392 Oct 19 00:02 anaconda-ks.cfg
-rw-------.  1 root root        1 Oct 20 10:57 .bash_history
-rw-r--r--.  1 root root       18 Aug 13  2018 .bash_logout
-rw-r--r--.  1 root root      176 Aug 13  2018 .bash_profile
-rw-r--r--.  1 root root      176 Aug 13  2018 .bashrc
drwx------. 16 root root     4096 Nov 11  2023 .cache
drwx------. 15 root root     4096 Oct 19 19:31 .config
drwxr-xr-x.  2 root root       24 Oct 19 15:42 csdn.net
-rw-r--r--.  1 root root      100 Aug 13  2018 .cshrc
drwx------.  3 root root       25 Oct 19 00:03 .dbus
drwxr-xr-x.  2 root root        6 Oct 19 00:04 Desktop

查看/etc目录的属性信息:

[root@localhost ~]# ls -ld /etc/
drwxr-xr-x. 135 root root 8192 Nov 11  2023 /etc/

4.tree命令

tree命令用于以树状图的格式列出目录内容及结构

使用ls查看目录中的文件:

[root@localhost ~]# ls
A  anaconda-ks.cfg  csdn.net  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos

使用tree查看目录中的文件:

[root@localhost ~]# tree
.
├── A
│   └── B
│       └── C
├── anaconda-ks.cfg
├── csdn.net
│   └── index.html
├── Desktop
├── Documents
├── Downloads
├── initial-setup-ks.cfg
├── Music
├── Pictures
├── Public
├── Templates
└── Videos

12 directories, 3 files

5.find命令

find命令用于按照指定条件查找文件所对应的位置,语法格式为"find [查找范围] 寻找条件"。

在Linux中,搜索工作一般都是通过find命令来完成的,它可以使用不同的文件特性作为寻找条件(如文件名、大小、修改时间、权限信息等),一旦匹配成功则默认将信息显示到屏幕上。

find命令的参数及作用如下:

参数 作用
-name 匹配名称
-perm 匹配权限(mode 为完全匹配,-mode 为包含即可)

-user 

匹配所有者
 -group 匹配所有组
-mtime -n +n 匹配修改内容的时间(-n 指n天内,+n 指n天以前)
-atime -n +n 匹配访问文件的时间(-n 指n天内,+n 指n天以前)
-ctime -n +n 匹配修改文件权限的时间(-n 指n天内,+n 指n天以前)
-nouser 匹配无所有者的文件
-nogroup 匹配无所属组的文件
-nwer   f1  !f2 匹配比 f1 新但比 f2 旧的文件
--type b/ d/ c/ p/ l/ f

匹配文件类型(后面的字母依次表示块设备、目录、字符设备、

管道、链接文件、文本文件)

-size 匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件)
-prune 忽略某个目录
-exec.... { } \; 后面可跟用于进一步处理搜索结果的命令

-exec参数用于把find命令搜索到的结果由紧随其后的命令做进一步处理,由于find命令对参数有特殊要求,因此虽然exec是长格式形式,但它前面依然只需要一个减号(-)

实例演示:

Linux系统的配置文件会保存到 /etc目录中,获取该目录中所有以host开头的文件列表:

[root@localhost ~]# find /etc -name "host*"
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/avahi/hosts
/etc/hostname

 6.locate命令

locate命令用于按照名称快速搜索文件所对应的位置,语法格式为"locate 文件名称"。

使用find命令进行全盘搜索虽然更准确,但是效率较低。locate命令可以查找一些常见且又知道大概名称的文件。

在使用locate命令时,先使用updatedb命令生成一个索引文件,这个库文件的名字是 /var/lib/mlocate/mlocate.db,后续再使用locate命令搜索文件时就是在该库中进行查找操作,速度会块很多。

第一次使用locate命令之前,先执行updatedb命令生成索引数据库再进行查找:

[root@localhost home]# updatedb
[root@localhost home]# ls -l /var/lib/mlocate/mlocate.db 
-rw-r-----. 1 root slocate 2974385 Oct 20 16:15 /var/lib/mlocate/mlocate.db

使用locate命令搜索出包含”whereis“ 名称的文件所在位置:

[root@localhost home]# locate whereis
/usr/bin/whereis
/usr/share/bash-completion/completions/whereis
/usr/share/man/man1/whereis.1.gz

7.whereis命令

whereis命令用于按照名臣快速搜索二进制程序(命令)、源代码以及帮助文件所对应的位置,语法格式为"whereis 命令名称"。

whereis命令也是基于updatedb命令所生成的索引库文件进行搜索,它与locate命令的区别是不关心那些相同名称的文件,仅仅是快速找到对应的命令文件及其帮助文件所在的位置。

使用whereis命令分别查找出 ls 和 pwd 命令所在的位置:

[root@localhost home]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@localhost home]# whereis pwd
pwd: /usr/bin/pwd /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz

8.which命令

which命令用于按照指定名称快速搜索二进制程序(命令)所对应位置,语法格式为"which 命令名称"。

which命令是在PATH变量所指定的路径中,按照指定条件搜索命令所在的路径。也就是说,如果我们即不关心同名文件( find 与 locate),也不关心命令所对应的源代码和帮助文件(whereis),仅仅是想找到命令本身所在路径,那么which命令再适合不过了。

查找locate 和 whereis 命令所对应的路径:

[root@localhost home]# which locate
/usr/bin/locate
[root@localhost home]# which whereis
/usr/bin/whereis

猜你喜欢

转载自blog.csdn.net/qq_41255880/article/details/127426333
今日推荐