Find – Awesome Linux Command

This post will show you how you can use find command to search for files in a directory hierarchy and combine with different commands and operators to change the permission for files and directories and remove them only, if required and to accomplish many other purposes.

To change permission of all files to 664

 
# find . -type f -exec chmod 664 {} \;


To change permission of all directories to 775

 
# find . -type d -exec chmod 775 {} \;

To find all files/directories having super user id bit set

 
# find / -perm +4000

OR

 
# find / -perm +u=s

To find all files/directories having super group id bit set

 
# find / -perm +2000

OR


# find / -perm +g=s

To Find Files/directories named ‘los’ in all directories and sub-directories in current directory and remove them


# find . -name los -exec rm -rf {} \;

To find all *.exe files in current directory and removes them with confirmation from user:


# find . -type f -name "*.exe" -exec rm -i {} \;

Note: Above command should be run with normal user acount not by root user account, because root account will not ask you for confirmation.

To Find all .js files containing string linuxonlinesolutions and remove that string


# find -type f -name "*.js" -exec sed -i 's/linuxonlinesolutions//g' {} \;

To Find all .js files containing string  *.linuxonlinesolutions  from all .js files and delete those lines


# find -type f -name "*.js" -exec sed -i '/.*.linuxonlinesolutions/d' {} \;

To find all files in /var/www/html with permission 777 and print the result


$ find /var/www/html -type f -perm 777 -print

To Find all .js file with 777 permission, print them and change their permission to 664


# find / -name *.js -type f -perm 777 -print -exec chmod 664 {} \;

To find Multiple File at Once

 


#find / -name "server" -o -name "bash_shell" -o -name "trozan" -o -name "system32"

To find all directories taking  greater than 1024 KB space on server


# find / -type d -size +1024k

You can leave a response, or trackback from your own site.

3 Responses to “Find – Awesome Linux Command”

  1. Naveen Bahmania says:

    sir i want to know more abt super permission…..plz post something abt them
    And thanks for this post

  2. Arman says:

    Not sure what flavour of linux you’re using but this works on our Red Hat seerrvs just fine.The above command actually deletes all log files 3 days old or less. Should be +3 to delete older.

Leave a Reply