user217631
user217631

Reputation: 1308

moving files recurisively on linux

find ./dir -type f -iname "*.t[argz]*[bz2]" -print | xargs mv --target-directory=dir seems to fail on file that has spaces in the name. how to improve it? or alternative?

thanks for answer below: my mv doesn't support --null or -0, I'm using cygwin:

$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments t
.

Upvotes: 6

Views: 5607

Answers (3)

ghostdog74
ghostdog74

Reputation: 342363

GNU find

find ./dir -type f -iname "*.t[argz]*[bz2]" -exec mv "{}" /destination +;

Upvotes: 0

Sean Madden
Sean Madden

Reputation: 1099

Have you looked at the -exec option for find?

find ./dir -type f -iname "*.t[argz][bz2]" -exec mv {} --target-directory=dir ';' 

The -exec option will execute any options following it as a command until it sees the semi-colon wrapped in quotes. This way you won't have to deal with the spacing issue.

Upvotes: 0

martin clayton
martin clayton

Reputation: 78105

Use -print0 instead of -print on the find command, and the xargs -0 (or --null) option - then NULs will be used as separators rather than newlines and spaces.

find ./dir -type f -iname "*.t[argz]*[bz2]" -print0 | xargs --null mv --target-directory=dir

Upvotes: 11

Related Questions