In this article, we will see how to find files and directories in Linux. It always looks a daunting task to find a file or a directory from a list of probably hundreds and thousands of files and directories available in your Linux Server. But in reality this has been made so easy with a utility called find
which can be used to find or search any files or directories in your server, given that you have adequate permission to look into the required path or location.
Along with find utility, you can also make use of other useful utilities like locate
command to locate the files effectively. As we go along, we will see the usage of both the utilities with the help of some real world examples. But here we will mainly focus on the usage of find utilities to search files and directories in Linux.
How to find files and directories in Linux
Also Read: How to Set Root Password in Ubuntu- linuxnasa
When it comes to finding or searching some files or directories in a linux server, there is no better utility than find
command which provides number of options and features to increase the level and depth of your search. For example, if you are looking for some file called window.js
under current location then you need to use find . -name window.js
command as shown below.
itsfosslinux@ubuntu:~$ find . -name window.js ./window.js
In case you don't know the file name if it is in small or capital letter then to ignore the case sensitivity you can use -iname
option instead of -name
as shown below.
itsfosslinux@ubuntu:~$ find . -iname window.js ./Window.js ./window.js
Instead of searching files in current directory, you can also search files under some specific location such as /home/itsfosslinux
using below command.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -iname window.js /home/itsfosslinux/Window.js /home/itsfosslinux/window.js
Similarly, if you are searching for some directory called luajit
under current location then you have to use -type d
option.
itsfosslinux@ubuntu:~$ find . -type d -name luajit ./luajit
You can also find files on the basis of its current permissions. If you are looking for all the files under /home/itsfosslinux
location having 777
permission then you have to use find /home/itsfosslinux -type f -perm 0777 -print
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -type f -perm 0777 -print /home/itsfosslinux/.wine/drive_c/ProgramData/Apple Computer/iTunes/SC Info/SC Info.txt
If you are looking for all hidden files under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -type f -name ".*"
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -type f -name ".*"
If you are looking for all empty directories under /home/itsfosslinux
path then you have to use find /home/itsfosslinux -type d -empty
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -type d -empty
If you would like to check all the files under /home/itsfosslinux
which have SUID set then you have to use find /home/itsfosslinux -perm /u=s
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -perm /u=s
If you are searching for all the files which does not have some specific permission, for example 0744
under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -type f ! -perm 0744
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -type f ! -perm 0744
If you are looking for all the files with sticky bit set and with some specific permissions such as 644 under /home/itsfosslinux
location then you have to run find /home/itsfosslinux -perm 1644
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -perm 1644
Similarly, if you are looking for all the files which has SGID bit set and has some specific permissions such as 600 under /home/itsfosslinux
location then you need to use find /home/itsfosslinux -perm 2600
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -perm 2600
If you are looking for all executable files under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -perm /a=x
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -perm /a=x
If you are looking for all read only files under /home/itsfosslinux
then you have to use find /home/itsfosslinux -perm /a=r
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -perm /a=r
Similarly, if you are searching for all write only files under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -perm /a=w
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -perm /a=w
You can also find a file by setting maxdepth and mindepth level. For example, if you are looking for a file called abc.txt
with maxdepth
level 2 under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -maxdepth 2 -name abc.txt
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -maxdepth 2 -name abc.txt
Similarly, if you are looking for a file called abc.txt
with mindepth
level 2 under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -mindepth 2 -name abc.txt
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -mindepth 2 -name abc.txt
You can also find files and directories based on its access, modified and changed date and time. For example, if you are looking for all the files which was changed in last 45 mins
under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -cmin 45
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -cmin 45
Similarly, if you are searching for all the files which was changed more than 45 mins
back under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -cmin +45
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -cmin +45
If you are looking for all the files which was accessed in last 60 mins under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -amin 60
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -amin 60
Similarly, if you are searching for all the files which was accessed more than 60 mins back under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -amin +60
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -amin +60
If you are looking for all the files which was modified in last 20 mins under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -mmin 20
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -mmin 20
Similarly, if you are searching for all the files which was modified more than 20 mins back under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -mmin +20
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -mmin +20
If you are searching for all the files which was changed more than 10 days back and less than 20 days then you have to use find /home/itsfosslinux -ctime +10 -ctime -20
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -ctime +10 -ctime -20
If you are searching for all the files which was accessed more than 20 days back and less than 30 days then you have to use find /home/itsfosslinux -atime +20 -atime -30
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -atime +20 -atime -30
If you are searching for all the files which was modified more than 5 days
back and less than 20 days
then you have to use find /home/itsfosslinux -ctime +5 -ctime -20
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -mtime +5 -mtime -20
You can also search files based on its size. For example, if you are looking for all the files of size 80MB
under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -size 80M
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -size 80M
Similarly if you are looking for all the files of size between 50M to 80M under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -size +50M -size -80M
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -size +50M -size -80M
You can also find all the files which belongs to a user or a group. For example, if you are looking for all the files which belongs to user itsfosslinux
under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -user itsfosslinux
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -user itsfosslinux
If you are looking for all the files which does not belong to any known specific user then you have to use find /home/itsfosslinux -nouser
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -nouser
Similarly, if you are looking for all the files which belongs to a group say itsfosslinux
in our case under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -group itsfosslinux
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -group itsfosslinux
If you are looking for all the files which does not belong to any known groups under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -nogroup
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -nogroup
You can also search all the files belongs to a user or a group by their user id or group id. For example, if you are looking for all the files belongs to user id 300
under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -user 300
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -user 300
Similarly, if you are searching for all the files belong to group id 400
under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -group 400
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -group 400
You can also search for all empty files and directories. For example, if you are looking for all empty files under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -type f -empty
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -type f -empty
Similarly, if you are looking for all empty directories under /home/itsfosslinux
location then you have to use find /home/itsfosslinux -type d -empty
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -type d -empty
You can also find files and directories and perform certain operation on it in a single line of command. For example, you can search all the files with permission 0654
under /home/itsfosslinux
location and modify its permission to 644
using find /home/itsfosslinux -type f -perm 0654 -print -exec chmod 644 {} \;
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -type f -perm 0654 -print -exec chmod 644 {} \;
Similarly, if you want to find and remove a file called abc.txt
from /home/itsfosslinux
location then you have to use find /home/itsfosslinux -type f -name abc.txt -exec rm -rf {} \;
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -type f -name abc.txt -exec rm -rf {} \;
Similarly, if you are looking to find and remove all the txt files with size greater than 20M
from /home/itsfosslinux
location then you have to run find /home/itsfosslinux -type f -name *.txt -size +20M -exec rm -rf {} \;
command as shown below.
itsfosslinux@ubuntu:~$ find /home/itsfosslinux -type f -name *.txt -size +20M -exec rm -rf {} \;