Linux pt. 4: Finding Files
It happens to everyone: You need a certain file for whatever reason, but you just can’t remember where you saved it. If you’re like me organization may not be your forte and it may be easy to lose files, luckily Linux has ways of finding them. The ‘find’ command is very useful for this, but can take a while to learn. It has a variety of uses, but for this guide we’re just going to use it for finding lost files that we can remember the name of.
(Damn it, where did I save my to-do list?)
The ‘find’ command has four main parts. After the command ‘find’, you need to specify which directory to look through, then you can tell the program the type (the find command can locate both files and directories), then the name. I’ll talk about the last bit in a minute.
So say I need to find a file called TODO.txt, and I know it’s in my ~/Documents directory. I can use the command ‘find ~/Documents -type f -name TODO.txt’. the ‘-type f’ flag denotes we are looking for a file, and -name is pretty self-explanatory.
Now I have the path to the file.
Finding a Very Lost File:
What if I don’t know where I saved it at all? Well then when specifying the location you use ‘/’, and the output is less pretty:
(I don’t think I have the right permissions to access some of these files)
We can see the file here, but sometimes it can be hard to find in the sea of ‘Permission denied’, this is where the trick I mentioned earlier comes into play. There is a file called /dev/null on Linux where anything written to it is lost. By adding 2>/dev/null to the end we send all errors (represented by 2) to /dev/null.
(Isn’t that much better?)
Finding a directory works very similarly to finding a file, but you change the value of the ‘-type’ flag to ‘d’ instead: