Chat Zalo Chat Messenger Phone Number Đăng nhập
10 ways to use the Linux find command | Enable Sysadmin - Red Hat

10 ways to use the Linux find command | Enable Sysadmin – Red Hat

The find command is one of the most useful Linux commands, especially when you’re faced with the hundreds and thousands of files and folders on a modern computer. As the name implies, finding helps you find things, and not just by file name.

Whether you’re on your own computer or trying to support someone in an unfamiliar system, here are 10 ways finding can help you locate important data.

[ Keep your most used commands handy with the Linux command cheat sheet. ]

1. Find a single file by

name

When you know the name of a file but don’t remember where you saved it, use Search to search your home directory. Use 2>/dev/null to silence permission errors (or use sudo to get all permissions).

$ find / -name “Foo.txt” 2>/dev/null /home/seth/Documents/Foo.txt

2. Search for a single file by approximate

name If you can’t

remember the exact name of the file, or aren’t sure if you’ve capitalized any characters, you can do a partial, case-sensitive search like this:

$ find / -iname “*foo*txt” 2>/dev/null /home/seth/Documents/Foo.txt /home/seth/Documents/foo.txt /home/seth/Documents/foobar.txt

3. Find all

The ls -R command enumerates the contents of a directory recursively, meaning that it not only enumerates the destination you provide to it, but also descends to each subdirectory within that destination (and each subdirectory in each subdirectory, and so on). The find command also has that function, through

the -ls option: $ find ~/Documents -ls 3554235 0 drwxr-xr-x […] 05:36 /home/seth/Documents/ 3554224 0 -rw-rw-r- […] 05:36 /home/seth/Documents/Foo 3766411 0 -rw-rw-r- […] 05:36 /home/seth/Documents/Foo/foo.txt 3766416 0 -rw-rw-r- […] 05:36 /home/seth/Documents/Foo/foobar.txt

Note that I don’t use 2>/dev/null in this case because I’m only listing the contents of a file path within my home directory, so I don’t anticipate permission errors.

4. Search by content

A search command does not have to perform a single task. In fact, one of the options in find allows you to run a different command on any result that finds returns. This can be especially useful when you need to search for a file by content rather than by name, or you need to search by both.

$ find ~/Documents/ -name “*txt” -exec grep -Hello penguin {} ; /home/seth/Documents/Foo.txt:I like penguins. /home/seth/Documents/foo.txt:Penguins are fun.

[ Learn how to manage your Linux environment for success. ]

5. Search for files by

type

You can display files, directories, symbolic links, named pipes, sockets, and more using the

-type option. $ find ~ -type f /home/seth/.bash_logout /home/seth/.bash_profile /home/seth/.bashrc /home/seth/.emacs /home/seth/.local/share/keyrings/login.keyring /home/seth/.local/share/keyrings/user.keystore /home/seth/.local/share/gnome-shell/gnome-overrides-migrated […]

As long as you are using the GNU version of find, you can include several file types in the search results: $ find ~ -type f,l

-name “notebook*” /home/seth/notebook.org /home/seth/Documents/notebook-alias.org

6. List

directories only

One shortcoming of the ls command is that you can’t filter your results by file type, so it can be noisy if you only want a list of directories in one path. The find command combined with the -type d option is a better choice:

$ find ~/Public -type d find ~/Public/ -type d /home/seth/Public/ /home/seth/Public/example.com /home/seth/Public/example.com/www /home/seth/Public/example.com/www/img /home/seth/Public/example.com/www/font /home/seth/Public/example.com/www/style

7. With

hundreds of files in a default user directory and thousands more outside of that, sometimes you get more search results than you want. You can limit the depth of searches with the -maxdepth option, followed by the number of directories you want to descend to after the starting point:

$ find ~/Public/ -maxdepth 1 -type d /home/seth/Public/ /home/seth/Public/example.com

8. Search for

empty files

Sometimes it is useful to discover empty files as a way to sort:

$ find ~ -type f -empty random.idea.txt

Technically, you can use find to delete empty files, but programmatic file deletion is dangerous. For example, if you forget to include -type f in a search for empty files, you will get directories in your results. By adding a delete flag, you would remove potentially important directory structures.

It is vital to compose the find command and then check the results before deleting them. In addition, a poorly placed delete indicator in the search can delete the results before rating them (in other words, you can delete directories in a command intended to delete only files by placing the deletion indicator before the type indicator).

I

prefer to use xargs or Parallel and a trash can command on the rare occasions when I delete files with find.

9. Find

files by age The -mtime option

allows you to limit a search to files older than, but also newer files than, some values multiplied by 24

. $ find /var/log -iname “*~” -o -iname “*log*” -mtime +30

The + before the -mtime number does not mean adding that number to the time. It is a conditional statement that matches (in this example) a value greater than 24 times 30. In other words, the sample code looks for log files that have not been modified in a month or more.

To find log

files modified in the last week, you can use the – conditional: $ find

/var/log -iname “*~” -or -iname “*log*” -mtime -7 /var/log/tallylog /var/log/cups/error_log /var/log/cups/access_log /var/log/cups/page_log /var/log/anaconda/anaconda.log /var/log/anaconda/syslog /var/log/anaconda/X.log […]

You already know the -ls flag, so you can combine it with these commands for clarity

: $ find /var/log -iname “*~” -o -iname “*log*” -mtime -7 -ls -rw- 1 root root 0 Jun 9 18:20 /var/log/tallylog -rw- 1 root lp 332 Aug 11 15:05 /var/log/cups/error_log -rw- 1 root lp 332 Aug 11 15:05 /var/log/cups/access_log -rw- 1 root lp 332 Aug 11 15:05 /var/log/cups/ page_log – rw- 1 root root 53733 Jun 9 18:24 /var/log/anaconda/anaconda.log -rw- 1 root root 835513 Jun 9 18:24 /var/log/anaconda/syslog -rw- 1 root root 21131 Jun 9 18:24 /var/log/anaconda/X.log […]

10. Find a path

Sometimes you know the directory structure that

leads to a file you need; You just don’t know where the directory structure is located within the system. To search within a path string, you can use the -ipath option, which treats points and forward slashes not as regex characters, but as periods and slashes.

$ find / -type d -name ‘img’ -ipath “*public_html/example.com*” 2>/dev/null /home/tux/Public/public_html/example.com/font

I found it

The find command is an essential tool for a system administrator. It is useful for researching or learning about a new system, finding misplaced data, and troubleshooting everyday problems. But it’s also just a convenience tool.

[ Download the Linux search cheat sheet to keep all these shortcuts in one place. ]

You don’t need a “good” reason to use find. Using search makes it easier to search for something instead of going through your system. It’s a discreet yet endlessly useful tool that embodies the sublime pleasure of everyday Linux. Start using it today and find out what makes it great.

Contact US