Do you ever no longer remember where exactly you put a file on your Linux computer or server? You’re confident that it was saved somewhere but can’t find exactly where. For Windows users, the obvious solution is to use Explorer’s ‘Find’ function. But what if you’re unsure about how to find a file in Linux?
We’ve compiled a list of methods you can use to locate missing files in Linux. As always, we include tools for both more experienced users and beginners, and we hope that these suggestions allow you to avoid the traumatic loss of important data!
Using the grep command to find a file by its contents
Grep has been part of Unix for so long that you will often see the term used as a verb in Linux discussions online (it’s generally synonymous with “search locally”). It can be thought of as the ultimate “pattern recognition tool”. However, Grep should only be used to search for the contents of a file rather than finding items using their filename.
Now, let’s look at how grep is actually used. Let’s say you had a file in your ~/Documents directory called ‘Give.txt’. The contents of the file are as follows:
What's real can't die
You only get what you give
You're gonna get what you give
Don't give up
Just don't be afraid to leave
Of course, these are the lyrics to a famous song by New Radicals. It’s an excellent example in this case, as it highlights how useful grep is and a few of its behaviors that might appear idiosyncratic at first to new users.
From our Documents directory, we’re going to execute the following command:
grep you give.txt
Press return, and you’ll see that it returns every line with the word “you” in it:
Now, let’s try another command (remember to include the escaped apostrophe):
grep don't give.txt
And here is our result:
As you can see, the fifth line was identified; however, because grep is case-sensitive by default, the fourth line (“Don’t give up”) was not returned.
Now, we’ll use the “-i” flag to turn off case-sensitivity in our following query (I’ve also now used quotations to avoid relying on the backslash character this time):
grep -i "don't" give.txt
This time, we can see that every instance of the word – regardless of case – is brought back. If you use the “-i” flag when searching for “you”, you’ll also notice that it now highlights the capitalized appearances of the word as well:
Using the find command to find a file by its name
While grep allows users to query the contents of files on their computer, the find tool provides an efficient way to locate items based on their actual filename.
The most basic use of the find command is as follows:
find .
The period (‘.’) tells the command to return every file and subdirectory found if we search from the present working directory. Essentially, all this is is a good starting point.
However, there are a variety of additional arguments which allow us to get a lot more specific with how we use the find tool. For example, the following command will return all subdirectories from the present location (with ‘d’ standing for “directories”):
find . -type d
Alternatively, we can return only files by typing:
find . -type f
The find command becomes especially useful for locating misplaced files when switching out the “-type” option with “-name”. For example, we can return all ePub files (either in the current directory or within its subdirectories) by using:
find . -name "*.epub"
Or, we can find all ePub files with the word “great” in the name by typing:
find . -name "*great*epub"
And, as with the grep tool, we can choose to make our query case-insensitive:
find . -iname "*great*epub"
And, if we want to narrow it down even further, we can combine options too:
find . -type f -iname "*GrEat*"
The above example is slightly redundant, as we only have files with the word ‘great’ in their title. However, if the ~/Downloads folder also had a subdirectory called “Great Expectations”, it would be omitted from our results as we have specifically included the ‘-type f’ option.
Find files using Catfish
For those who prefer not to use the command line, you can use a GTK3 application called Catfish. This can be installed from the software repository of your Linux distribution by typing:
sudo apt-get install catfish -y
Other popular choices for Linux users who prefer to use GUI to locate files include fsearch, Cerebro, and Terrier.
Catfish can provide complex searches (based on criteria like date of modification, file type, and so on). While it’s not quite as powerful as grep or find, it should suit most users fine.