LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 07-22-2004, 02:02 AM   #1
robo555
LQ Newbie
 
Registered: Mar 2003
Posts: 7

Rep: Reputation: 0
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.
 
Old 07-22-2004, 03:02 AM   #2
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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.
 
Old 07-23-2004, 01:25 AM   #3
robo555
LQ Newbie
 
Registered: Mar 2003
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks, what would be the equivalent for grep? Performing searches inside files, but ignoring hidden files and directories.
 
Old 07-23-2004, 05:47 PM   #4
Vincent_Vega
Member
 
Registered: Nov 2003
Location: Jacksonville, FL
Distribution: Slackware & Arch
Posts: 825

Rep: Reputation: 31
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?!
 
Old 07-23-2004, 06:29 PM   #5
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix, 98,2000 + various
Posts: 3,171

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

i think.
 
Old 07-23-2004, 09:22 PM   #6
robo555
LQ Newbie
 
Registered: Mar 2003
Posts: 7

Original Poster
Rep: Reputation: 0
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.

Last edited by robo555; 07-23-2004 at 09:23 PM.
 
Old 07-23-2004, 09:45 PM   #7
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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!
 
Old 07-24-2004, 12:29 AM   #8
Vincent_Vega
Member
 
Registered: Nov 2003
Location: Jacksonville, FL
Distribution: Slackware & Arch
Posts: 825

Rep: Reputation: 31
Actually, that's what I meant - grep in a directory. By default it won't look pay any attention to the hidden files.
 
Old 08-08-2011, 06:30 AM   #9
paulmarc
LQ Newbie
 
Registered: Jul 2011
Distribution: Fedora
Posts: 11

Rep: Reputation: 4
Post Search directory for files containing string, excluding hidden files/directories

Quote:
Originally Posted by robo555 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
Several "find -exec" and "find | xargs" questions thanhvn Programming 4 12-02-2005 01:04 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Gnome "Search for Files..." not into hidden directory max74 Linux - Software 2 09-11-2003 06:53 PM


All times are GMT -5. The time now is 10:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration