Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find

Status
Not open for further replies.

maynarja

MIS
Jan 24, 2007
41
CA
This is most likely an easy one.

But i am writing a bash script to search for certain file extensions and then move the files to another directory. It works fine except that it searches the directory that the files are moved too.

find / -noleaf -iname "*.bak" -exec mv {} dump /;

I have tried the -path and -prune but it does not work for me or i have them in the worng places????


 
try this

Code:
find / -noleaf -iname "*.bak" | grep -v dump | xargs -i mv {} /dump/
 
Hmm, sbrew's suggestion is fine until you get 'dump.bak' Try
Code:
find / \
  \( -name dump -a -prune \) -o \
  \( -name proc -a -prune \) -o \
  -type f -iname "*.bak" -exec mv {} /dump/;
This is tested on AIX 5.1. As an added bonus I've avoided searching /proc as well.

BTW, is it a good idea to have the 'dump' directory in the root filesystem? Something like /var/dmp would make more sense to me.


Ceci n'est pas une signature
Columb Healy
 
I agree that "dump" should be in another location but this was just for testing.

BTW this is a SUSE 10.2 box

I have tried the command
Code:
find / \
  \( -name dump -a -prune \) -o \
  \( -name proc -a -prune \) -o \
  -type f -iname "*.bak" -exec mv {} /dump/;

and had to modify it to 

find / \ -noleaf
  \( -path /dump -a -prune \) -o \
  \( -path /proc -a -prune \) -o \
  -type f -iname "*.bak" -exec mv {} /dump/;

This does work fine. But is it not more effiecent to run xargs as the command with xargs is run once as -exec is ran once for every line?

Code:
find / \ -noleaf
  \( -path /dump -a -prune \) -o \
  \( -path /proc -a -prune \) -o \
  -type f -iname "*.bak" | xargs -i mv {} /dump/
 
Yep, the xargs version is probably more efficient, but, unless you have lots and lots of files, does it matter?

Incidentaly, I think your version of the code should be
Code:
find / -noleaf \
  \( -path /dump -a -prune \) -o \
  \( -path /proc -a -prune \) -o \
  -type f -iname "*.bak" | xargs -i mv {} /dump/

Ceci n'est pas une signature
Columb Healy
 
Your right....It helps to "Preview Post" before you "Submit Post".

;)

Thanks
 
Have you tested the xargs version ?
I doubt it does what you want ...
 
Yes it seemed to work and got the same result.

But i have decided to stay with "sbrews" way of doing it as i am not concerned about a dump.bak and such.

find / -noleaf -iname "*.bak" | grep -v dump | xargs -i mv {} /dump/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top