Friday, September 9, 2016

How to search files from the Linux terminal



How to search files from the terminal on Linux

   While there are many ways with which we can search and locate files and directories on Linux, the easiest and quickest is probably through the terminal. However, not many Linux users know about that, which leads to unneeded frustration. Here is a quick guide that will hopefully help you find what you're looking for in your system.

   As an example, I want to search for a file that contains a poster in my computer. I know that the filename contains the word “poster” in it, but I don't exactly remember the name.In a terminal I enter the below command:

sudo find / -iname "*poster*"

   The command “find” actually is searching your disks for the files and directories that the user is looking for. ( with parameter “-iname”, I could get results with no regards to letter case).

   Find is perfect for when you're trying to locate a file or a directory but you can't remember its name, because “find” can search for files that belong to a certain user or group of users, files that were modified or accessed recently, files that of a specific size range, hidden files, executables, read-only files, and files with certain permissions. The best part is that a user is free to combine multiple of the above criteria in one “find” command, essentially narrowing down the results.

   Speaking of narrowing down, the first thing that you want to do when running “find” is to tell it to search on a specific directory. This will speed up the search process significantly, but always depending on the size of the directory. If you know where the file might be, open the terminal, navigate to the directory and run “find . [filename]”. That dot tells find to search on the current directory. If you want to search your Home directory instead, replace the dot with “~/”, and if you want to search your whole filesystem, use “/” instead.