[Linux] How to search files in Linux
ENVIRONMENT
- Red Hat Linux 7
Hello everybody,
today I’m going to show you how to search file in linux with common command: ls, find and locate.
LS
The most common command, this will be likely the second or third command that you learn to use when you start using linux terminal.
Ls could be seem not so powerful, but combined with wildcard, could be very powerful
For example, if you have to search for all log files, you can launch this command:
ls *.log
Other examples:
ls v??? #search for a file that starts for v and has exactly 4 character in its name
ls v* #search for a file that starts for v
ls vi[a-m]* #search for a file that starts for vi and its third letter is between a and m
ls vi[!a-m]* #search for a file that starts for vi and its third letter is not between a and m
Attention, you need to stay in the directory!
Locate
With locate, you can find a file by just typing its name:
locate filename
This command will query an internal database to find the file.
Attention, you’ve to update this internale database with this command:
updatedb #you've to launch this as root
Find
Lastly, we have a very powerful command: find.
With find you can combine name search pattern with other options, like size of the file, last access time, last modify time, etc…
The most basic command:
find filename
It will find for that filename in the current directory.
Then we can combine a lot of options:
find ./* -iname '*filename*' -type f -mtime +30 -size +50M
This find will search in the directory and in all subdirectories for a file (-type f) that has in the name *filename* (-iname) case unsensitive, that has been modified more than 30 days ago (-mtime +30) that is larger than 50 MB (-size +50M).
That’s it, I hope that you find useful this commands!
Regards