0% found this document useful (0 votes)
3 views

find examples

The document provides a comprehensive guide on using the 'find' command in Unix/Linux for searching files and directories based on various criteria such as name, type, permissions, and size. It includes examples of how to use options like '-name', '-type', '-perm', and '-size', as well as advanced features like 'mindepth', 'maxdepth', 'xargs', and '-exec'. Additionally, it covers searching for files based on their modification and access times.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

find examples

The document provides a comprehensive guide on using the 'find' command in Unix/Linux for searching files and directories based on various criteria such as name, type, permissions, and size. It includes examples of how to use options like '-name', '-type', '-perm', and '-size', as well as advanced features like 'mindepth', 'maxdepth', 'xargs', and '-exec'. Additionally, it covers searching for files based on their modification and access times.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

find examples

Find command used to search and locate list of


files and directories
Syntax:-
find <searching from path> -name search file

Find all the files whose name is emp.csv in a current working directory.

root@krosumlabs Day3]# pwd


/root/ShellScript/Day3

root@krosumlabs Day3]# find -name emp.csv


./emp.csv
./L1/emp.csv
./L1/L2/emp.csv
./L1/L2/L3/emp.csv

# find command search the input files recursively


Find all the files whose name is emp.csv in a
login directory .
root@krosumlabs Day3]# find ~ -name emp.csv
/root/emp.csv
/root/Demo/emp.csv
/root/ShellScript/Day3/emp.csv
/root/ShellScript/Day3/L1/emp.csv
/root/ShellScript/Day3/L1/L2/emp.csv
/root/ShellScript/Day3/L1/L2/L3/emp.csv
/root/Temp/emp.csv
Find Files Using Name and Ignoring Case

• root@krosumlabs Day3]# find ~ -iname emp.csv


./EMP.csv
./emp.csv
./L1/emp.csv
./L1/Emp.csv
./L1/L2/emp.csv
./L1/L2/L3/emp.csv
Search a file with pattern

• root@krosumlabs Day3]# find -name "*.log"


• ./r1.log
• ./r2.log
• ./L1/temp.log
Find list of regular files in a current directory.

root@krosumlabs Day3]# find -type f


./ab.txt
./EMP.csv
./emp.csv
./L1/emp.csv
./L1/Emp.csv
./L1/L2/emp.csv
./L1/L2/L3/emp.csv
./r1.log
./r2.log
./L1/temp.log
./p1.sh
./temp.log
Find list of directory files in a current directory.

root@krosumlabs Day3]# find -type d


.
./L1
./L1/L2
./L1/L2/L3
Find list of character type device files in a /dev directory

• root@krosumlabs Day3]# find /dev -type c


• /dev/hidraw0
• /dev/rfkill
• /dev/vcsa5
• /dev/tty1
• ...
• /dev/mem
• /dev/vga_arbiter
Find Files Based on their Permissions

• Find all the files whose permissions are 777

• root@krosumlabs Day4]# find -perm 0777


• ./p1.sh
• ./p2.sh
• ./p3.sh

• root@krosumlabs Day4]# find -perm -u=rwx


• ./p1.sh
• ./p2.sh
• ./p3.sh
mindepth and maxdepth

• using mindepth and maxdepth limiting search to a specific


directory.

• maxdepth levels : Descend at most levels (a non-negative integer)


levels of directories below the starting-points.
• -maxdepth 0 means only apply the tests and actions
to the starting-points themselves.

• mindepth levels : Do not apply any tests or actions at levels


less than levels (a non-negative integer).
• -mindepth 1 means process all files except the starting-points.
Find the passwd file under all sub-directories
starting from root directory.
• root@krosumlabs ~]# find / -name passwd
• /usr/bin/passwd
• /sys/fs/selinux/class/passwd
• /usr/share/bash-completion/passwd
• /etc/pam.d/passwd
• /etc/passwd
Find the passwd file under / directory and one
level down
(i.e root — level 1, and one sub-directory — level 2)

root@krosumlabs ~]# find / -maxdepth 2 -name passwd


/etc/passwd
Find the passwd file under / directory
(search from level 3)
root@krosumlabs ~]# find / -mindepth 3 -name passwd
/usr/bin/passwd
/sys/fs/selinux/class/passwd
/usr/share/bash-completion/passwd
/etc/pam.d/passwd
Find the passwd file under / directory (search from level 4)

• root@krosumlabs ~]# find / -mindepth 4 -name passwd


• /sys/fs/selinux/class/passwd
• /usr/share/bash-completion/passwd
Find Files and Directories Based on Date and Time

• As units you can use:


• b – for 512-byte blocks (this is the default if no suffix is used)
• c – for bytes
• w – for two-byte words
• k – for Kilobytes (units of 1024 bytes)
• M – for Megabytes (units of 1048576 bytes)
• G – for Gigabytes (units of 1073741824 bytes)
• we can search for exact file size, or just for bigger (+) or smaller (–) files.
For example all bigger than 512k files
root@krosumlabs ~]# find / -size +512k

# search only reg.files


root@krosumlabs ~]# find / -type f -size +512k

To find all 50MB files.


root@krosumlabs ~]# find / -size 50M
To find all the files which are greater than 50MB and
less than 100MB.
root@krosumlabs ~]# find / -size +50M -size -100M

To find all the files which are modified 30 days back.


root@krosumlabs ~]# find / -mtime 30

To find all the files which are accessed 30 days back.


root@krosumlabs ~]# find / -atime 30
• To find all the files which are modified more
than 50 days back and less than 100 days.

• root@krosumlabs ~]# find / -mtime +50 –mtime -100


To find all the files which are changed in last 1 hour.

root@krosumlabs ~]# find / -cmin -60

To find all the files which are modified in last 1 hour.

root@krosumlabs ~]# find / -mmin -60


xargs

• xargs converts input from standard input into arguments to a


command.

• root@krosumlabs ~]# echo "one


• two
• three
• four"

• one
• two
• three
• four
• By default xargs displays whatever comes to its
stdin as shown below.

root@krosumlabs ~]# echo "one


two
three
four"|xargs

one two three four


root@krosumlabs~]# find -name "*.txt"
./ab.txt
./sab.txt
./temp.txt

delete all the .txt files

root@krosumlabs~]# find -name "*.txt"|xargs rm

• find list of emp.csv files under /root directory


• search a sales keyword from filtered files

find /root -name "*.csv" |xargs grep -n sales


exec

• execute command

• find -exec command {} \;

• find /root -name "*.csv" -exec grep -n sales {} \;

• search all files with size more than 100MB and delete
them.

• find / -size +100M -exec /bin/rm {} \;

You might also like