×

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be used to search for files based on various criteria such as name, type, size, and date modified.

The basic syntax of the find command is:

find [path] [options] [expression]

The "path" is the directory or directories where the search should begin. If no path is specified, the search will begin in the current directory. The "options" are used to specify the criteria for the search, and the "expression" is used to specify the name of the file or files to be searched for.

The most basic use of the find command is to search for a specific file by name. For example, to search for a file called "fruits.txt" in the current directory, you would use the following command:

find . -name "fruits.txt"

This command will search the current directory and all its subdirectories for a file called "fruits.txt" and will display the path to the file if it is found.

The find command also has the ability to search for files based on various criteria such as type, size, and date modified. For example, the following command will search for all files in the current directory that are larger than 100KB:

find . -size +100k

The command will search the current directory and all its subdirectories for files larger than 100KB and will display the path to the files that are found.

The find command also has several options that can be used to modify its behavior. Some of the most commonly used options include:

  • -type: specifies the type of file to be searched for, such as f for regular files or d for directories.
  • -name: specifies the name of the file or files to be searched for.
  • -size: specifies the size of the file or files to be searched for.
  • -mtime: specifies the number of days ago the file was last modified.
  • -exec: allows you to execute a command on the files that are found.
  • -print: will print the files that match the search criteria.
  • -delete: will delete the files that match the search criteria.

The find command can also be used in combination with other commands to perform more complex tasks. For example, you can use the find command in combination with the grep command to search for a specific pattern in all files in a directory and its subdirectories. For example, the following command will search for the word "apple" in all text files in the "fruits" directory and its subdirectories:

find fruits -type f -name "*.txt" -exec grep "apple" {} +

The find command can also be used to perform actions on the files that are found. For example, the following command will search for all files in the current directory that are larger than 100KB and will delete them:

find . -size +100k -delete

Another useful feature of the find command is the ability to use regular expressions to search for files. This can be done by using the -regex option. For example, the following command will search for all files in the current directory that have a .txt or .md extension:

find . -regex ".*\.\(txt\|md\)"

The find command also has the ability to search for files that have specific permissions. This can be done by using the -perm option. For example, the following command will search for all files in the current directory that have read and write permissions for the owner:

find . -perm -600

The find command can also be used to search for files that were modified within a specific time frame. This can be done by using the -mtime option. For example, the following command will search for all files in the current directory that were modified within the last 24 hours:

find . -mtime 0

Additionally, the find command also has the ability to search for files based on their inode number. This can be done by using the -inum option. For example, the following command will search for all files in the current directory that have the inode number 12345:

find . -inum 12345

The find command also has the ability to search for files based on the user or group ownership. This can be done by using the -user or -group option respectively. For example, the following command will search for all files in the current directory that are owned by the user "john":

find . -user john

Another useful feature of the find command is the ability to search for files based on their access time. This can be done by using the -atime option. For example, the following command will search for all files in the current directory that were accessed within the last 24 hours:

find . -atime 0

The find command also has the ability to search for files based on their status change time. This can be done by using the -ctime option. For example, the following command will search for all files in the current directory that had their status changed within the last 24 hours:

find . -ctime 0

The find command can also be used to search for files based on their type, such as block special files, character special files, etc. This can be done by using the -type option along with the specific file type. For example, the following command will search for all block special files in the current directory:

find . -type b

In summary, the find command in Linux is a powerful command-line utility that is used to search for files and directories in a file system. It is versatile, it can be used to search for files based on various criteria such as name, type, size, date modified, user or group ownership, access time, change time and many more. The find command has several options that can be used to modify its behavior and it can be used in combination with other commands to perform more complex tasks. It is an essential command for anyone who works with files in Linux, it can help you quickly find the files you need, even in the most complex of file systems.


Related Topics

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

5 minutes read.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine...

4 minutes read.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 minutes read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in...

5 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name...

4 minutes read.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

4 minutes read.