[Sussex] Excluding hidden files and folders with find command

Fay Zee sussex at eglug.org.uk
Tue Sep 20 21:40:06 UTC 2011


Hi again Dominic

On 20 September 2011 17:06, Dominic Humphries  wrote:
>> What I don't understand about this command is that it just returns
an
>> alphabetised list of the root folders and files inside /home/fay
>
> This is because the command enclosed in backticks returns nothing,
so
> the above is basically the same as 'ls -l' which will default to
your
> current directory.

Doh!

> It looks like
> $ find . -name '*listit*' | grep -v 'Downloads' | grep -v
'/home/fay/listit/'
>
> would be the 'find' command you want, given the above scenario.

I tested this one out but I couldn't get it to work.
The output from the find command doesn't include the /home/fay part.
So it's been reduced from an absolute path to a relative path.
Each result begins with ./ and that's what grep receives.

$ find . -name '*listit*' | grep -v 'Downloads'
produces:
./Documents/how_to_choose_what_to_buy/listit
./Documents/how_to_choose_what_to_buy/listit/testlistit.html
./listit
./listit/january_listit.txt
and I only want the second result from that list.

Using the -type argument to restrict output to regular files as 
suggested by David B (18:58 post):

$ find . -type f -name '*listit*' | grep -v 'Downloads'
produces:
./Documents/how_to_choose_what_to_buy/listit/testlistit.html
./listit/january_listit.txt
gets me closer.

I still need help working out how to exclude results from the one 
/listit/ directory while still including results from the other 
/listit/ directory. In practice, there could be other /listit/ 
directories dotted around, but only the one /home/fay/listit/.

From man find:

-name pattern
Base  of  file  name  (the  path  with  the  leading
directories
removed) matches  shell  pattern  pattern.   The  metacharacters
(‘*',  ‘?',  and ‘[]') match a ‘.' at the start of the base
name
(this is a change in findutils-4.2.2; see section STANDARDS CON‐
FORMANCE  below).  To ignore a directory and the files under it,
use -prune; see an example in the description of -path.

-path pattern
File name matches shell pattern pattern.  The metacharacters  do
not treat ‘/' or ‘.' specially; so, for example,
          find . -path "./sr*sc"
will  print an entry for a directory called ‘./src/misc' (if one
exists).  To ignore a whole directory tree,  use  -prune  rather
than  checking every file in the tree.  For example, to skip the
directory ‘src/emacs' and all files and  directories  under  it,
and  print the names of the other files found, do something like
this:
          find . -path ./src/emacs -prune -o -print
Note that the pattern match test applies to the whole file name,
starting from one of the start points named on the command line.
It would only make sense to use an absolute path  name  here  if
the  relevant  start point is also an absolute path.

From these entries , I can't tell where to place the path I want to 
exclude. This command returns nothing:

$ find . -type f -name '*listit*' -path /home/fay/listit -prune | grep
-v 'Downloads'

 Best Regards,
Fay
East Grinstead Linux User Group
www.eglug.org.uk


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/sussex/attachments/20110920/d07e772c/attachment.htm>


More information about the Sussex mailing list