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.

find.png


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:

Code:
find . -name
“myfile.txt”
./myfile.txt
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:

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

Code:
-inname “File*” -type f
./File.txt
./File.txt
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.

Code:
find -L -iname “MyFile*”
./Myfile.file
./MyFile.text
./MyFile.directory
./MyFile.directory/MyFile2.file
./MYFile.file
./link.directory/MyFile2.file
You can also exclude mount points file systems by using the “-xdev” option.

Code:
find . -xdev -name “file*” -type f
./file2.php
./file.php
“-maxdepth” parameter allows you to set the range.
Code:
find /home -maxdepth 1 -type f
/home/finding_max_depth.file
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.

Code:
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:
Code:
find . -type f -empty
./empty2.file
./empty3.file
./empty.file
find . -type d -empty
./empty_dir
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
Code:
 find . -type f ! -newermt 2020-01-01
./new_file.txt
./older_file.txt

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

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

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

[CODE]find . -type f -size +10MB
./MyFile.file
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:

Code:
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.

Code:
find . -type f -perm 777
./not_malware.txt
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:

Code:
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.

Code:
find . -user Mike
./files_by_Mike.txt
Finding Files by Modified Time

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

Code:
find . -name "*.png" -atime -30
./my_image.png

find . -name "*.jpg" -atime +30
./my_image2.jpg
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.
Author
kumkumsharma
Views
3,026
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top