How to search files effectively — Part 2 — GoGoSoon

5MinsLearn
7 min readMar 7, 2023

--

Hello People! Thanks for your great feedback on the basics of the find command blog. After reading that blog most people requested me to write the advanced version of find command. Thanks again for all your patience. Now, it's time to take your skills to the next level. In this article, let's explore the advanced version of find command.

For those who’re reading this for the first time, in my last blog about find command, I covered the basics of file searching and explored some powerful commands to search files quickly using Linux. If you haven't read my previous blog I would highly recommend you to read that blog and come back here.

How to search files owned by a user

find commands accept a special argument called user which you can use to filter the files owned by a user.

The syntax is,

find [path] -user [username] [options]

Let’s say you want to search all the files owned by me (my username on this laptop is aruna), you can achieve it by using the below command.

find ./5minslearn/ -user aruna
Search files owned by a user

The above command will list all the files owned by the user aruna in the 5minslearn directory.

You may notice the [options] added to the end of the syntax. This basically means you can add any arguments following this to make your search a bit more effective.

For example, let’s try to filter only the directories owned by me. To filter the directories, you have to add -type option. Here's the command for it.

find ./5minslearn/ -type d -user aruna
Find directories created by a user

From the above screenshot, you can notice that this command lists all the directories and sub-directories owned by me inside 5minslearn directory.

How to search files that have specific permission

Using -perm argument, you can search for files having specific permission.

Here’s the syntax,

find [path] -perm [permissions] [options]

For example, let’s assume you want to search all the files that have read-only permission in the current directory. The code for read-only files is 400.

Note: If you don’t know how this code ( 400) is generated and curious to know, please refer How can I remove permissions using Octal Mode? section of my Linux File Permission blog.

find . -perm 400
Find files with read-only permission

I created this welcome.txt file long ago with read-only permission. You can see from the above screenshot that find command has identified it perfectly.

I would like to share a weird experience that I faced in my career.

I was working on a project that contains a huge number of minified (around 200+) files. Out of those, I have to find all the files that don’t have read-only permission. I’m pretty sure that it’ll be too low that I can count on one hand. Looking over 200 files and finding 5 files is a time-consuming process. So, I decided to find an alternative way.

I searched on Google and I was surprised by the result.

It can be achieved by just prepending a -not flag with our command. Here's the command for you.

find . -not -perm 400

The command will list all the files in the current directory that do not have read-only permission.

Find all the files that do not have read-only permission

The additional advantage is that you can prepend this -not flag before any option in find command to find the opposite of the search query.

Here’s another example for you,

find . -not -type f

Running the above command will list all the items that are not files (directories, symlinks, etc. ) in the current directory.

How to search files that belong to a particular group

As we see in our File Permissions blog, there may be multiple people who share a common machine for their work. They’re grouped together as Developers, QA, etc. What if you want to find all the files that people in Developers group can view? That's difficult, right?

But don’t worry. Here’s a simple alternative from find command.

Using -group argument in the find command you can search for all files that belong to a group.

The syntax is,

find [path] -group [groupname] [options]

I have a group with my name on my machine. Let’s try to find all the files owned by my group.

find . -group aruna

You can also combine -group option with other find command options to refine your search

For example, you can combine -group option with -perm option to search files that belong to a group and have specific permissions.

find . -group aruna -perm 400

The above command will list all read-only files that belong to aruna group.

Similarly, as I mentioned earlier, you can also combine -group option with -not option to find files that do not belong to a particular group.

Let’s try to find the files that do not belong to the sudo group.

find . -not -group sudo

How to find modified files after a file

The -newer option in the find command search for files that are modified after the last modification of the given file.

The syntax is

find [path] -newer [reference_file]

For example, if you want to search for all files that are modified after notes.txt file you can use the following command.

find . -newer notes.tx

This command will search for all files in the current directory that were recently modified after notes.txt file.

Find recently modified files

Search for files that were accessed within minutes ago

To search for files that were accessed a few minutes ago, you can use -amin argument. This argument will accept the number of minutes ( n) and finds all the files that are accessed n minutes ago.

The syntax is,

find [path] -amin [n] [options]

For example, Let’s try to find the files that were accessed within the last 30 minutes,

find . -amin -30
Find all files that were accessed in the last 30 minutes

Hope you notice the negative (minus) sign before the number. It indicates the file access in the past.

How to search all the files that are empty

The -empty flag in the find command is used to search for files and directories that are empty.

Here’s the syntax,

find [path] -empty [options]

To search for files all empty files and directories, you can use the following command

find . -empty
Find all empty files and directories

To search only for the empty directories, you can combine the -empty option with -type option

find . -type d -empty
Find all empty directories

This command will list all the empty directories in the current directory.

The -empty flag is often appended by -delete flag to delete all the empty files and folders.

How to find files that match a specific regular expression

The -regex argument allows you to apply the filter by regular expressions.

Syntax is,

find [path] -regex [expression] [options]

For example, Let’s assume you want to search the files whose names start with the letter w. You can use the below command to do that,

find . -regex "./w.*"
Find all files that match the specified regular expression

Conclusion

In this article, you’ve learned some advanced tips to search files effectively and perform various operations over them.

I would recommend you learn the find command by trying it out. Assume a few scenarios yourself and try to find the files using the techniques you learned. Dropping a couple of scenarios for you here,

  1. Find and delete files that end with .txt
  2. Find files that are not owned by you
  3. Find files that do not match a pattern

Hope you enjoyed reading this article!

Subscribe to my newsletter by entering your email address in the below box to receive similar blogs in your inbox.

Have a look at my site which has a consolidated list of all my blogs.

Originally published at https://www.gogosoon.com on March 7, 2023.

--

--

5MinsLearn
5MinsLearn

Written by 5MinsLearn

Learn any concept under 5 minutes. Subscribe to newsletter at https://5minslearn.gogosoon.com

No responses yet