[Sussex] Excluding hidden files and folders with find command

Dominic Humphries linux at oneandoneis2.org
Tue Sep 20 07:53:29 UTC 2011


Looks like you could use:

$ find /home/fay/ -path '/home/fay/[^\.]*~' -exec rm '{}' \; -print

to do what you want.

For your other issue, you can probably do it cleverly with find, but in
the interests of just getting it done, I would just use backticks to
string a couple of commands together to do this:

ls -l `find . -name 'form' | grep -v 'dir1' | grep -v 'dir2'`

This will run the find command to get all 'form' files, then pipes that
output through two greps to remove dir1 & dir2; then the output of THAT
is sent to 'ls' in this case to show you all the files it has found.

Hope this helps!

Dominic

On Tue, 2011-09-20 at 00:37 +0100, Fay Zee wrote:
> Hi All,
> 
> I want to refine one of my pre-backup commands.
> 
> The following command deletes tilde files throughout my user 
> directory: 
> 
> $ find /home/fay/ -name '*~' -exec rm '{}' \; -print
> 
> But I want to keep the potentially valuable backup files in the
> hidden 
> dot directories or in the root of the user directory (I might need 
> those backups in case of corruption)
> 
> Examples of files that I want to exclude from the rm command are:
> 
> /home/fay/.bluefish/session~
> /home/fay/.screenrc~
> 
> So I want to delete everything starting with '/home/fay/' EXCEPT 
> anything starting with '/home/fay/.' so the character after the 
> third slash mustn't be a dot.
> 
> I'm not sure what to feed the -name argument. This option looks at 
> just a file name not its path name.
> 
> The first example file, named 'session~' doesn't begin with a dot but 
> it's in a directory that begins with a dot.
> 
> Looking at the man find pages I see I need to follow a shell pattern. 
> I looked at the entries for -name, -iname and -path but I'm no nearer.
> 
> 
> 
> On a different tack, although there isn't a whole directory tree I 
> need to ignore this time, so -prune doesn't apply, I'd like help on 
> this one too, please. In the interests of future searches, I would 
> like to know how to use the find command when I want to exclude, say, 
> two complete directories, "dir1" and "dir2" when searching for a 
> filename containing the string 'form'. Would I have to type the -prune
> once for each directory and what would the finished command look like?
> 
> 
> Best Regards,
> Fay
> East Grinstead Linux User Group
> www.eglug.org.uk
> 
> --
> Sussex mailing list
> Sussex at mailman.lug.org.uk
> E-mail Address: sussex at mailman.lug.org.uk
> Sussex LUG Website: http://www.sussex.lug.org.uk/
> https://mailman.lug.org.uk/mailman/listinfo/sussex





More information about the Sussex mailing list