Find Command

  • The option -L (options) tells the find the command to follow symbolic links.
  • The /var/www (path…) specifies the directory that will be searched.
  • The (expression) -name "*.js tells find to search files ending with .js (JavaScript files).
find -L /var/www -name "*.js"

To search for a file named document.pdf in the /home/linuxize directory, you would use the following command:

find /home/linuxize -type f -name document.pdf

To run a case-insensitive search, change the -name option with -iname:

find /home/linuxize -type f -iname document.pdf

Find Files by Extension 

Searching for files by extension is the same as searching for files by name. For example, to find all files ending with .log.gz inside the /var/log/nginx directory, you would type:

find /var/log/nginx -type f -name '*.log.gz'

To find all files that don’t match the regex *.log.gz you can use the -not option. For example, to find all files that don’t end in *.log.gz you would use:

find /var/log/nginx -type f -not -name '*.log.gz'

For instance, to find all directories in the current working directory, you would use:

find . -type d

The common example would be to recursively change the website file permissions to 644 and directory permissions to 755 using the chmod command:

find /var/www/my_website -type d -exec chmod 0755 {} \;
find /var/www/my_website -type f -exec chmod 0644 {} \;

Find Files by Size

The following command will find all files of exactly 1024 bytes inside the /tmp directory:

find /tmp -type f -size 1024c

In the following example, we search for all files less than 1MB inside the current working directory. Notice the minus - symbol before the size value:

find . -type f -size -1M

If you want to search for files with a size greater than 1MB, then you need to use the plus + symbol:

find . -type f -size +1M

You can even search for files within a size range. The following command will find all files between 1 and 2MB:

find . -type f -size +1M -size 21M

Find Files by Modification Date

Let’s say that a few days ago, you modified one of the dovecot configuration files, but you forgot which one. You can easily filter all files under the /etc/dovecot/conf.d directory that ends with .conf and has been modified in the last five days:

find /etc/dovecot/conf.d -name "*.conf" -mtime 5

Another example of filtering files based on the modification date using the -daystart option. The command below will list all files in the /home directory that were modified 30 or more days ago:

find /home -mtime +30 -daystart

Find Files by Permissions 

The -perm option allows you to search for files based on the file permissions.

For example, to find all files with permissions of exactly 775 inside the /var/www/html directory, you would use:

find /var/www/html -perm 644
find . -perm /444

The above command will match all the files with read permissions set for either user, group, or others.

If minus - is used as the prefix, then for the file to match, at least the specified bits must be set.

The following command will search for files that have read and write permission for the owner and group and are readable by other users:

find . -perm -664

Find Files by Owner

Search for all files and directories owned by the user linuxize, you would run:

find / -user linuxize

Let’s say you want to find all files owned by the user www-data and change the ownership of the matched files from www-data to nginx:

find / -user www-data -type f  -exec chown nginx {} \;

Find and Delete Files

To delete all matching files, append the -delete option to the end of the match expression.

Ensure you are using this option only when you are confident that the result matches the files you want to delete. It is always a good idea to print the matched files before using the -delete option.

For example, to delete all files ending with .temp from the /var/log/, you would use:

find /var/log/ -name `*.temp` -delete

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *