Linux Powerful Command : "find".
Linux Powerful "find" Command
We have created some files by touch command in /home directory. See below image.
1. Find all the files whose name is 'itbd.txt' in a current working directory.
[root@itbd ~]# find . -name itbd.txt [Here . means -> current working directory(/root)]
./itbd.txt
2. Find all the files under /home directory with name itbd.txt.
[root@itbd ~]# find /home -name itbd.txt
/home/itbd.txt
3.Find all the files whose name is itbd.txt and contains both capital and small letters in /home directory
[root@itbd ~]# find /home -iname itbd.txt
/home/itbd.txt
/home/ITBD.txt
/home/Itbd.txt
4. Find all directories whose name is 'itbd' in / directory.
[root@itbd ~]# find / -type d -name itbd
/home/itbd
Now, we are creating some files with .php & .html extensions in /home directory for our practice
5. Find all php files whose name with .php extension into /home directory.
[root@itbd ~]# find /home -type f -name web1.php
/home/web1.php
/home/web2.php
/home/web3.php
6. Find all the files whose permissions are 666.
[root@itbd ~]# find /home -type f -perm 666
/home/web1.php
/home/web2.php
/home/web3.php
[root@itbd ~]# find /home -type f ! -perm 666 [Except 666 permission]
Find all 666 permission files in /home directory and use chmod command to set permissions to 777.
[root@itbd ~]# find /home -type f -perm 0666 -exec chmod 777 {} \;
Find all 777 permission directories and use chmod command to set permissions to 755.
[root@itbd ~]#find / -type d -perm 777 -print -exec chmod 755 {} \;
7. To find and remove multiple files such as .html, then use.
[root@itbd ~]#find /home -type f -name "*.html" -exec rm -f {} \;
8. To find all empty files under /home.
[root@itbd ~]#find /home -type f -empty
To file all empty directories under a certain path.
[root@itbd ~]#find /tmp -type d -empty
9. Find files whose user are 'itbd' under /home
[root@itbd ~]# find /home -user itbd
Find all files whose group are 'itbd-group'
[root@itbd ~]# find /home -group itbd-group
10. Find all SUID set files.
[root@itbd ~]# find / -perm /u=s
Find all SGID set files.
[root@itbd ~]# find / -perm /g=s
Find all Read Only Files
[root@itbd ~]# find / -perm /u=r
Find all Executable files.
[root@itbd ~]# find / -perm /a=x
=================================================================
Now we will find Files and Directories Based on Date and Time:
mtime = modify time for day
atime =access time for day
cmin = changed time for minute
mmin = modified time for minute
amin = accesed time for mimute
To find all the files which are modified 50 days back.
[root@itbd ~]# find / -mtime 50
To find all the files which are accessed 50 days back.
[root@itbd ~]# find / -atime 50
To find all the files which are modified more than 50 days back and less than 100 days.
[root@itbd ~]# find / -mtime +50 –mtime -100
To find all the files which are changed in the last 1 hour.
[root@itbd ~]# find / -cmin -60
To find all the files which are modified in the last 1 hour.
[root@itbd ~]# find / -mmin -60
To find all the files which are accessed in the last 1 hour.
[root@itbd ~]# find / -amin -60
=================================================================
Now we will find Files and Directories Based on Size:
To find all 50MB files
[root@itbd ~]# find / -size 50M
To find all the files which are greater than 50MB and less than 100MB.
[root@itbd ~]# find / -size +50M -size -100M
To find all 100MB files and delete them using one single command.
[root@itbd ~]# find / -type f -size +100M -exec rm -f {} \;
Find all .mp3 files with more than 10MB and delete them using one single command.
[root@itbd ~]# find / -type f -name *.mp3 -size +10M -exec rm {} \;
=================================================================
Please try to give the answers of below questions. We will check your answers.
Questions:
1. Find all hidden files of your system.
2.Find all .txt files of user 'itbd' under /home directory.
3. Find all files whose user are 'itbd' from your system
4. Find all .mp4 files with more than 600MB and delete them using one single command.
5. find all the files which are modified in the last 10 hour.
6. Find all files in the /var directory owned by user "root" and group "mail"
Comments
Post a Comment