Find: Difference between revisions

From Network Security Wiki
Content added Content deleted
mNo edit summary
No edit summary
Line 1: Line 1:
[[Category:Linux]]
[[Category:Linux]]


Created within 2 days
{| class="wikitable"
find ~/test -ctime -2 -print
|-

! Task !! Command
Modified within 24 hours
|-
| Created within 2 days || find ~/test -ctime -2 -print
find ~/test -mtime -1 -print

|-
Find Text Files
| Modified within 24 hours || find ~/test -mtime -1 -print
find ~/test -name *.txt
|-

| Find Text Files || find ~/test -name *.txt
Find empty folders
|-
| Find empty folders || find ~/test -type d -empty
find ~/test -type d -empty

|-
Delete empty folders
| Delete empty folders || find ~/test -type d -empty -exec rmdir {} \;<br />find ~/ -type d -empty -delete
find ~/test -type d -empty -exec rmdir {} \;
|-
find ~/ -type d -empty -delete
| Find and Delete .ini & .db files || find ~/test -name *.ini<br />find ~/test -name *.ini -delete<br />find ~/test -name *.db<br />find ~/test -name *.db -delete

|-
Find and Delete .ini & .db files
| Search all jpg images in the system and archive it || find ~/test -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
find ~/test -name *.ini
|-
find ~/test -name *.ini -delete
| Find Files Using Name || find -name "MyCProgram.c"
find ~/test -name *.db
|-
find ~/test -name *.db -delete
| Find Files Using Name and Ignoring Case || find -iname "MyCProgram.c"

|-
Search all jpg images in the system and archive it
| Limit Search To Specific Directory || find / -name passwd
xargs tar -cvzf images.tar.gz
|-

| Find the passwd file under root and one level down || find -maxdepth 2 -name passwd
Find Files Using Name
|- Find the password file between sub-directory level 2 and 4 || find -mindepth 3 -maxdepth 5 -name passwd
find -name "MyCProgram.c"
|-

| Executing Commands on the Files Found by the Find Command || find -iname "MyCProgram.c" -exec md5sum {} \;
Find Files Using Name and Ignoring Case
|-
| Inverting the match || find -maxdepth 1 -not -iname "MyCProgram.c"
find -iname "MyCProgram.c"

|-
Limit Search To Specific Directory
| Find files which has read permission to group || find . -perm -g=r -type f -exec ls -l {} \;
find / -name passwd
|-

|Find files which has read permission only to group || find . -perm g=r -type f -exec ls -l {} \;
Find the passwd file under root and one level down
|-
find -maxdepth 2 -name passwd
| Find all empty files (zero byte file) || find ~ -empty

|-
Executing Commands on the Files Found by the Find Command
| List all the empty files only in your home directory || find . -maxdepth 1 -empty
find -iname "MyCProgram.c" -exec md5sum {} \;
|-

| List only the non-hidden empty files only in the current directory || find . -maxdepth 1 -empty -not -name ".*"
Inverting the match
|-
find -maxdepth 1 -not -iname "MyCProgram.c"
| Finding the Top 5 Big Files || find . -type f -exec ls -s {} \; | sort -n -r | head -5

|-
Find files which has read permission to group
| Finding the Top 5 Small Files || find . -type f -exec ls -s {} \; | sort -n | head -5
find . -perm -g=r -type f -exec ls -l {} \;
|-

| List the smaller files other than the ZERO byte files || find . -not -empty -type f -exec ls -s {} \; | sort -n | head -5
Find files which has read permission only to group
|-
find . -perm g=r -type f -exec ls -l {} \;
| Find only the socket files || find . -type s

|-
| Find all directories || find . -type d
Find all empty files (zero byte file)
find ~ -empty
|-

| Find only the normal files || find . -type f
List all the empty files only in your home directory
|-
find . -maxdepth 1 -empty
| Find all the hidden files || find . -type f -name ".*"

|-
| Find all the hidden directories || find -type d -name ".*"
List only the non-hidden empty files only in the current directory
find . -maxdepth 1 -empty -not -name ".*"
|-

| Show files which are modified after the specified file || find -newer ordinary_file
Finding the Top 5 Big Files
|-
sort -n -r | head -5
| Find files bigger than the given size || find ~ -size +100M

|-
Finding the Top 5 Small Files
| Find files smaller than the given size || find ~ -size -100M
sort -n | head -5
|-

| Find files that matches the exact given size || find ~ -size 100M
List the smaller files other than the ZERO byte files
|-
sort -n | head -5
| Remove the files named a.out frequently || alias rmao="find . -iname a.out -exec rm {} \;"<br /># rmao

|-
Find only the socket files
| Remove the core files generated by c program || alias rmc="find . -iname core -exec rm {} \;"<br />
find . -type s
# rmc

|-
Find all directories
| Removes *.zip files that are over 100M || find / -type f -name *.zip -size +100M -exec rm -i {} \;"
find . -type d
|-

| List only files recursively without Directory name || find . -maxdepth 10 -type f -printf '%f\n' > fileslist.txt
Find only the normal files
|-
find . -type f
| Find files in directory and sub-directories updated within 60 min || find . -mmin -60

|-
| Finds all the files updated within 1 day || find / -mtime -1
Find all the hidden files
find . -type f -name ".*"
|-

| Find files which got accessed within 60 minutes || find -amin -60<br />find / -atime -1
Find all the hidden directories
|-
find -type d -name ".*"
| Find files which changed within 60 minutes || find . -cmin -60<br />find / -ctime -1

|-
Show files which are modified after the specified file
| Restricting the find output only to files || find /etc/sysconfig -amin -30 -type f
find -newer ordinary_file
|-

| Do not display hidden files in find output || find . -mmin -15 \( ! -regex ".*/\..*" \)
Find files bigger than the given size
|-
find ~ -size +100M
| Displays all files which are modified after the /etc/passwd files was modified || find -newer /etc/passwd

|-
Find files smaller than the given size
| Displays all files which are accessed after modifying /etc/hosts || find -anewer /etc/hosts
find ~ -size -100M
|-

| Displays all files whose status got changed after modifying the /etc/fstab || find -cnewer /etc/fstab
Find files that matches the exact given size
|-
find ~ -size 100M
| Long list the files which are edited within the last 1 hour || find -mmin -60 -exec ls -l {} \;

|-
Remove the files named a.out frequently
| If multiple partitions mounted under /, this command will NOT search all mounted partitions || find / -xdev -name "*.log"
alias rmao="find . -iname a.out -exec rm {} \;"
|-
$ rmao
| If you don’t want to see the errors and would like to redirect it to null || find -name "*.txt" 2>>/dev/null

|-
Remove the core files generated by c program
| Replaces space in all the *.mp3 files with _ || find . -type f -iname “*.mp3″ -exec rename “s/ /_/g” {} \;
alias rmc="find . -iname core -exec rm {} \;"
|-
$ rmc
| Delete 100 days old file || find * -mtime +100 -exec rm {} \;

|-
Removes *.zip files that are over 100M
| Move Specific files to another location || find ./* -name *.jpg -exec mv {} ~/Public/ \;
find / -type f -name *.zip -size +100M -exec rm -i {} \;"
|}

<br />
List only files recursively without Directory name
find . -maxdepth 10 -type f -printf '%f\n' > fileslist.txt

Find files in directory and sub-directories updated within 60 min
find . -mmin -60

Finds all the files updated within 1 day
find / -mtime -1

Find files which got accessed within 60 minutes
find -amin -60
find / -atime -1

Find files which changed within 60 minutes
find . -cmin -60
find / -ctime -1

Restricting the find output only to files
find /etc/sysconfig -amin -30 -type f

Do not display hidden files in find output
find . -mmin -15 \( ! -regex ".*/\..*" \)

Displays all files which are modified after the /etc/passwd files was modified
find -newer /etc/passwd

Displays all files which are accessed after modifying /etc/hosts
find -anewer /etc/hosts

Displays all files whose status got changed after modifying the /etc/fstab
find -cnewer /etc/fstab

Long list the files which are edited within the last 1 hour
find -mmin -60 -exec ls -l {} \;

If multiple partitions mounted under /, this command will NOT search all mounted partitions
find / -xdev -name "*.log"

If you don’t want to see the errors and would like to redirect it to null
find -name "*.txt" 2>>/dev/null

Replaces space in all the *.mp3 files with _
find . -type f -iname “*.mp3″ -exec rename “s/ /_/g” {} \;

Delete 100 days old file
find * -mtime +100 -exec rm {} \;

Move Specific files to another location
find ./* -name *.jpg -exec mv {} ~/Public/ \;






*Perform Any Operation on Files Found From Find Command
*Perform Any Operation on Files Found From Find Command

Revision as of 12:52, 26 September 2017


Created within 2 days

find ~/test -ctime -2 -print

Modified within 24 hours

find ~/test -mtime -1 -print

Find Text Files

find ~/test -name *.txt

Find empty folders

find ~/test -type d -empty

Delete empty folders

find ~/test -type d -empty -exec rmdir {} \;
find ~/ -type d -empty -delete
Find and Delete .ini & .db files
find ~/test -name *.ini
find ~/test -name *.ini -delete
find ~/test -name *.db
find ~/test -name *.db -delete

Search all jpg images in the system and archive it

xargs tar -cvzf images.tar.gz

Find Files Using Name

find -name "MyCProgram.c"

Find Files Using Name and Ignoring Case

find -iname "MyCProgram.c"

Limit Search To Specific Directory

find / -name passwd

Find the passwd file under root and one level down

find -maxdepth 2 -name passwd

Executing Commands on the Files Found by the Find Command

find -iname "MyCProgram.c" -exec md5sum {} \;

Inverting the match

find -maxdepth 1 -not -iname "MyCProgram.c"

Find files which has read permission to group

find . -perm -g=r -type f -exec ls -l {} \;

Find files which has read permission only to group

find . -perm g=r -type f -exec ls -l {} \;

Find all empty files (zero byte file)

find ~ -empty

List all the empty files only in your home directory

find . -maxdepth 1 -empty

List only the non-hidden empty files only in the current directory

find . -maxdepth 1 -empty -not -name ".*"

Finding the Top 5 Big Files

sort -n -r | head -5

Finding the Top 5 Small Files

sort -n | head -5

List the smaller files other than the ZERO byte files

sort -n | head -5

Find only the socket files

find . -type s

Find all directories

find . -type d

Find only the normal files

find . -type f

Find all the hidden files

find . -type f -name ".*"

Find all the hidden directories

find -type d -name ".*"

Show files which are modified after the specified file

find -newer ordinary_file

Find files bigger than the given size

find ~ -size +100M

Find files smaller than the given size

find ~ -size -100M

Find files that matches the exact given size

find ~ -size 100M

Remove the files named a.out frequently

alias rmao="find . -iname a.out -exec rm {} \;"
$ rmao

Remove the core files generated by c program

alias rmc="find . -iname core -exec rm {} \;"
$ rmc

Removes *.zip files that are over 100M

find / -type f -name *.zip -size +100M -exec rm -i {} \;"

List only files recursively without Directory name

find . -maxdepth 10 -type f -printf '%f\n' > fileslist.txt

Find files in directory and sub-directories updated within 60 min

find . -mmin -60

Finds all the files updated within 1 day

find / -mtime -1

Find files which got accessed within 60 minutes

find -amin -60
find / -atime -1

Find files which changed within 60 minutes

find . -cmin -60
find / -ctime -1

Restricting the find output only to files

find /etc/sysconfig -amin -30 -type f

Do not display hidden files in find output

find . -mmin -15 \( ! -regex ".*/\..*" \)

Displays all files which are modified after the /etc/passwd files was modified

find -newer /etc/passwd

Displays all files which are accessed after modifying /etc/hosts

find -anewer /etc/hosts

Displays all files whose status got changed after modifying the /etc/fstab

find -cnewer /etc/fstab

Long list the files which are edited within the last 1 hour

find -mmin -60 -exec ls -l {} \;

If multiple partitions mounted under /, this command will NOT search all mounted partitions

find / -xdev -name "*.log"

If you don’t want to see the errors and would like to redirect it to null

find -name "*.txt" 2>>/dev/null

Replaces space in all the *.mp3 files with _

find . -type f -iname “*.mp3″ -exec rename “s/ /_/g” {} \;

Delete 100 days old file

find * -mtime +100 -exec rm {} \;

Move Specific files to another location

find ./* -name *.jpg -exec mv {} ~/Public/ \; 



  • Perform Any Operation on Files Found From Find Command
find <CONDITION to Find files> -exec <OPERATION> \;
   rm       :  remove the files found by find command.
   mv      :  rename the files found.
   ls -l   :  get details of the find command output files.
   md5sum  :  find command output files
   wc      :  count the total number of words on find command output files.
   Execute any Unix shell command/custom shell script/command on find command output files.


  • List only Directories
 find ~/test ! -name *.jpg ! -name *.gif ! -name *.png ! -name *.jpeg ! -name *.JPG ! -name *.bmp ! -name *.html ! -name *.htm ! -type d 


{{#widget:DISQUS |id=networkm |uniqid=Find |url=https://aman.awiki.org/wiki/Find }}