HostArmada Web Hosting Knowledge Base

Knowledge is power! Use that power and achieve total and unconditional control over the Web Hosting Services!

The find command.

The built-in Linux "find" command is perhaps the most often used tool. Its applications are practically unlimited as you can search for files and execute various commands on them, depending on what you need to do. In this article, we cannot cover all of the uses for this command, however, we are going to give you an idea of how it works and provide you with a few useful examples you can use. From there, it's up to your imagination and creativity how you adjust it for a specific purpose.

The most basic example is to find out if a file exists under a specific name. Using this command, we are going to look for the "demo1" within the home directory of the user. The syntax is as follows:

find <directory path> -name "<file_name>" 

Here is what happens when executed:

[root@ger1 user]# find . -name "demo1"
./public_html/find_tutorial/demo1
./public_html/find_tutorial/find_tutorials/demo1

The above example is going to look for files/folders located in the dot location, "." which means "within the current directory". It is also going to proceed recursively inside any other folder in there looking for everything that has the name "demo1". If you are interested in finding only the FILES that have the name "demo1", find has a flag "-type f" that can enable you to search for files only.

Please refer to the below example:

[root@ger1 user]# find . -type f -name "demo1"
./public_html/find_tutorial/demo1

If you want to look only for directories, please replace the "-type f" with "-type d".

[root@ger1 user]# find . -type d -name "demo1"
./public_html/find_tutorial/find_tutorials/demo1

Now that you know the primary use of this tool, we are going to provide you with a few examples of commands that are going to be very useful in certain situations.

Fix File/Folder permissions

find . -type d -print0 | xargs -0 chmod 0755 && find . -type f -print0 | xargs -0 chmod 0644

This command is going to change the file permissions to 644 and the folder permissions to 755 when executed in a specific folder. It is going to do this recursively and is going to affect ALL files and ALL folders inside. Of course, you can modify it to set whichever permissions you need, however, we at HostArmada advise our clients to keep their web application files with these permissions.

Check the size of files with specific extensions

The next command is handy in situations where you need to clean your hosting account. Sometimes when you have not managed your disk space in a long time, there is much clutter, and it is hard to find what is taking up a lot of disk space.

for i in `find . -maxdepth 4 -type f -name "*tar.gz"`;do du -sh $i;done | sort -h | grep G

Executing this command is going to look for all the files that have the "tar.gz" file extension and give you a list of their locations. If they are of no use to you, delete them using the rm command, which we explained in this article. A neat trick you can use here is to copy this command and paste it within a text editor such as NotePad. After that, you can modify the text after the "-name" flag to look for a different file extension. Great examples of this would be "*zip", "*log", and "*tar".

Check the total inode count in a directory

The last example here is going to help you figure out which folder utilizes the highest number of inodes (number of files).

for d in `find -maxdepth 1 -type d |cut -d\/ -f2 |grep -xv . |sort`; do c=$(find $d |wc -l) ; printf "$c\t\t- $d\n" ; done ; printf "Total: \t\t$(find $(pwd) | wc -l)\n"

When executed, this command is going to show you a list of all the folders. Furthermore, it is going to contain a number on the left side of each folder, displaying the total files they hold. On the bottom of the generated output, you are going to find the TOTAL number of inodes within all the above-listed folders. Typically, you would want to run this in the home dir of your hosting account.

[root@ger1 user]# for d in `find -maxdepth 1 -type d |cut -d\/ -f2 |grep -xv . |sort`; do c=$(find $d |wc -l) ; printf "$c\t\t- $d\n" ; done ; printf "Total: \t\t$(find $(pwd) | wc -l)\n"
6041            - demo-domain.com
1               - cache
114             - .cagefs
3               - .cl.selector
5               - .cpaddons
45              - .cpanel
7               - .cphorde
4               - etc
2043            - demo-domain-2.com
1               - .htpasswds
14              - logs
1               - lscache
119             - mail
7               - perl5
2               - public_ftp
2293            - public_html
2284            - public_html1
8               - .razor
1               - sbin
1               - sed
2               - .sitepad
2               - .spamassassin
4               - .ssh
30              - ssl
2               - .subaccounts
404             - tmp
1               - .trash
2               - .vim
10              - .wp-cli
Total:          13514 

Was this article useful and on point?

Find out more about HostArmada entire range of optimized Web Hosting Services and take action today on improving your website Loading Speed, Security, and overall Stability!