Find no match linux

Содержание
  1. 40 Best Examples of Find command in Linux
  2. Find files and Directories
  3. Find specific files by name or extension
  4. Looking for specific files in another directory
  5. Search for files by extension
  6. Find files and directories by name
  7. Find files or directories only
  8. Case insensitive find command
  9. Search for a file from multiple directories
  10. Find multiple files with different extensions from all directories
  11. Find files containing certain text
  12. Find Files and Directories Based on Size
  13. Find files of a certain size – equal to 30MB
  14. Find files larger than a specified size
  15. Find files less than 10MB in the current directory
  16. Find files with sizes between 100-200MB
  17. Look for directories larger than 20kb
  18. Find empty files and directories.
  19. Find files by age or modification time
  20. By modification date
  21. Find files based on access or modification
  22. Find files modified within the last n days
  23. Find files modified within a specific period.
  24. Files and directories accessed within the last 10 minutes
  25. Find files matching specific permissions
  26. Find files with permission 777
  27. Find files writable by the owner
  28. Find files owned by a user
  29. Find specific files owned by a user
  30. Find and list files and directories together with their permissions
  31. Find and act on the results
  32. Find files and change permissions
  33. Find and change file and directory permissions
  34. Find and copy files or directories
  35. Find and copy one file to many directories
  36. Find and move files to a different directory
  37. Find certain files and move to a specific different folder
  38. Find and move files based on age
  39. Find and delete files and directories
  40. Find and delete specific files only
  41. Remove both files and directories
  42. Delete by extension
  43. Find and delete files older than n days
  44. Find and delete directories only
  45. Find and remove empty files
  46. Find and remove empty directories
  47. What’s next?
  48. 25 simple examples of Linux find command
  49. Linux find command
  50. 1. List all files in current and sub directories
  51. 2. Search specific directory or path
  52. 3. Limit depth of directory traversal
  53. 4. Invert match
  54. 5. Combine multiple search criterias
  55. 6. Search only files or only directories
  56. 7. Search multiple directories together
  57. 8. Find hidden files
  58. 9. Find files with certain permissions
  59. 10. Find files with sgid/suid bits set
  60. 11. Find readonly files
  61. 12. Find executable files
  62. 13. Find files owned to particular user
  63. 14. Search files belonging to group
  64. Search file and directories based on modification date and time
  65. 15. Find files modified N days back
  66. 16. Find files accessed in last N days
  67. 17. Find files modified in a range of days
  68. 18. Find files changed in last N minutes.
  69. 19. Files modified in last hour
  70. 20. Find Accessed Files in Last 1 Hour
  71. 21. Find files of given size
  72. 22. Find files in a size range
  73. 23. Find largest and smallest files
  74. 24. Find empty files and directories
  75. Some advanced operations
  76. 25. List out the found files
  77. 26. Delete all matching files or directories
  78. Summary
  79. 55 thoughts on “ 25 simple examples of Linux find command ”

40 Best Examples of Find command in Linux

Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.

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

Besides locating files and directories, combining the find command with others enables you to take action on the results. Adding the -exec option enables sysadmins to run external commands and perform actions like copying, moving, deleting, or changing permissions of the files matching the specified criteria such as size, name, etc.

In this article, we will start by explaining the basic Linux find commands with examples. This will show you how to find files and directories. We will then show you how to use the -exec option to act on the files or directories based on their size, permissions, etc.

The general syntax for the find command is

  • path specifies the directory.
  • name-of file or dir-to-search : Name of the file or directory to look for
  • action-to-take : such as copy, delete, move, etc.

In this tutorial, we will explain how to locate files and directories matching specified patterns. We will also see how to perform actions on the files or directories that the find command locates. The guide is based on Ubuntu but is applicable to most Linux distributions and versions.

Find files and Directories

Find specific files by name or extension

To look for a specific file, run the following command from the root (/). The command contains the exact name for the file you are searching for.

Please note that the results include the path. This is important if you don’t know the directory where the file is located, or when it is in more than one place.

You can also search for the file in another directory while still in the current location. In this case, you need to provide the path for the directory where you want to search.

Looking for specific files in another directory

In our case, we will look for all those starting with the letters file in the test directory.

Search for files by extension

To find a file in Linux with a certain extension, add it to the command.

Find files and directories by name

Use the command below to look for files and directories starting with the letters qa . In our computer, we have the qatree.txt and qa.txt files as well as a directory by the name qa .

If we run the command;

It returns the following output

The command returns both the files and directories matching the search criteria. To find files or directories only, you need to specify this in the command.

Find files or directories only

For files only, use the type f switch.

Files only

Directories only

Add the type d option to locate directories only.

Case insensitive find command

All searches with -name switch are case sensitive and will not give results with capital letters. To get all cases, use the -iname option.

Search for a file from multiple directories

To find the files in different directories, add their paths in the command. In our case, we will check in the test and numeric directories.

Find multiple files with different extensions from all directories

You can use the find command to locate multiple files that share the different extensions such as *.doc , *.txt *.pdf , etc. This can be done separately, one extension at a time, or using just one command that includes all the desired extensions.

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

Find files containing certain text

Sometimes, you want to access a file containing certain text but cannot recall its file name or location. This command allows you to find all the files containing your target text.

To look for all the files containing the word hyperconvergence”, use;

The –i option enables the command to ignore cases and will find the text whether capitalized or not i.e. hyperconvergence, Hyperconvergence , etc.

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

Find Files and Directories Based on Size

You can find all files or directories that are smaller, equal or greater than a certain size, within a certain range or empty. Use the appropriate size format depending on the type of files or directories you are searching for.

Size options include;

Find files of a certain size – equal to 30MB

To Search find all 30MB files

Find files larger than a specified size

Find files less than 10MB in the current directory

Find files with sizes between 100-200MB

When looking for files within a specific range such as between 100 and 200 MB

Читайте также:  Срок окончания активации windows

Look for directories larger than 20kb

find / -type d -size +20k

Find empty files and directories.

Files

find ./ -type f -size 0

Directories

Find files by age or modification time

Find files older than n days

The -mtime +8 will look for txt files that are older than 8 days.

By modification date

This will look for files modified within the last 17 hours

Looks for directories modified within the last 10 days

Find files based on access or modification

Find files based on date or time accessed. This allows you to see files that have or haven’t been accessed within a specified period.

To see files that have not been accessed within the last 10 days in the home directory.

Files accessed exactly 10 days ago

Accessed within the last 10 days

Find files modified within the last n days

You can also look for the files in the /home directory modified within the last 10 days using the command;

Find files modified within a specific period.

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

Files and directories accessed within the last 10 minutes

To find the files accessed within the last 10 minutes, use the -amin option.

Directories accessed within the last 10 minutes

Find files matching specific permissions

Where mode is the permission which is either numeric such as 644, 655, 700, 777 , etc, or letters such as u=x, a=r+x, etc.

You can specify the mode in the following three different ways.

  1. Without a prefix when you want to find files with the exact permissions specified.
  2. With “ — “ for files with at least the specified permission. This returns files with the specified as well as additional higher permissions.
  3. Using “ / ” requires specifying the owner or group with the permission to the file.

Find files with permission 777

Find files with at least 766
find -perm -766

The command looks for all files in which the

  • The file owner has read/write/execute permissions.
  • Group has read/write permissions
  • Others have read/write permission

As such, it returns two files that meet this criterion – file1 and file2. The files do not need to have the exact 766 permissions and can have additional ones as long but must have at least the specified.

Find files writable by the owner

We will now use the “ / ” to looks for files writable by either their owner, or group, or others.

The above looks for files that are writable by either their owner or group.
This returns files that are writable by either but not necessarily both. To see files, where both have writable permissions, use the – prefix.

Find files owned by a user

Find all files owned by Jack

Find specific files owned by a user

Find all text files owned by Jack

Find and list files and directories together with their permissions

Find and act on the results

In this section, we will look at how you can act on the files that match the pattern specified in the find command.

Find files and change permissions

Find and change permissions of certain file types. In our case, we will work with PHP files with different permissions as shown below.

We will now look for all the PHP files (above) and replace their permissions with 755

The command looks for PHP files in the ver directory and then sets their permission to 755 ( rwxr-xr-x )

Find and change file and directory permissions

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

You can also look for directories with 644 permissions and replace this with 755.

The docs folder has 644 permissions

To set it to 755 , we run

Now we can check again to see what exactly 755

From above we can see the root and docs directories have the 755 permissions.

Ls –la command gives the following details

Find and copy files or directories

Find and copy a specific file to a directory

The command below will find the file22.tx t file and copy it to the

Find and copy one type of files to a directory

To find files such as images with jpg extension in the current directory and copy them to a different location like an images folder, use;

This will find and copy all the jpg files to the

Find and copy one file to many directories

Find and copy a single to multiple directories.

This will find the file hci file and copy it to the three directories of /tmp/dir1/ /tmp/dir2/ and $HOME/3/

Find and move files to a different directory

To move a known file from a directory to another. To move the universal.php file;

Search and move files with a certain extension to a different folder

Find certain files and move to a specific different folder

The command looks for all the files with names starting with uni and having any extension. It then moves them to the directory /unifiles/

Find and move files based on age

Find and move files older than specified days to a different location such as the archive.

This will look for pdf files older than 20 days and move them to the backup1 directory.

Find and delete files and directories

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

find . -type f -name «file to delete» -exec rm -f <> ; to delete files only or

find . -type d -name «dir-to-delete» -exec rm -rf <> ; to delete directories only

Find and delete specific files only

To find and delete files starting with til, use;

To find and delete directories starting with til

Remove both files and directories

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

Delete by extension

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

In case you want the system to prompt you to confirm before deleting each file, add the -i option as below.

By default, the -rm will not remove the directories and you need to use the –r option to ensures a recursive removal. This ensures the deletion of empty directories and those containing files. The -f option forces the removal and is used for both the files and directories.

Find and delete files older than n days

Find and delete backup files older than 20 days from the current directory.

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

Find and delete directories only

To delete a directory called dir22

Ensure that you match the directory name case or use the -iname option.

Removes both Dir22 and dir22

To confirm before deletion, use the -i option.

Output
rm: remove directory ‘./Dir22’? n
rm: remove directory ‘./dir22’? y

In our case, we typed n for directory Dir22 which will not be deleted and y for the dir22 which will now be removed.

Find and remove empty files

You can use any of the following commands to locate empty files and delete them automatically.

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

Find and remove empty directories

To remove empty directories we will use the d option.

Another alternative is to use delete instead of remove.

Please note that deleting system or critical files from your computer can damage the operating system or applications or lead to loss of your important data.

To avoid accidental deletions, it is best practice to use a non-root user account. Also, ensure that you are deleting the right files that are not useful and that you have a backup of all your data files just in case.

Читайте также:  Оболочка для windows ce навигатор

What’s next?

Go ahead and try the above Linux find commands in your lab or NON PRODUCTION environment. See if you can use some to automate the file system cleanup with Crontab. And, to master Linux, check out this online course.

Источник

25 simple examples of Linux find command

Linux find command

The Linux find command is a very useful and handy command to search for files from the command line. It can be used to find files based on various search criterias like permissions, user ownership, modification date/time, size etc. In this post we shall learn to use the find command along with various options that it supports.

The find command is available on most linux distros by default so you do not have to install any package. The find command is an essential one to learn, if you want to get super productive with the command line on linux.

The basic syntax of the find command looks like this

1. List all files in current and sub directories

This command lists out all the files in the current directory as well as the subdirectories in the current directory.

The command is same as the following

2. Search specific directory or path

The following command will look for files in the test directory in the current directory. Lists out all files by default.

The following command searches for files by their name.

We can also use wildcards

Note that all sub directories are searched recursively. So this is a very powerful way to find all files of a given extension.

Trying to search the «/» directory which is the root, would search the entire file system including mounted devices and network storage devices. So be careful. Of course you can press Ctrl + c anytime to stop the command.

Ignore the case

It is often useful to ignore the case when searching for file names. To ignore the case, just use the «iname» option instead of the «name» option.

3. Limit depth of directory traversal

The find command by default travels down the entire directory tree recursively, which is time and resource consuming. However the depth of directory travesal can be specified. For example we don’t want to go more than 2 or 3 levels down in the sub directories. This is done using the maxdepth option.

The second example uses maxdepth of 1, which means it will not go lower than 1 level deep, either only in the current directory.

This is very useful when we want to do a limited search only in the current directory or max 1 level deep sub directories and not the entire directory tree which would take more time.

Just like maxdepth there is an option called mindepth which does what the name suggests, that is, it will go atleast N level deep before searching for the files.

4. Invert match

It is also possible to search for files that do no match a given name or pattern. This is helpful when we know which files to exclude from the search.

So in the above example we found all files that do not have the extension of php, either non-php files. The find command also supports the exclamation mark inplace of not.

5. Combine multiple search criterias

It is possible to use multiple criterias when specifying name and inverting. For example

The above find command looks for files that begin with abc in their names and do not have a php extension. This is an example of how powerful search expressions can be build with the find command.

When using multiple name criterias, the find command would combine them with AND operator, which means that only those files which satisfy all criterias will be matched. However if we need to perform an OR based matching then the find command has the «o» switch.

The above command search for files ending in either the php extension or the txt extension.

6. Search only files or only directories

Sometimes we want to find only files or only directories with a given name. Find can do this easily as well.

Quite useful and handy!

7. Search multiple directories together

So lets say you want to search inside 2 separate directories. Again, the command is very simple

Check, that it listed files from 2 separate directories.

8. Find hidden files

Hidden files on linux begin with a period. So its easy to mention that in the name criteria and list all hidden files.

9. Find files with certain permissions

The find command can be used to find files with a specific permission using the «perm» option. The following command searches for files with the permission 0664

This can be useful to find files with wrong permissions which can lead to security issues. Inversion can also be applied to permission checking.

10. Find files with sgid/suid bits set

The «perm» option of find command accepts the same mode string like chmod. The following command finds all files with permission 644 and sgid bit set.

Similarly use 1664 for sticky bit. The perm option also supports using an alternative syntax instead of octal numbers.

Note that the «2>/dev/null» removes those entries that have an error of «Permission Denied»

11. Find readonly files

Find all Read Only files.

12. Find executable files

The following command will find executable files

13. Find files owned to particular user

To find all or single file called tecmint.txt under /root directory of owner root.

We could also specify the name of the file or any name related criteria along with user criteria

Its very easy to see, how we can build up criteria after criteria to narrow down our search for matching files.

14. Search files belonging to group

Find all files that belong to a particular group.

Did you know you could search your home directory by using the

Search file and directories based on modification date and time

Another great search criteria that the find command supports is modification and accessed date/times. This is very handy when we want to find out which files were modified as a certain time or date range. Lets take a few examples

15. Find files modified N days back

To find all the files which are modified 50 days back.

16. Find files accessed in last N days

Find all files that were accessed in the last 50 days.

17. Find files modified in a range of days

Find all files that were modified between 50 to 100 days ago.

18. Find files changed in last N minutes.

Find files modified within the last 1 hour.

19. Files modified in last hour

To find all the files which are modified in last 1 hour.

20. Find Accessed Files in Last 1 Hour

To find all the files which are accessed in last 1 hour.

21. Find files of given size

Search files and directories based on size. To find all 50MB files, use.

22. Find files in a size range

To find all the files which are greater than 50MB and less than 100MB.

23. Find largest and smallest files

The find command when used in combination with the ls and sort command can be used to list out the largest files.
The following command will display the 5 largest file in the current directory and its subdirectory. This may take a while to execute depending on the total number of files the command has to process.

Similary when sorted in ascending order, it would show the smallest files first

24. Find empty files and directories

The following command uses the «empty» option of the find command, which finds all files that are empty.

Читайте также:  Remove windows sign in

To file all empty directories use the type «d».

Really very simple and easy

Some advanced operations

The find command not only finds files based on a certain criteria, it can also act upon those files using any linux command. For example, we might want to delete some files.

Here are some quick examples

25. List out the found files

Lets say we found files using find command, and now want to list them out as the ls command would have done. This is very easy.

26. Delete all matching files or directories

The following command will remove all text files in the tmp directory.

The same operating can be carried out with directories, just put type d, instead of type f.

Lets take another example where we want to delete files larger than 100MB

Summary

So that was a quick tutorial on the linux find command. The find command is one of the most essential commands on the linux terminal, that enables searching of files very easy. Its a must of all system administrators. So learn it up. Have any questions ? Leave a comment below.

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

55 thoughts on “ 25 simple examples of Linux find command ”

Great summary with categorized examples
Thanks!

Long Path tool can resolve error messages like: path too long, cannot delete file, too long path, destination path is too long, etc.

Very good article but how i can use find to search names with (S) for example ?

Becuase i try :
find . -iname ‘(S)’
find . -iname “(S)”

but the no results

Have you tried:
find . -iname ‘*(S)*’
or
find . -iname “*(S)*”
?

I wanna find a certain txt doc. in ROOT by using:
find / -name test.txt
Why does this command not work?

use this command instead (find ./filename)

Was after a solid search and replace technique for file names today, and uncovered this page on ‘find’. Thanks for the tips. I am sure I’ll incorporate them into day-to-day operations.

Hi everyone. I am Kreg i am from Philipines. Thanks for approved.

Thanks for the article. It’s really useful. I am wondering how can I use the find command and exclude the files that are using by some processes? I can find if files are used by process by:
find path_to_files -type f -name “some_name” -exec fuser <> \;
The output will show if there are files used by any processes but how can I get output that will show only files that are currently not used by any process?

It would become more perfect if “Delete all files which are older than X time” in the last. This is a good article. Thanks for sharing.

Your article is very informative. it’s really helpful……

You use the word “criterias”. That’s a mistake. Criteria is already plural. The singular is criterion. Thanks for providing the info here.

Excelente presentacion. buenos ejemplos y muy practicos.
Saludos.

That was really helpful. Concise and clear article, thank you!

Very nice presentation. Thank you so much

Very useful find command explanation. Thanks

Yes you are right about it

In connection with -o need to mention use of escaped parentheses \(, \) to group the criteria

Note: one criterion, two criteria, no criterias.

Also “Its” is a possessive adjective. “it’s” is short for “it is”.

thanks a lot for very useful post =))

Very useful article! Thank you!

You are awesome! this is a treasure trove of helpful information!

I used to have similar problems too, but after using”long path tool” everything was solved.

Please use this software and solve your computer, copy, delete, long path files.

Thanks for these good examples.

The path you entered, is too long. Enter a shorter path
File Name could not be found. Check the spelling of the filename,
and verify that the file location is correct.

I used to have similar problems too, but after using
“long path tool” You can use to solve this problem.

The path you entered, is too long. Enter a shorter path
File Name could not be found. Check the spelling of the filename,
and verify that the file location is correct.

Do not worry if you want to remove the blocked files or too long path files from your system, here I suggest a smooth way. Use “Long path tool” software and keep yourself cool.

Great intro! Thanks!
I think I have found a typo, in the Section “Find readonly files”: instead of “-perm /u=r” (which means: at least user-readable — compare with the following section), it should be “-perm -u=r”. That is, according to the man, ‘/’ means “at least” and ‘-‘ means “exactly”. I admit though that the man daunted me before I saw this page.

every command i tried fails with “find: paths must precede expression”
why?

find . -type f -exec ls -s <> \; | sort -n -r | head -5

-s what it will do ??

what about braces “ < >” in that ?

find . -type f -exec ls -s <> \; | sort -n -r | head -5

find = find files
. = directory to start at
-type = only match regular files

-exec = when you find a matching file, run this command
<> = the matching file
\; = the end of the command

So when find matches find foo.txt, it runs:

What -s means depends on the ls command.

Sorry, had a typo, it should be:

So when find matches foo.txt, it runs:

-s it print the allocated size

Thank you very much. i FIND it very helpful 🙂
Could you also please post some info regarding finding all files that contains a particular search text.

Isn’t command 25 deleting everyting larger than 10M, not 100M like in the description?

Thanks a lot. Clear and complete !

Very good. Thank you.

Awesome article . Great work

Rakesh, when you use <> ; with an exec statement the find utility will replace <> with the path and filename. In essence in the the example above if the following were the results without an exec command:

$ find /home/bob/dir -type f -name *.log -size +10M
/home/bob/dir/large.log
/home/bob/dir/even_larger.log

What find would do is run 2 separate statements:

$ find /home/bob/dir -type f -name *.log -size +10M -exec rm -f <> ;
rm -f /home/bob/dir/large.log
and
rm -f /home/bob/dir/even_larger.log

It is helpful to know that the path is relative, so if you were to say be in the /home/bob folder and use:
find . type f -name *.log -size +10M

Your results would end up like this:
./dir/large.log
./dir/even_larger.log

Very good briefing on FIND. perhaps AWK should be next? 🙂

I think you’ve got an mistake: Hidden files do NOT start with a period, they start with a dot. In your example you search in your home dir for hidden files.
The way to find executable files by usind find ist the “-executable” flag and you have to know that directories executeables, too.
If you just want to find executable files you can use

find PATH -executable -type f -name “whatever*”

on the other hand, if you looking for directories you can use

find PATH -executable -type d -name “whatever*”

what’s the difference between dot and period in the command line?

really helpfull, thank you very much

All the commands really useful…Thank you very much

thanks good article

Good examples, well explained.
But…
You need to quote all the wildcards in examples 2, 3, 4, 6, 7, 26.
Unless you’re going to explain to your readers why commands might fail unexpectly 🙂

Nice article, I bookmarked it for reference. It’d be interesting to have an offline version available for download, preferably as simple text, manpage or texinfo format.

Try using the backslash character before the parentheses, like “\(S\)”.

Источник

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