LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to tell "find" to not search inside hidden folders? (https://www.linuxquestions.org/questions/linux-general-1/how-to-tell-find-to-not-search-inside-hidden-folders-208169/)

robo555 07-22-2004 03:02 AM

How to tell "find" to not search inside hidden folders?
 
I basically want "find" to ignore all hidden files and folders.

I've got a command like:

find ~/ -type f -name 'whatever'

But it takes a while 'cos it seems to look inside all the hidden folders as well (where hidden folders are ones that begin with "."). What's the best way to search for files in my home dir, but don't bother looking inside hidden folders? I don't want it to display hidden files as well.

osvaldomarques 07-22-2004 04:02 AM

The first example won't show you any hidden file or directory
Code:

find . \( ! -regex '.*/\..*' \) -type f -name "whatever"
The second just discards the hidden directories, showing the hidden files into normal directories
Code:

find . \( ! -regex '.*/\..*/..*' \) -type f -name "whatever"
It's the regular expression option of find.

robo555 07-23-2004 02:25 AM

Thanks, what would be the equivalent for grep? Performing searches inside files, but ignoring hidden files and directories.

Vincent_Vega 07-23-2004 06:47 PM

If you're performing a grep inside a file you need to specify the name. Don't specify hidden files and it won't search them, right?!

whansard 07-23-2004 07:29 PM

find . \( ! -regex '.*/\..*' \) -type f -name "whatever" -exec grep "text_to_search" {} \;

i think.

robo555 07-23-2004 10:22 PM

Quote:

Originally posted by Vincent_Vega
If you're performing a grep inside a file you need to specify the name. Don't specify hidden files and it won't search them, right?!
I meant, given a directory, find all files that contain a certain string, but ignoring hidden files and directories.

osvaldomarques 07-23-2004 10:45 PM

Hi robo555:
You have to do nothing. Really, hard is to grep this hidden files. I prepared a directory with three files: 2 are normal and 1 is hidden. Here is the directory:
Code:

Modelo:~/teste/grep# ls -laR
.:
total 24
drwxrwxr-x    3 root    root        4096 Jul 23 23:28 .
drwxrwxr-x    3 root    root        4096 Jul 23 23:16 ..
drwxrwxr-x    2 root    root        4096 Jul 23 23:29 .hidden
-rw-rw-r--    1 root    root            6 Jul 23 23:17 .text
-rw-rw-r--    1 root    root            6 Jul 23 23:17 text1
-rw-rw-r--    1 root    root            4 Jul 23 23:17 text2

./.hidden:
total 20
drwxrwxr-x    2 root    root        4096 Jul 23 23:29 .
drwxrwxr-x    3 root    root        4096 Jul 23 23:28 ..
-rw-rw-r--    1 root    root            6 Jul 23 23:17 .text
-rw-rw-r--    1 root    root            6 Jul 23 23:17 text1
-rw-rw-r--    1 root    root            4 Jul 23 23:17 text2

The contents of the files are:
Code:

text1
HELLO

text2
ALO

.text
HELLO

The same files were copied to the subdirectory ".hidden".
Entering
Code:

Modelo:~/teste/grep# grep HELLO *
text1:HELLO

Entering
Code:

Modelo:~/teste/grep# grep  -r HELLO *
text1:HELLO

See what happens when we grep the hidden file and the files under the hidden directory. Enter
Code:

Modelo:~/teste/grep# grep -r HELLO * .*
text1:HELLO
./text1:HELLO
./.text:HELLO
./.hidden/text1:HELLO
./.hidden/.text:HELLO
../grep/text1:HELLO
../grep/.text:HELLO
../grep/.hidden/text1:HELLO
../grep/.hidden/.text:HELLO
.hidden/text1:HELLO
.hidden/.text:HELLO
.text:HELLO

Have a nice week end!

Vincent_Vega 07-24-2004 01:29 AM

Actually, that's what I meant - grep in a directory. By default it won't look pay any attention to the hidden files.

paulmarc 08-08-2011 07:30 AM

Search directory for files containing string, excluding hidden files/directories
 
Quote:

Originally Posted by robo555 (Post 1066036)
I meant, given a directory, find all files that contain a certain string, but ignoring hidden files and directories.

I would do it in this way:
Code:

find . \( ! -regex '.*/\..*' \) -exec grep -l "STRING_YOU_WANT_TO_CHECK_FOR" {} \;
Explanation:
  • \( ! -regex 'REGULAR_EXPRESSION' \) Used to exclude hidden files and directories.
  • -exec COMMAND {} \; Execute COMMAND on the arguments {}. '\;' is necessary to end the statement.
  • grep -l "STRING" FILENAME List the file name, if it contains the string.


All times are GMT -5. The time now is 11:19 PM.