Using the command line can certainly be faster, but there are still a few things that a GUI does more naturally. For example if you want to find a file using any of the popular GUI interfaces, you usually just need to open a dialogue box and type in the name of the file. If you have indexing available, you probably just need to click a button and start typing. In Windows 10 for example, you can just press the “Start Menu” button and being typing. The file index will automatically bring up your results.
The Linux CLI however, has a different set of tools. While they might not be as intuitive as what you find in the GUI, they’re arguably more powerful once you get used to them. For some tasks like sorting, you need to specify the correct parameters and remember which commands are ideal. So let’s get started from the simplest, to the more complex.
Searching a Single Directory for a File
The most common scenario is if you’re not sure of the exact name of a file, or you need to get the version number just right. We’re all familiar with the “ls” command for listing the contents of a directory. But if there are a large number of files inside it, finding the one we want may not be easy – especially when you need to have it in sight while typing out the next command.
A great example is searching the “/bin” directory. There are a huge number of files here. Let’s say I want to locate a file called “pmap”. Here is the command to do that:
ls /bin | grep pmap
Note how we use the grep command to “pipe” the output of one command into another. In this case, we simply search the output of “ls /bin” for the character sequence “pmap”, and we get an output like this:
As you can see, it returns the file “pmap”, so we know it’s there. We can also use the “find” command like this:
find /bin/ -name "*pmap*"
Which gives the output:
Note how I close the folder name with a slash “/” in the example above (/bin/, and not /bin). I’ve found that using the slash is more consistent and reduces the chances of the command failing.
Personally, I find using “ls” easier for searching a single directory (without subdirectories). But if you want to search recursively in subdirectories, ls is of limited use.
Searching Recursively Within Subdirectories
“ls” only lists the contents of a directory – it doesn’t give us results further down. Let’s say I want to search for a certain script withing the subdirectories of my current location. I can use something like this:
find . -name "*adduser*"
The “.” signifies that I want to use my current directory as the starting point. You can replace it with any directory and it will search it recursively. Note how you can use wildcards like “*” and how you need to enclose the filename in quotes. The above command gives the following output:
It found the file “adduserexpass” inside the “bin” directory which was in my current folder.
Sorting Files by Date Modified and Accessed
Often, you need to access files that were most recently modified so you can continue working on them. We can once again use the “ls” command for this with the following command:
ls /bin/ -ltc
This gives us:
You can see that it’s in descending order from the most recent modified to the oldest. You can use the -r command to reverse the chronology if you want, so it would become “ls /bin/ -ltcr” instead.
Finally, we can also sort the files based on last accessed – whether modified or not. This is considerably more complicated. However, the following command will achieve it.
find /bin/ -printf "%AD %AT %p\n"
Replace “/bin” with whatever folder you want. The output is:
Here we use the -printf option to generate the output. This gives us the most recently accessed files.
Using these commands, you should be able to find files in Linux even with inexact file names. Each command has its own variations that you can read up on. But these should give you a great base on which to start.