Utilizing the Find Command in Linux: A Complete Guide

Utilizing the Find Command in Linux: A Complete Guide

You aren’t utilizing the Command Line Utility to its fullest if you aren’t or haven’t used the Find command till now. It’s simplicity and versatility makes the Command Line a must-use tool for everyone using Linux machines. In UNIX-based machines, it’s used as default to search for files using name, date, size, and other parameters. In this article, you’ll learn how to utilize the command line utility in Linux.

Search for Files

As mentioned earlier, most people use Command Line Utility to search for files stored on their computer. You can search using this command in the command line - find [parameters] [path] [search pattern].

But you can also make more complex searches using this tool. You’ll also be learning about the search patterns that will help you get the most out of it.

If you use the find command without specifying the parameters, then it will return the files and directories present in the current location.

In the subsequent sections, you’ll learn how to make advanced searches.


Finding a File by Name

You can search for a file using its name. To find a file, you must use the -name tag or the -iname tag for case insensitive search.

So an example of it would be:

find . -name
It will return the myfile.txt file stored on your computer. Search it with -iname if you want to make case insensitive searches. It will return all the results that match the search, irrespective of if they’re uppercase or lowercase.

The “.” is used to specify that the search should be in the current directory.

You can also find a directory with this approach.

By adding an asterisk “*” with the search keyword, you can find any file that starts with that keyword. For example in the following search, the result will return all the text files that start with keyword File:

-iname “File*”
By adding “-type f” which is the flag -type operator, you narrow down to the file name explicitly.

-inname “File*” -type f
Other file types operator you can use are:
  • l: symbolic link
  • p: named pipe
  • s: socket
  • c: character devices
Using Special Variables

For more specific or accurate results, you can make use of special variables. The symbolic link l: is a type of special variable. By using it, you get all the files that match the search keyword.

find -L -iname “MyFile*”
You can also exclude mount points file systems by using the “-xdev” option.

find . -xdev -name “file*” -type f
“-maxdepth” parameter allows you to set the range.
find /home -maxdepth 1 -type f
As you can see, find only descended one level down as directed.

Redirecting Error Message

In case your search patters are vague or you don’t know exactly where your files are, you will encounter the error messages frequently. Also, if you do not have any root directory access, you’ll get the message “Permission denied”. To resolve such scenarios, you can redirect the error messages to a directory. In most cases, it’s the “/dev/null” path that is a device file that discards all the messages written on it.

find / -maxdepth 3 -name "my_file.txt"
find: ‘/root’:
Permission denied
find: ‘/lost+found’:
Permission denied
find / -maxdepth 3 -name "my_file.txt" 2>/dev/null
Finding Empty Files and Directories

You can use the F command to find empty directories and files. You can do this by using the -empty tag like this:
find . -type f -empty
find . -type d -empty
Finding Files by Dates

To find files by date, you need to use the -newerXY directive along with the following options:
  • mt: modified time
  • at: access time
  • ct: inode status change
  • bt: birth time
 find . -type f ! -newermt 2020-01-01

find . -type f ! -newerat 2014-12-01
Finding Files by Size

You can use the “size” option to specify files that either exceed, match, or are smaller than a given size.

find . -type f -size 1 MB
./MyFile.txt (1 MB)

[CODE]find . -type f -size +10MB
In the first search, the file size that are exactly 1 MB are returned. In the second result, files larger than 10 MB returned.

Finding and Deleting Files

You can find and delete a file simultaneously using the following command:

find /var/log/ -name "*.MyFile" -delete
Security-oriented Searches

When you feel like there’s a security breach in your Linux system. You can make search-oriented searches.

For example, you can use the -perm tag to find files that have incorrect permissions. As much as possible, you should avoid files with 777 permission.

find . -type f -perm 777
If you find the files that aren’t supposed to have those permissions, you can change them instantly. Use the following command if you want to change a file’s permission to 644:

find . -type f -perm 777 -exec chmod 644 {} \;
Searching for Files by Owner

If you want to search for files by owner, you can utilize the “-user” tag.

find . -user Mike
Finding Files by Modified Time

You can use the “-attime” tag to find files by modified date.

find . -name "*.png" -atime -30

find . -name "*.jpg" -atime +30
In the first command, you get results that are modified within the last 30 days. In the second command, you get results where the files were modified anytime before 30 days.
First release
Last update
0.00 star(s) 0 ratings

More resources from kumkumsharma