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

  • Simple Stateful Load Balancer with iptables and NAT

    NOTE: To demonstrate how iptables can perform network address translation this how-to shows how to use it to implement a over-simplified load balancer. In practice we would use a daemon...
  • Reset MySQL Password

    Resetting a MySQL Root Password is one of the easiest things to do. However, finding documentation on how to do so can be challenging.   We as system administrators, have...
  • How to get rid of ^M characters.

    When you edit a file in Windows and then open in UNIX, you must have seen ^M characters getting appended in the content. How to get rid of it ?...
  • Scanning for rootkits and possible exploits – RKHunter

    Rootkits are malicious softwares that are installed by intruders to gain access to your server once the security of the server has been breached.  Intruders can leave some malicious softwares...