Skip to content

Instantly share code, notes, and snippets.

@rkcb
Last active September 15, 2018 12:49
Show Gist options
  • Select an option

  • Save rkcb/ccbfa62aa141b1fa22573193c5089aac to your computer and use it in GitHub Desktop.

Select an option

Save rkcb/ccbfa62aa141b1fa22573193c5089aac to your computer and use it in GitHub Desktop.
Useful examples of Linux find

Examples of Linux find

List all files in current and sub directories

Find recursively all files and directories in the current path:

find

Search specific directory or path

As above but in the subdirectory:

find mydir

Limit depth of directory traversal

find mydir -maxdepth 1 -name \*.php

Invert match

find mydir -not -name file.txt

find mydir ! -name file.php

Combine multiple search criteria

"Not" operator:

     find mydir -name 'abc\*' ! -name '\*.php'

"Or" operator:   

    find -name '\*.php' -o -name '\*.txt'

Search only files or only directories

Find only files:

     find dir -type f -name file.txt

Find only directories:  

    find dir -type d -name file.txt

Search multiple directories

find dir1 dir2 type f -name \*.txt 

Find hidden files

find ~ -type f -name .bash*

Find files with certain permissions

find dir -type f -perm 0664

find . -type f ! -perm 0777

Find files with sgid or suid bits set

 find / -perm 2644

 find / -maxdepth 2 -perm /u=s 2>/dev/null

Find readonly files

 find /etc -maxdepth 1 -perm /u=r

Find executable files

find /bin -maxdepth 2 -perm /a=x

Find files owned to particular user

find . -user bob

find . -user bob -name *txt

Search files belonging to a group

find /var/www -group developer

find ~ -name hidden.php

Find files modified N days back

find / -mtime 50

Find files accessed in last N days

find / -atime 50

Find files modified in a range of days

Find all files that were modified between 50 to 100 days ago:

    find / -mtime +50 –mtime -100

Find files changed in last N minutes.

Find files modified within the last 1 hour:

find /home/bob -cmin -60

Files modified in last hour

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

    find / -mmin -60

Find Accessed Files in Last 1 Hour

find / -amin -60

Find files of given size

Search files and directories based on size. To find all 50MB files, use:

find / -size 50M

Find files in a size range

To find all the files which are greater than 50MB and less than 100MB.

find / -size +50M -size -100M

Find largest and smallest files

The following command will display the 5 largest file in the 
current directory and its subdirectory. This may take a while to execute 
depending on the total number of files the command has to process:

    find . -type f -exec ls -s {} \; | sort -n -r | head -5
    
Similary when sorted in ascending order, it would show the smallest files first:

    find . -type f -exec ls -s {} \; | sort -n | head -5

Find empty files and directories

Find empty directories and files:       

    find /tmp -type f -empty

List out the found files

find . -exec ls -ld {} \;

Delete all matching files or directories

find /tmp -type f -name "\*.txt" -exec rm -f {} \;

Lets take another example where we want to delete files larger than 100MB:

    find /home/bob/dir -type f -name \*.log -size +10M -exec rm -f {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment