Administration FreeBSD Linux Tips and Tricks

Some useful ‘find’ commands

The ‘find’ command is quite useful as it helps to identify files with specific permissions, users,groups, size, etc.  Also, it is quite helpful to as it allows to process its results when combined with other commands. This article talks about some useful ‘find’ commands.

  • Find all files named ‘test.jpg’ inside /home directory

            find /home —type f -name ‘test.jpg’

  • Run case insensitive search for all files named ‘test.jpg’

            find /home —type f -iname ‘test.JPG’

  • Find all directories that are named ‘test’ under / directory.

           find / -type d – name ‘test’

  • Find all files and directories named ‘test’ starting at current location and search up to 5 levels of directories.

           find . -name ‘test’ -maxdepth 5

  • Find files and directories named ‘test’ starting at current location and search between 3-5 directory levels.

           find .  -type f -name ‘test’ -mindepth 3 -maxdepth 5

  • Find all files starting at current location owned by user ‘bec

           find . -type f -user username

  • Find all files starting at current location owned by some group

          find . -type f -group groupname

  • Find the file whose inode number is 2009

          find . -type f -inum 2009

  • Find all files starting at current location whose size is greater then 200Mb

           find .-type f -size +200M

  • Find all files starting from / whose size is in between 50Mb and 100Mb

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

  • Find all files from the current directory whose permission is 777

          find . -type f -perm 0777

  • Find all files starting from / which was modified between 50 and 100 days ago.

          find / -mtime +50 –mtime -100

  • Find all files starting from / which is owned by user ‘username’ , has permission 777 and is of size greater than 50Mb

          find / -user username -perm 0777 -size +50M

Find can also be used to search files/directories as listed above and then its result can be proccessed by some other commands. For eg:

  • Find all files starting from current directory which are greater than 900Mb and delete such files.

           find . type f  -size +900M -exec rm {} \;

  • Find top 5 big files starting from current directory

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

Related Articles

  • Add IP to CentOS 5/6

    If you recently got a secondary IP block for your server but you do not know how to bind the secondary IP address, please follow the steps below:   First, run...
  • Patching The Ghost Vulnerability

    The Ghost vulnerability, also known as CVE-2015-0235, is an exploit in a library named glibc. This library is referenced by many applications which run on your server. The exploit is...
  • Setting files as immutable.

    Wouldn’t it be nice if we can set files so that even root cannot modify or delete it ? Both FreeBSD and Linux come with commands that allows us to...
  • How to List Compiled PHP Modules from the Command Line

    The general command is php -m; this command will give you the full list of extensions: php -m This command will give you an output like: bz2 calendar Core ctype...