Find Command – A must for any Linux user.

The find command allows for fast and very smart way of finding files. It also gives you the ability to manipulate those files to your liking. This tutorial covers only the very basics of this command; yet even the basics of this command offer some extremely powerful features to solve complicated problems.

 

Find command:

Looking for files.

  • Find a file called “bacon” from your current directory and all sub directories.
find . -type f -name bacon
  • Find a file called “bacon” in a specific directory.
 find  /home/gmastrokostas/Documents  -type f -name bacon 
  • Find a file called “bacon” and specify how many sub directories to look into (two sub directories in this example)
find  /home/gmastrokostas  -type f -maxdepth 2  -name bacon

  • Find a file called “bacon” and create a copy of it by giving it the extension of backup.
find  /home/gmastrokostas  -type f  -name bacon  -exec cp '{}' '{}'.backup \;

  • Find a series of files called “bacon” and sort by modification time
find  /home/gmastrokostas  -type f  -name bacon* -exec ls -ltr  '{}' \;

  •  Find a file called “bacon” and search for a specific text within that file
find /home/gmastrokostas  -type f  -name bacon  -exec grep -i  cooking   '{}' \;

 

Looking for directories

  • Find a directory called “music” from your current directory and all sub directories below it.
find .  -type d  -name music

  • Find a directory called “music” in a specific directory
find  /home/gmastrokostas  -type d  -name music  

  •  Find a directory called “music” and specify how many sub directories to look into (two sub directories in this example)
find  /home/gmastrokostas  -type d  -maxdepth 2  -name music

  • Find a directory called “music” and create a backup of it with the extension backup.
find  /home/gmastrokostas -type d  -name music  -exec cp -r  '{}'  '{}'.backup \;

  • Find a directory called “music” and search for a string of text “bacon”  in one of the files in that directory and sub directories.
find  /home/gmastrokostas -type d -name music  -exec grep -ir bacon '{}'  \;

 

More Advanced uses of the find command  

–  Find files according  to permissions.

  • How to list files that belong to a specific user
find .  -type f  -user gmastrokostas

  • How to list files that belong to a specific group
find . -type f  -group gmastrokostas

  • How to list files that belong to a specific user and group
find .  -type f  -user gmastrokostas  -group root

  • How to list files with specific permissions
find . -type f  -perm u=rwx 

    (u stands for user)

find . -type f  -perm o=rw 

     (o stands for others)

find . -type f  -perm g=xr 

      (g stands for group)

find .  -type f  -perm u=rx  -perm g=rw

find .  -type f  -perm u=rw  -user=gmastrokostas  -group=root

 

–  Find files according  to file size.

  • How to list files that are one Gigabyte or more
find . -size +1G

  • How to list files that are 10 Megabytes or more
find . -size +10M

  • How to list all files in a directory and their size
find  /home/gmastrokostas  -type f -print0 | xargs -0 du -h | sort -hr | head -20

 

 

 

Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.