en 非公開: 40 Best Linux Find Command Examples

40 Best Linux Find Command Examples

The Linux find command is a powerful tool that allows system administrators to locate and manage files and directories based on a wide range of search criteria. You can search directories and files by name, type or extension, size, permissions, and more.

In addition to searching for files and directories, you can also combine the find command with other commands to perform actions on the results. Adding -exec option allows system administrators to run external commands and perform actions such as copying, moving, deleting, or changing permissions on files that match specified criteria such as size or name. Masu.

This article provides an example of the basic Linux find command. This section explains how to search for files and directories. Next, we’ll show you how to use the -exec option to manipulate files or directories based on size, permissions, etc.

The general syntax for the find command is:

 find {path} {name -of-file or dir-to-search} {action-to-take}

where,

  • Specify the path directory.
  • name-of file or dir-to-search : name of the file or directory to search
  • action-to-take : copy, delete, move, etc.

This tutorial shows you how to find files and directories that match a specified pattern. It also describes how to perform actions on files or directories found by the find command. This guide is based on Ubuntu, but is applicable to most Linux distributions and versions.

Searching files and directories

40 Best Linux Find Command Examples
40 Best Linux Find Command Examples

Search for specific files by name or extension

To search for a specific file, run the following command from root (/): This command includes the exact name of the file you are searching for.

 find . -name file22.txt

output

 ./test/file22.txt
./sales/file22.txt

Note that the result includes the path. This is important if you don’t know which directory the file is in, or if the file is in multiple locations.

You can also search for files in another directory while remaining in their current location. In this case, you must specify the path of the directory to search.

 find ./test -name file22.txt 
40 Best Linux Find Command Examples
40 Best Linux Find Command Examples

Looking for a specific file in another directory

In our case, we will search for all files starting with the letter file in the test directory.

 find ./test -name file*

output

 ./test/file22.txt
./test/file30.doc
./test/file1.txt
./test/file5,doc 
40 Best Linux Find Command Examples
40 Best Linux Find Command Examples

Find files by extension

To search for files with a specific extension on Linux, add it to the command.

 find . -name *.txt

output

 ./test/subtest/subfil.txt
./test/file22.txt
./test/file1.txt
./home1/files32.txt
./home2/file22.txt
./qa/tags.txt 
40 Best Linux Find Command Examples
40 Best Linux Find Command Examples

Find files and directories by name

Use the command below to search for files and directories that start with the letters qa . Our computer has qatree.txt and qa.txt files and a directory named qa .

When I run the command,

 find ./ -name "qa*"

The following output is returned

 ./test/qatree.pdf
./test/qa.txt
./home/qa

This command returns both files and directories that match your search criteria. To search only files or directories, you need to specify this in the command.

40 Best Linux Find Command Examples
40 Best Linux Find Command Examples

Search only files or directories

For files only, use type f switch.

file only

 find ./ -type f -name "qa*"

output

 ./test/qatree.pdf
./test/qa.txt

directory only

To search directories only, add type d option.

 find ./ -type d -name "qa*"

output

 ./home/qa

Case-insensitive search command

All searches using -name switch are case sensitive and do not return uppercase results. Use the -iname option to get all cases.

 find ./ -iname "qa*"

output

 ./test/qatree.pdf
./test/qa.txt
./test/QAtest.txt
./home/qa

Search for files in multiple directories

To search for files in another directory, add a path to the command. This example checks in test and numeric directories.

 find ./test ./numeric -name file22.txt -type f

output

 ./test/file22.txt
/root/numeric/file22.txt

Find multiple files with different extensions from all directories

The find command allows you to find multiple files that share different extensions such as *.doc , *.txt *.pdf etc. You can do this individually, one extension at a time, or use one command that includes all extensions. Required extension.

find . -type f ( -name "*.txt" -o -name "*.pdf" -o -name "*.doc" )

output

 ./test/subtest/subfil.txt
./test/qatree.pdf
./test/file22.txt
./test/qa.txt
./test/file30.doc
./books/acro.pdf
./data1/FILE22.txt
./docs/files32.txt

Find files containing specific text

Sometimes you want to access a file that contains specific text, but you can’t remember its file name or location. This command allows you to search all files containing the target text.

To find all files containing the word “hyperconvergence” use:

 find / -type f -exec grep -l -i "hyperconvergence" {} ;

output

 /root/numeric/hci
/root/HCIP

With –i option, the command is case-insensitive and searches for text regardless of whether it is uppercase or not (e.g. hyperconvergence , Hyperconvergence , etc.).

To search for files in a specific directory, simply add them to the command.

 find ./numeric -type f -exec grep -l -i "hyperconvergence" {} ;

output

 ./numeric/hci

Let’s take a closer look at the grep command example.

Find files and directories based on size

You can search for all files or directories that are less than a certain size, within a certain range, or empty. Use the appropriate size format depending on the type of file or directory you are searching.

Size options include:

c – bytes

k – kilobytes

M – megabyte

G – gigabyte

Find files of a certain size (equivalent to 30MB)

Search Search all 30MB files

 find / -size 30M

Find files larger than a specified size

 find -size +2M

output

 . /Downloads/ubuntu18043.iso 
./.cache/pip/http/6/e/3/8/3/6e38383f41850c3e5705d48116bc52f81b92a80500f414305057 7a9c

Find files smaller than 10MB in current directory

 find . -type f -size -10M

Find files between 100 and 200MB in size

When searching for files within a specific range such as 100-200MB

 find / -size +100M -size -200M

Look for directories larger than 20kb

find / -type d -size +20k

Find empty files and directories.

file

find ./ -type f -size 0

or

 find ./ -type f –empty

directory

 find ./ -type d –empty

Find files by age or modification time

Find files older than n days

 find /path/ -type f -name '*.txt' -mtime +8

-mtime +8 Search for txt files older than 8 days.

Modified date order

This will search for files modified within the last 17 hours

 find . -mtime -17 -type f 

Look for directories modified within the last 10 days

 find . -mtime -10 -type d 

Find files based on access or modification

Search for files based on the date or time they were accessed. This allows you to see which files have been accessed or not accessed within a specified period of time.

Shows files in your home directory that have not been accessed in the last 10 days.

 find /home -atime +10

Files accessed just 10 days ago

 find /home -atime 10

Accessed within the last 10 days

 find /home -atime -10

Find files modified within the last n days

You can also search for files in the /home directory that have been modified within the last 10 days using the following command:

 find /home -mtime -10 

Find files modified within a specific time period.

For example, all files in your home directory that were modified between 6 and 15 days ago.

 find /home -type f -mtime +6 -mtime -15

Files and directories accessed in the last 10 minutes

Use the -amin option to search for files accessed in the last 10 minutes.

 find . -amin -10 -type f

output

 ./.bash_history
./.profileroot@ubuntu1804:~#

Directories accessed in the last 10 minutes

 find . -amin -10 -type d

Find files matching specific permissions

syntax

 $ find -perm mode

Here, mode is a number such as 644, 655, 700, 777 , or a permission of u=x, a=r+x, or a letter.

Mode can be specified in three different ways:

  1. If you want to search for files with the exact permissions specified, do not prefix.
  2. Files with at least the specified permissions are marked with ” - “. This will return files with the specified permissions and additional higher permissions.
  3. To use ” / “, you must specify an owner or group that has permissions on the file.

Search for files with permissions 777

 find -perm 777

output

 ./file1

Search for at least 766 files
Search-perm-766

output

 ./file2
./file1

This command:

  • File owner has read/write/execute permissions.
  • Group has read/write permissions
  • others have read/write permissions

Therefore, two files (file1 and file2) that meet this criteria are returned. The file does not need exact 766 permissions and can have additional permissions as well, but it does need at least the specified permissions.

Find files writable by the owner

Now use ” / ” to search for files that are writable by the owner, group, or other users.

 find -perm /222
.
./file2
./file3
./file1

The above example searches for files that are writable by the owner or group.
This will return a file that is writable by either, but not necessarily by both. To display files that both have write permission to, use the – prefix.

 find -perm -220

output

 ./file2
./file1

Find files owned by a user

Find all files owned by Jack

 find /home -user jack

output

 /home/jack
/home/jack/examples.desktop
/home/jack/.gnupg
/home/jack/.gnupg/private-keys-v1.d
/home/jack/ans.cgi

Search for specific files owned by a user

Find all text files owned by Jack

 find /home -user jack -iname "*.txt"

output

 /home/jack/docs/file32.txt

Search for files and directories and list them with their permissions.

 find -name "*.conf" | ls -l

output

 total 120
drwxr-xr-x 2 root root 4096 Dec 31 13:19 backup2
drwxr-xr-x 2 root root 4096 Jan 4 11:57 Desktop
drwxr-xr-x 2 root root 4096 Dec 31 11:50 Documents
drwxr-xr-x 2 root root 4096 Dec 31 12:27 Downloads
-rw-r--r-- 1 root root 0 Jan 4 12:02 file22.doc
drwxr-xr-x 2 root root 4096 Jan 4 12:14 file23
-rw-r--r-- 1 root root 0 Jan 4 12:23 file23.txt

Find results and act on them

In this section, we’ll look at what you can do with files that match a pattern specified by the find command.

Find files and change permissions

Find and change permissions for specific file types. In this example, we will be working with PHP files that have different permissions, as shown below.

 jack@ubuntu1804:~/ver$ ls -la

total 8
drwxrwxr-x 2 jack jack 4096 Jan 3 14:11 .
drwxr-xr-x 8 jack jack 4096 Jan 3 14:05 ..
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 ans.php
-rw-rw-r-- 1 jack jack 0 Jan 3 14:11 base.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 query.php
-rw-rw-r-- 1 jack jack 0 Jan 3 14:11 qust.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 text.php
-rw-rw-r-- 1 jack jack 0 Jan 3 14:11 vary.php

Next, find all PHP files (above) and replace their permissions with 755.

 find ver -name "*.php" -type f -exec chmod 755 {} ;

This command searches for PHP files in the ver directory and sets their permissions to 755 ( rwxr-xr-x ).

result

 jack@ubuntu1804:~/ver$ ls -la
total 8
drwxrwxr-x 2 jack jack 4096 Jan 3 14:11 .
drwxr-xr-x 8 jack jack 4096 Jan 3 14:05 ..
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 ans.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:11 base.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 query.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:11 qust.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 text.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:11 vary.php

Find and change file and directory permissions

Find files with 644 permissions and change them to have 655 permissions.

 find . -type f -perm 644 -exec chmod 655 {} ;

You can also search for directories with permissions of 644 and replace them with 755.

 find . -type d -perm 644

output

 ./docs   

docs folder has 644 permissions

 drwxrwxr-x 2 jack jack 4096 Jan 3 12:45 docs

To set it to 755 , run the following command:

 find . -type d -perm 644 -exec chmod 755 {} ;

Now we can once again see what exactly 755 is.

 find . -type d -perm 755

output
.
./docs

From above, you can see that the root and docs directories have permissions of 755.

The Ls –la command gives the following details:

 drwxr-xr-x 2 jack jack 4096 Jan 3 12:45 docs

Find and copy files or directories

Find specific files and copy them to a directory

The command below finds the file22.tx t file and copies it to the ~/tmp/images directory.

 find -iname file22.txt -exec cp {} ~/tmp/images ;

Find and copy one type of file to a directory

To find files such as images with the jpg extension in the current directory and copy them to another location, such as an images folder, use:

 find -iname '*.jpg' -exec cp {} ~/tmp/images ;

This will find all jpg files and copy them to the ~/tmp/images folder.

Find and copy one file to multiple directories

Search a single directory and copy it to multiple directories.

 find ~/tmp/dir1/ ~/tmp/dir2/ $HOME/3/ -maxdepth 0 -exec cp ~/numeric/hci {} ;

This will search for the hci file and copy it to three directories /tmp/dir1/ /tmp/dir2/ and $HOME/3/ .

Find a file and move it to another directory

Moving known files from one directory to another. To move the universal.php file;

 find ~/folder/ -type f -name universal.php -exec mv {} ~/numeric/ ;

Find files with a specific extension and move them to another folder

 find ~/numeric/ -type f -name '*.php' -exec mv {} ~/folder/ ;

Find a specific file and move it to a specific different folder

 find -type f -name uni*.* -exec mv {} ~/unifiles/ ;

This command finds all files whose name starts with uni and has any extension. Then move them to the directory /unifiles/ .

Find and move files based on age

Find files older than a specified number of days and move them to another location, such as an archive.

 find /path/ -type f -name '*.pdf' -mtime +20 -exec mv {} /backup1/ ;

This will find PDF files older than 20 days and move them to the backup1 directory.

Find and delete files and directories

The syntax for finding and deleting files or directories in the current directory is:

find . -type f -name "file to delete" -exec rm -f {} ; Delete only the file or

find . -type d -name "dir-to-delete" -exec rm -rf {} ; To delete only a directory

Search for and delete only specific files

To find and delete files starting with til, use:

 find . -type f -name "til*" -exec rm {} ; 

To search for and delete directories starting with til

 find . -type d -name "til*" -exec rm {} ;

Delete both files and directories

 find . -name "til*" -exec rm -rf {} ; 

This will remove both files and directories that begin with the letters til.

Delete by extension

Below is how to find and delete all txt files in the current directory. Replace txt with another extension you want to remove, such as bak or pdf .

 find . -type f -name "*.txt" -exec rm -f {} ;

If you want the system to prompt you for confirmation before deleting each file, add the -i option as shown below.

 find . -type f -name "*.txt" -exec rm -i {} ;

output

 rm: remove regular empty file './File22.txt'? y
rm: remove regular empty file './file22.txt'? y
rm: remove regular empty file './file23.txt'?

By default, -rm does not remove directories, so you must use the –r option to ensure recursive removal. This ensures that empty directories and directories containing files are removed. The -f option forces deletion and is used for both files and directories.

Find and delete files older than n days

Finds and deletes backup files older than 20 days from the current directory.

 find . -type f -name '*.bak' -mtime +20 -exec rm {} ;

This will delete all .bak files older than 20 days.

Search and delete only directories

To delete the directory dir22

 find . -type d -name dir22 -exec rm -rf {} ;

Make sure the case of the directory name matches, or use the -iname option.

 find . -type d -iname Dir22 -exec rm -rf {} ;

Remove both Dir22 and dir22

Use the -i option to confirm before deleting.

 find . -type d -iname Dir22 -exec rm -rf -i {} ;

output
rm: Do you want to delete the directory “./Dir22”? n
rm: Do you want to delete the directory “./dir22”? y

In this example, we entered n for the directory Dir22 that will not be deleted and y for the directory dir22 that will be deleted.

Find and delete empty files

You can find and automatically delete empty files using one of the following commands:

 find ./ -type f -size 0 -exec rm -f {} ;

or
Find ./ -type f -size 0 | xargs rm -f
or
find ./ -type f -size 0 –delete

Find and delete empty directories

Use d option to remove empty directories.

 find ./ -type d -empty -exec rmdir {} ;

Another method is to use delete instead of delete.

 find ./ -type d -empty -delete

Be aware that deleting system files or important files from your computer may damage your operating system or applications or cause loss of important data.

To avoid accidental deletion, we recommend using a non-root user account. Also, make sure that you are deleting the appropriate files that are no longer useful and that you have backups of all your data files just in case.

What’s next?

Try the above Linux find command in a lab or non-production environment. See if you can automate file system cleanup using Crontab. To master Linux, check out this online course.

Easy-to-understand explanation of “Best 40 Examples of Linux Find Command”! Best 2 videos you must watch

【聞き流し/作業用】 聞き流しリファレンス 「主要Linuxサーバーコマンド編」【プログラミング】
https://www.youtube.com/watch?v=iOuGTOP3Dn0&pp=ygU1IExpbnV4IOOBriBGaW5kIOOCs-ODnuODs-ODieOBruODmeOCueODiCA0MCDkvosmaGw9SkE%3D
Linux コマンドとターミナルと仲良くなる勉強会
https://www.youtube.com/watch?v=M5JCfGttqno&pp=ygU1IExpbnV4IOOBriBGaW5kIOOCs-ODnuODs-ODieOBruODmeOCueODiCA0MCDkvosmaGw9SkE%3D