Using the find command to exclude files for the searching

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Hello, friends. In this short post, I will help you to use the find command to show hidden files but excluding a specific file.

The Linux find command is used to search the file system. As you can imagine it is powerful, that is, if you manage to use it properly. There is nothing better for doing all kinds of file and folder searches than this command.

We can even use find to search according to many search criteria. And that’s the trick we’re going to show you today.

Use find to exclude from the search certain files

In the work I do, I was presented with the following situation. I wanted to search for all the hidden files in the web directory, but excluding .htaccess which refers to Apache guidelines.

First, it is necessary to invoke find and tell it what we want to display

find . -type f -name ".*" 

If we look closely at the command, we will notice that it is asking the current directory prompt to search for all files whose names begin with .. This will cause it to search for hidden files.

Sample output:

.
.
.
./netbeans/nb/.lastModified
./netbeans/php/.lastModified
.
.
.htaccess .
.
Find command
Find command

As you can see in the screen output, the .htaccess file is displayed.

So, how to do it? Well, it is simple with the find command and the ! or -not expression that we have to add to the command.

find . -type f -name ".*" ! -name .htaccess

Now this command, what it indicates is that it will do the same search but excluding the files named .htaccess.

If you execute it, you will have the correct result.

As you can notice in this example you can modify it to your liking and with only replacing .htaccess you can use it for other files.

One last point is that if you add -delete to the above command when you find the file, it will delete it.

find . -type f -name ".*" ! -name .htaccess -delete

Although I don’t recommend it very much, there you have that trick.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook