Use Linux Find Command on Ubuntu

With the Linux find command, you can do a quick search on files and directories with very detailed options. The find command is very useful when using linux. You can search in a folder. How to use find command in linux? How is the use of find command in Ubuntu?

find commands find command

The find command is one of the most powerful tools in the Linux system administrators’ arsenal. It searches for files and directories in a directory hierarchy based on a user-supplied expression and can perform a user-specified action on each matching file.

You can use the find command to search for files and directories by their permissions, type, date, ownership, size, and more. It can also be combined with other tools like grep or sed.

If you are new to the Linux command line you can read the command line beginner tutorial here.

1. Command Syntax

find [options] [path…] [expression]
  • The options attribute controls the handling of symbolic links, debug options, and optimization methods.
  • The path… attribute defines the starting directory or directories from which find will search for files.
  • The expression attribute consists of options, search patterns, and actions separated by operators.

2. Folder Search Operations


Of course, we are not limited to searching only filenames. Let’s take a look at -type (type) expressions before folder searches.

b custom block file
c special character file
d directory
f simple file
l symbolic link
P FIFO
s socket

For more:

find --help

To search for files in a directory, the user calling the find command must have read permissions on that directory.

find -L /var/www -name "*.log"
  • The -L option (options) tells the find command to find symbolic links as well.
  • /var/www (path…) specifies the directory to search.
  • -name “*.log” (expression) tells to search for files ending in .log

Finding files by name is probably the most common use of the find command. To find a file by name, use the -name option followed by the name of the file you’re looking for. For example, to search for a file named profile.sh in the /home/configzone directory, you would use the following command:

find /home/configzone-type f -name profile.sh

To do a case-insensitive search, replace the -name option with -iname:

find /home/configzone-type f -iname ubuntu-guide.pdf

Above command “ubuntu-guide.pdf”, “UBUNTU-GUIDE.pdf” ..etc. will match.

3. Searching by File Extensions


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

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

If the word you’re looking for really needs a * sign, you can tell the shell not to interpret the escape characters with *.
You can also use the -not option to find all files that do not match the *.log.gz regex. For example, to find all files that do not end with *.tar.gz, you would use:

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

4. Searching by File Types


Sometimes you may need to search for certain file types, such as regular files, directories, or symbolic links. In Linux, everything is a file. For example, to find all directories in the current working directory, you would use:

find . -type d

As a general example, it would be possible to change the website file permissions to 644 and the directory permissions to 755 one by one using the chmod command, with the -exec command

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

5. Searching by File Sizes


To find files by file size, we can use the -size parameter along with the size criteria. You can use the following attachments to specify the file size:

b 512-byte blocks (default)
c bytes
w two-byte words
k Kilobytes
M Megabytes
G Gigabytes
The following command will find all files of exactly 512 bytes in the /var/log directory:

find /var/log -type f -size 512c

The find command also allows you to search for files larger or smaller than the specified size.
In the example below, we are looking for all files smaller than 1MB in the current working directory. Note the symbol before the size value:

find . -type f -size -1M

If you want to search for files larger than 1MB, you have to use the + symbol:

find . -type f -size +1M

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

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

find linux commands find command

6. Searching by File Dates

The find command can also search for files based on last modification, access or modification time.

Use the plus and minus symbols for “greater than” or “less than”, as when searching by size.

Let’s say you changed one of the samba config files a few days ago but forgot which one. You can easily filter out all files under the /etc/dovecot/conf.d directory that end in .conf and have been modified in the last five days:

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

Here is another example of filtering files by modification date using the -daystart option. The following command will list all the files in the /home directory that have been modified up to 30 days ago:

find /home -mtime +30 -daystart

7. Searching by File Permissions

The -perm option lets you search for files based on file permissions.

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

find /var/www/html -perm 644

You can put a minus – or / in front of the numeric mode.

When slash / is used, at least one category (user, group, or others) must have at least the relevant bits for a file to match.

Consider the following example command:

find . -perm /444

The above command will match all files with read permissions (4) set for user, group or others.

If – is used as a prefix, at least the specified bits must be set for the file to match. The following command will search for files that have read and write permissions for the owner and group and can be read by other users:

find . -perm -664

8. Searching with File Owners

You can use the -user and -group options to find files belonging to a specific user or group.

For example, to search for all files and directories owned by the configzone user, you would run:

find / -user configzone

Here is a real world example. Let’s say you want to find all files owned by user www-data and change ownership of matching files from www-data to nginx:

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

9. Searching for Files and Deleting Found Files

To delete all matching files, you can add the -delete option to the end of the match statement.

Be sure to use this option only when you are sure that the result matches the files you want to delete. It’s always a good idea to print matching files before using the -delete option.

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

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

Use the -delete option very carefully. The find command is treated as an expression, and if you add the -delete option first, the command deletes everything below the starting points you specify. Never use this rm -f /

In the case of directories, find can only delete empty directories like rmdir.

Muscal

Leave a Reply

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

Next Post

Install Python pip on Ubuntu 22.04

Fri Apr 7 , 2023
Python is a popular programming language that supports multiple paradigms such as object-oriented, functional, and procedural. Python is available in two versions: Python2 and Python3. Python2 is the legacy version that is no longer supported by the Python Software Foundation since January 1, 2020.
python3 pip install ubuntu

You May Like