15 Useful Linux find command examples
In addition to the basic operation of finding files under the directory structure, we can also use find
the command to perform some practical operations, which will make our command line journey easy.
In this article, let us review 15 find
practical examples of Linux commands that are useful for newbies and experts alike.
First, create the following sample empty file in your home directory so that you can easily try out some of find
the command examples mentioned below.
$ vim create_sample_files.sh
touch MybashProgram.sh
touch mycprogram.c
touch MyCProgram.c
touch Program.c
mkdir backup
cd backup
touch MybashProgram.sh
touch mycprogram.c
touch MyCProgram.c
touch Program.c
$ chmod +x create_sample_files.sh
$ ./create_sample_files.sh
Check the running results as follows
1. Find a file using its name
This is the basic usage of the command. This example finds all files with the name MyCProgram.cfind
in the current directory and all its subdirectories .
$ find -name "MyCProgram.c"
2. Find files using name and ignore case
This is the basic usage of the command. This example finds all files with the name MyCProgram.cfind
(ignoring case) in the current directory and all its subdirectories .
$ find -iname "MyCProgram.c"
3. Use mindepth and maxdepth to limit the search to a specific directory level
Find the passwd file in all subdirectories starting from the root directory .
$ find / -name passwd
Find the passwd file in the root directory and go down one level. (i.e. root directory - 1 level, one subdirectory - 2 levels)
$ find / -maxdepth 2 -name passwd
Find the passwd files in both levels of root . (i.e. root — level 1, and two subdirectories — levels 2 and 3)
$ find / -maxdepth 3 -name passwd
Locate the passwd file between subdirectory levels 2 and 4 .
$ find / -mindepth 3 -maxdepth 5 -name passwd
4. Execute commands on files found by Find command
In the following example, find
the command evaluates all files with the name MyCProgram.cmd5sum
(ignoring case). {}
Replace with the current file name.
$ find -iname "MyCProgram.c" -exec md5sum {} \;
5. Reverse the search results
Displays files or directories whose names are not MyCProgram.c . Since maxdepth is 1, only the current directory is searched.
$ find -maxdepth 1 -not -iname "MyCProgram.c"
6. Find the file by inode Number.
Each file has a unique inode number using which we can identify the file. Create two files with similar names. i.e. one file has a space at the end.
$ touch "test-file-name"
$ touch "test-file-name " # 注意这后面的空格
[Note: There is a space at the end]
$ ls -1 test*
The results of viewing the file are as follows
From the ls output, we cannot tell which file has the trailing space. Using the -l option -i
we can see inode
the numbers of the files, which will be different for the two files.
$ ls -i1 test*
We can find
specify the inode number on the command as shown below. In this example, the command renames the file find
using the inode number.
$ find -inum 37945133 -exec mv {} new-test-file-name \;
$ ls -i1 *test*
We can use this when we want to do something with a poorly named file, as shown in the following example. For example, file?.txt
a file named - contains a special character. If we try to execute rm file?.txt
, all three of the following files will be deleted. So, follow these steps to delete only the " file?.txt " file.
$ ls
file1.txt file2.txt file?.txt
Find the inode number of each file.
$ ls -i1
Delete the files having special characters in them using inode number as shown below.
$ find -inum 37945122 -exec rm {} \;
7. Find files based on file permissions
The following operations can be performed.
- Find files matching exact permissions
- Checks whether the given permission matches, regardless of other permission bits.
- Search by giving octal/symbolic representation
For this example, let's assume that the directory contains the following files. Note that the file permissions for these files are different.
$ ls -l
Find (x)
files that have execute permission for the group. Use the following command to find all files in our home directory that are executable, regardless of the other permissions of the file.
$ find . -perm -g=x -type f -exec ls -l {} \;
Find files that have read permissions only for the group.
$ find . -perm g=r -type f -exec ls -l {} \;
Find files with read permissions only for group [search by octal]
$ find . -perm 040 -type f -exec ls -l {} \;
8. Find all empty files (zero-byte files) in your home directory and its subdirectories
Most of the files output by the following command will be lock files and placeholders created by other applications.
$ find ~ -empty
Just list all the empty files in our home directory.
$ find . -maxdepth 1 -empty
List only non-hidden, empty files in the current directory.
$ find . -maxdepth 1 -empty -not -name ".*"
9. Find the top 5 largest files
The following command will display the top 5 largest files in the current directory and its subdirectories. This may take a while to execute, depending on the total number of files the command must process.
$ find . -type f -exec ls -s {} \; | sort -n -r | head -5
10. Find the top 5 small files
The method is the same as for finding larger files, but the only difference in sorting is ascending.
$ find . -type f -exec ls -s {} \; | sort -n | head -5
In the above command, it is likely that we will see only zero-byte files (empty files). So, we can use the following command to list the smaller files except the zero-byte files.
$ find . -not -empty -type f -exec ls -s {} \; | sort -n | head -5
11. Use option -type to find files based on file type
Only socket files are found.
$ find . -type s
Search all directories
$ find . -type d
Search only regular files
$ find . -type f
Find all hidden files
$ find . -type f -name ".*"
Find all hidden directories
$ find -type d -name ".*"
12. Find files by comparing modification time of other files
Displays files modified after the specified file. The following find
command displays all the files created/modified after test-file-name .
$ find -newer test-file-name
13. Find files by size
Using the -size option, we can search for files by size.
Find files larger than a given size
$ find ~ -size +100M
Find files smaller than a given size
$ find ~ -size -100M
Find files that exactly match a given size
$ find ~ -size 100M
注意
: - means less than the given size, + means greater than the given size, and unsigned means the exact given size.
14. Create aliases for frequent lookup operations
If you find some lookup operation is very useful, you can set it as an alias and execute it at any time.
Frequently delete files named a.out .
$ alias rmao="find . -iname a.out -exec rm {} \;"
$ rmao
Delete the core files generated by the c program.
$ alias rmc="find . -iname core -exec rm {} \;"
$ rmc
15. Use find command to delete large archive files
The following command deletes files larger than 100M *.zip
.
$ find / -type f -name *.zip -size +100M -exec rm -i {} \;"
Use an alias rm100m
to delete all files larger than 100M *.tar
(delete100M). Use a similar concept and create aliases like rm1g
, to delete files larger than 1G , 2G , and 5Grm2g
respectively .rm5g
$ alias rm100m="find / -type f -name *.tar -size +100M -exec rm -i {} \;"
$ alias rm1g="find / -type f -name *.tar -size +1G -exec rm -i {} \;"
$ alias rm2g="find / -type f -name *.tar -size +2G -exec rm -i {} \;"
$ alias rm5g="find / -type f -name *.tar -size +5G -exec rm -i {} \;"
$ rm100m
$ rm1g
$ rm2g
$ rm5g
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Restart PostgreSQL in Ubuntu 18.04
Publish Date:2025/04/09 Views:72 Category:PostgreSQL
-
This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n
Issues to note when installing Apache on Linux
Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM
-
As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s