translated

This commit is contained in:
geekpi 2018-12-17 08:59:20 +08:00
parent 78dd2b4219
commit 65332a0e14
2 changed files with 153 additions and 153 deletions

View File

@ -1,153 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Four Easy Ways to Search Or Find Files And Folders in Linux)
[#]: via: (https://www.2daygeek.com/four-easy-ways-to-search-or-find-files-and-folders-in-linux/)
[#]: author: (Prakash Subramanian https://www.2daygeek.com/author/prakash/)
Four Easy Ways to Search Or Find Files And Folders in Linux
======
Linux admins cant able to leave a day without performing a file search as this one of the activity for their routine.
Its good to know all the file search stuffs because it would help you in many ways when you are working on headless server.
These commands are not complicate to remember because these are using a standard syntax.
This can be performed through Four Linux commands and each command has their own unique feature.
### Method-1: Search Files And Folders in Linux Using find Command
Find command is widely used and very famous command to search files and folders in Linux. It searches given files in the current directory and recursively through its sub-directories based on the search criteria.
It allow users to perform all kind of file searches based on the criteria lie by size, name, owner, group, type, permissions, date, and other criteria.
Run the following command to find a given file in system.
```
# find / -iname "sshd_config"
/etc/ssh/sshd_config
```
Run the following command to find a given folder in system. To search a folder in Linux we need to use `-type` parameter.
```
# find / -type d -iname "ssh"
/usr/lib/ssh
/usr/lib/go/src/cmd/vendor/golang.org/x/crypto/ssh
/usr/lib/go/pkg/linux_amd64/cmd/vendor/golang.org/x/crypto/ssh
/etc/ssh
```
Use wildcard option to search set of files on your system. We are going to search all files available in the system with `.config` extension.
```
# find / -name "*.config"
/usr/lib/mono/gac/avahi-sharp/1.0.0.0__4d116c78973743f5/avahi-sharp.dll.config
/usr/lib/mono/gac/avahi-ui-sharp/0.0.0.0__4d116c78973743f5/avahi-ui-sharp.dll.config
/usr/lib/python2.7/config/Setup.config
/usr/share/git/mw-to-git/t/test.config
/var/lib/lightdm/.config
/home/daygeek/.config
/root/.config
/etc/skel/.config
```
Use the following command format to find an empty files and folders in system.
```
# find / -empty
```
Use the following command combination to find all files containing specific text on Linux.
```
# find / -type f -exec grep "Port 22" '{}' \; -print
# find / -type f -print | xargs grep "Port 22"
# find / -type f | xargs grep 'Port 22'
# find / -type f -exec grep -H 'Port 22' {} \;
```
### Method-2: Search Files And Folders in Linux Using locate command
locate command works faster than the find command because it uses updatedb database, whereas the find command searches in the real system.
It uses a database rather than hunting individual directory paths to get a given file.
locate command doesnt pre-installed in most of the distributions so, use your distribution package manager to install it.
The database is updated regularly through cron, however we can manually update it by running the following command.
```
$ sudo updatedb
```
Simply run the following command to list the given file or folder. There is no specific options need to be specified in locate command to print file or folder.
To search `ssh` folder in system.
```
# locate --basename '\ssh'
/etc/ssh
/usr/bin/ssh
/usr/lib/ssh
/usr/lib/go/pkg/linux_amd64/cmd/vendor/golang.org/x/crypto/ssh
/usr/lib/go/src/cmd/go/testdata/failssh/ssh
/usr/lib/go/src/cmd/vendor/golang.org/x/crypto/ssh
```
To search `ssh_config` file in system.
```
# locate --basename '\sshd_config'
/etc/ssh/sshd_config
```
### Method-3: Search Files in Linux Using which command
TThe which command returns the full path of the executable that would have been executed when the command had been entered in terminal.
Its very useful when you want to create a desktop shortcut or symbolic link for executable files.
Which command searches the directories listed in the current users PATH environment variable not for all the users. I mean, when you are logged in your own account and you cant able to search for root user file or directory.
Run the following command to print the full path of the vim executable file location.
```
# which vi
/usr/bin/vi
```
Alternatively, its allowing user to perform multiple file search in one shot.
```
# which -a vi sudo
/usr/bin/vi
/bin/vi
/usr/bin/sudo
/bin/sudo
```
### Method-4: Search Files in Linux Using whereis command
The whereis command used to search the binary, source, and man page files for a given command.
```
# whereis vi
vi: /usr/bin/vi /usr/share/man/man1/vi.1p.gz /usr/share/man/man1/vi.1.gz
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/four-easy-ways-to-search-or-find-files-and-folders-in-linux/
作者:[Prakash Subramanian][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.2daygeek.com/author/prakash/
[b]: https://github.com/lujun9972

View File

@ -0,0 +1,153 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Four Easy Ways to Search Or Find Files And Folders in Linux)
[#]: via: (https://www.2daygeek.com/four-easy-ways-to-search-or-find-files-and-folders-in-linux/)
[#]: author: (Prakash Subramanian https://www.2daygeek.com/author/prakash/)
四种简单的方法来搜索或查找 Linux 中的文件和文件夹
======
Linux 管理员一天都不能离开搜索文件,因为这是他们的日常活动。
了解一些搜索的东西是不错的,因为这能帮助你在命令行服务器中工作。
这些命令记忆起来不复杂,因为它们使用的是标准语法。
这里执行四个 Linux 命令,每个命令都有自己独特的功能。
### 方法 1使用 find 命令在 Linux 中搜索文件和文件夹
find 命令被广泛使用,并且是非常著名的在 Linux 中搜索文件和文件夹的命令。它搜索当前目录中的给定文件,并根据搜索条件递归遍历其子目录。
它允许用户根据大小、名称、所有者、组、类型、权限、日期和其他条件执行所有类型的文件搜索。
运行以下命令以在系统中查找给定文件。
```
# find / -iname "sshd_config"
/etc/ssh/sshd_config
```
运行以下命令以查找系统中的给定文件夹。要在 Linux 中搜索文件夹,我们需要使用 `-type` 参数。
```
# find / -type d -iname "ssh"
/usr/lib/ssh
/usr/lib/go/src/cmd/vendor/golang.org/x/crypto/ssh
/usr/lib/go/pkg/linux_amd64/cmd/vendor/golang.org/x/crypto/ssh
/etc/ssh
```
使用通配符搜索系统上的所有文件。我们将搜索系统中所有以 `.config` 为扩展名的文件。
```
# find / -name "*.config"
/usr/lib/mono/gac/avahi-sharp/1.0.0.0__4d116c78973743f5/avahi-sharp.dll.config
/usr/lib/mono/gac/avahi-ui-sharp/0.0.0.0__4d116c78973743f5/avahi-ui-sharp.dll.config
/usr/lib/python2.7/config/Setup.config
/usr/share/git/mw-to-git/t/test.config
/var/lib/lightdm/.config
/home/daygeek/.config
/root/.config
/etc/skel/.config
```
使用以下命令格式在系统中查找空文件和文件夹。
```
# find / -empty
```
使用以下命令组合查找 Linux 上包含特定文本的所有文件。
```
# find / -type f -exec grep "Port 22" '{}' \; -print
# find / -type f -print | xargs grep "Port 22"
# find / -type f | xargs grep 'Port 22'
# find / -type f -exec grep -H 'Port 22' {} \;
```
### 方法 2使用 locate 命令在 Linux 中搜索文件和文件夹
locate 命令比 find 命令运行得更快,因为它使用 updatedb 数据库,而 find 命令在真实系统中搜索。
它使用数据库而不是搜索单个目录路径来获取给定文件。
locate 命令未在大多数发行版中预安装,因此,请使用你的包管理器进行安装。
数据库通过 cron 定期更新,但我们可以通过运行以下命令手动更新它。
```
$ sudo updatedb
```
只需运行以下命令即可列出给定的文件或文件夹。在 locate 命令中不需要指定特定选项来打印文件或文件夹。
在系统中搜索 `ssh` 文件夹。
```
# locate --basename '\ssh'
/etc/ssh
/usr/bin/ssh
/usr/lib/ssh
/usr/lib/go/pkg/linux_amd64/cmd/vendor/golang.org/x/crypto/ssh
/usr/lib/go/src/cmd/go/testdata/failssh/ssh
/usr/lib/go/src/cmd/vendor/golang.org/x/crypto/ssh
```
在系统中搜索 `ssh_config` 文件。
```
# locate --basename '\sshd_config'
/etc/ssh/sshd_config
```
### 方法 3在 Linux 中搜索文件使用 which 命令
which 返回在终端输入命令时执行的可执行文件的完整路径。
当你想要为可执行文件创建桌面快捷方式或符号链接时,它非常有用。
which 命令搜索当前用户而不是所有用户的 PATH 环境变量中列出的目录。我的意思是,当你登录自己的帐户时,你无法搜索 root 用户文件或目录。
运行以下命令以打印 vim 可执行文件的完整路径。
```
# which vi
/usr/bin/vi
```
或者,它允许用户一次执行多个文件搜索。
```
# which -a vi sudo
/usr/bin/vi
/bin/vi
/usr/bin/sudo
/bin/sudo
```
### 方法 4使用 whereis 命令在 Linux 中搜索文件
whereis 命令用于搜索给定命令的二进制、源码和手册页文件。
```
# whereis vi
vi: /usr/bin/vi /usr/share/man/man1/vi.1p.gz /usr/share/man/man1/vi.1.gz
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/four-easy-ways-to-search-or-find-files-and-folders-in-linux/
作者:[Prakash Subramanian][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.2daygeek.com/author/prakash/
[b]: https://github.com/lujun9972