Searching for files or patterns in files occurs quite frequently when dealing with CLI daily. Search for files in Linux is one of the essential skills to have while administrating servers. Here are few useful commands for fast implementation of such tasks.
ls -latr
This command will list all the files (including hidden ones) in given directory, sort them by modification time in reverse order (newest files at the bottom).
ls -lrhS
This command will list all the files sorted by size in human readable format (biggest files at the bottom).
find / -name "test*" 2>/dev/null
This allows you to find all of the files in your machine beginning with “test”. Keep in mind it might take some time (depending mostly on how many files you have), so if you know that you only want to find files under specific directory, feel free to specify it instead of root directory (“/”). Also note that redirecting stderr to /dev/null is highly recommended in order to avoid useless “Permission denied” messages (especially if the command is not being run by root).
grep -ri "test" /etc
This command will let you find pattern “test” (case insensitive) recursively in all the files being there, in this example everywhere in /etc. Similar to “find” example before, you can also choose to redirect stderr to /dev/null.
By employing just those few commands you can already perform quite deep searches! grep
and find
are your best friends when you want to perform search for files in Linux.