Linux find in current folder

25+ most used find commands in Linux [Cheat Sheet]

find command is one of the popular and commonly used tools in Unix-based operating systems. As its name suggests, it finds the files and directories in a directory hierarchy. You can pass different parameters and search files by their name, extension, type, size, permissions, modification time, owner, groups, and more. It performs a recursive search on the specified directory which means it searches in all sub-directories too.

Syntax to use find command

The syntax to use the find command is:

$ find [options] [path] [expression]
  • options : It includes the options that must appear before the first path name: -H , -L , -P , -D , and -O .
  • path : It indicates the directory path where you want to search the files.
  • expression : It defines search options, patterns to match, and actions to perform on the files.

The default path is the current working directory. When the find command is used without any parameters, it lists all files and folders in the current directory and its sub-directories.

Different examples to use find command

1. Find all files and directories in the current directory

Use find command followed by . symbol to search for all files and directories in the current working directory.

Sample Output:

find command to search for files in the current working directory

2. Find files in the specific directory

To find files in the specific directory, use the following syntax.

The following example searches for all files and directories present in the /etc/apt directory.

Sample Output:

find command to find files in the specific directory

3. Find only directories with find command

The -type option followed by d searches for directories only. The following example finds all directories in the home/golinux/computing directory.

$ find /home/golinux/computing -type d 

Sample Output:

Читайте также:  How to create txt file on linux

find command to find all directories

4. Find only files with find command

The -type f is used for regular files. It lists all files present in the specified directory.

$ find /home/golinux/computing -type f

Sample Output:

find command to find files only

The -type l will search for symbolic links only.

Sample Output:

find command to search for symbolic links only

6. Find files by name

You can find files by name using the -name option. It is case-sensitive. It is useful for checking whether the file is present in the specified directory hierarchy.

Run this command to locate new.txt files in the current working directory.

Sample Output:

find command to search files by name

7. Find directories by names

Similarly, you can search for directories by name with the following command. It finds all directories having a name spark in the current working directory.

Sample Output:

find command to search directories by name

8. Find files by case insensitive name

You can use -iname option to search for files by name where the match is case insensitive.

Sample Output:

find command to search files by ignoring cases

9. Find specific files by extension

With this method, you can find all files in the directory which has the same extension. For instance, to locate all .cpio files, use the command below.

Sample Output:

find command to search files by extension

10. Find files having specific patterns

You can use find command to search files by specific patterns, like files whose name begins with prep . The following command locates files whose name starts with prep in the /tmp directory.

$ find /home/golinux/record -name "tes*"

Sample Output:

# find /tmp/ -type f -name "prep*" /tmp/prepTspInstall18457.log /tmp/prepTspInstall2897.log /tmp/prepTspInstall11221.log /tmp/prepTspInstall15505.log /tmp/prepTspInstall13806.log

11. Find empty files and directories

The -empty option searches for the empty files and directories only.

$ find /home/golinux/test -empty

Sample Output:

find empty files and directories using find command

12. Find files with executable permission

The -executable flag matches files that are executable and directories that are searchable. To search executable files only in the current directory, you can run the following command.

Sample Output:

find command to search for executable files

13. Find files based on size

You can find files by size using the -size option. The following suffixes are used with -size .

  • b : 512 byte blocks (default, if no suffix is used)
  • c : bytes
  • w : two-byte words
  • k : Kilobytes (1024 bytes)
  • M : Megabytes
  • G : Gigabytes

The following command finds all files that are exactly 960 bytes in the specified directory.

$ find /home/golinux/record -type f -size 960c

Sample Output:

find files by size with find command

To find files that are greater or smaller than the specified size, use the + or — operator respectively.

This command finds files that are greater than 20MB in the Downloads directory.

$ find /home/golinux/Downloads -type f -size +20MB

find command to find files by size

The following command finds files that are smaller than 20MB in the Downloads directory.

$ find /home/golinux/Downloads -type f -size -20MB

find command to find files by smaller size

14. Find files by size range

You can also search files in a specific size range. For example, this command searches for files having sizes between 1 and 20 MB.

$ find /home/golinux/Downloads -type f -size +1M -size -20M

Sample Output:

Читайте также:  Linux удалить все найденные файлы

find command to find files by specific size range

15. Find files by permission

You can use -perm option to search for files on the basis of permissions. The following command finds all files which have read and write permission for their owner and group but are only readable by other users.

Sample Output:

find command to search for files by permissions

Similarly, to search for files that have full access (read/write/execute) to anyone, you can use:

16. Find files by modification time

The -mtime option finds files that were last modified n*24 hours ago. The following command will search for all files that are modified 15 days ago.

$ find /home/golinux/test -mtime 15

Sample Output:

find command to search files by modification time

You can also use + or — operator for specifying the greater or smaller than n value. For example, this command will search files that are modified less than 15 days ago.

$ find /home/golinux/test -mtime -15

Sample Output:

find command to search for files by modification time

17. Find files by last accessed time

The -amin option finds files that were last accessed n minutes ago. You can use the following command to search for files that were last accessed 5 minutes ago.

$ find /home/golinux/test -amin 5

Sample Output:

find command to find files by last accessed time

The -atime option finds files that were last accessed n*24 hours ago. This command will search files that were last accessed 5 days ago.

$ find /home/golinux/test -atime 5

18. Find files by changed time

The -cmin option allows you to search files that are changed n minutes ago. You can run the following command to find all files that are changed 5 minutes ago.

Sample Output:

find files by changed time

To find files that are changed n days ago, you have to use the -ctime option. The following command searches for all files that are changed 5 days ago.

19. Find all files owned by specific user

You can use -user option to find files that are owned by the specified user. For example, this command will search all files that are owned by the user deepak .

Sample Output:

find command to search for files by user

20. Find all files owned by specific group name

The -group option allows you to search files based on the group name. The following command finds all files in the current directory that belong to the group student .

Sample Output:

find command to search files by group

21. Find and delete files

You can use -delete flag at the end of a command to delete files if found in the specified location. For example, the following command will remove all .txt files from the directory /home/golinux/test .

$ find /home/golinux/test -name "*.txt" -delete

Sample Output:

It is always the best choice to check files before deleting them.

find command to find and delete files

22. Find text in files

The -exec flag runs the specified command on the files. The following command finds the text book in all .txt files in the current working directory.

$ find . -name "*.txt" -exec grep 'book' <> \;

Here, the -exec executes the grep command with a pattern book to match. The brackets <> are replaced by the filename found by the find command. The semicolon ; denotes the end of a command argument.

Читайте также:  Virtualbox монтирование общей папки linux

Sample Output:

find command to find text in files

23. Find text files and view their content

Similarly, you can run the cat command with the -exec option to view the content of all text files.

$ find /home/golinux/test -name "*.txt" cat <> \;

Sample Output:

find text files and view their content

24. Find the bash script file and execute it

The following command finds a bash script file script.sh in the current working directory and runs the script.

$ find . -name "script.sh" -exec bash <> \;

Sample Output:

find command to search script file and run it

25. Find files by limiting the directory depth to search

You can limit the recursion of the depth using the -maxdepth or -mindepth flag.

The following command searches for all .txt files in the directories that are not more than 3 levels deeper than the current working directory.

$ find . -maxdepth 3 -name "*.txt"

Sample Output:

find command to set the maximum recursion depth

The following example includes only the directories that are at least 3 levels deeper than the current working directory.

$ find . -maxdepth 3 -name "*.txt"

Sample Output:

find command to set the minimum recursion limit

To search for all .txt files in the directories that are at least 2 levels and not more than 4 levels deeper than the current working directory, you can use this command.

$ find . -mindepth -2 -maxdepth 4 -name "*.txt"

26. Find and print files that do not match the pattern

You can use the -not or ! option to search files that do not match the search pattern. For example, to find all files that are not owned by user deepak in the directory /home/golinux/test , you can use the command below.

$ find /home/golinux/test -type f -not -user deepak
$ find /home/golinux/test -type f ! -user deepak

Sample Output:

find files that do not match the search pattern

Conclusion

You have learned various examples of the find command to search files and directories based on different conditions. We have also shown you how to find files and perform operations on them using the find command. find is one of the most powerful commands and very useful for searching files and directories. Now you should know how to use find command in Linux. If you have any questions, please let us know in the comment section.

What’s Next

Further Reading

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Оцените статью
Adblock
detector