1

I have a bunch of directories like 001/ 002/ 003/ mixed in with others that have letters in their names. I just want to grab all the directories with numeric names and move them into another directory.

I try this:

file */ | grep ^[0-9]*/ | xargs -I{} mv {} newdir

The matching part works, but it ends up moving everything to the newdir...

Rorschach
  • 31,301
  • 5
  • 78
  • 129

3 Answers3

3

I am not sure I understood correctly but here is at least something to help.

Use a combination of find and xargs to manipulate lists of files.

find -maxdepth 1 -regex './[0-9]*' -print0 | xargs -0 -I'{}' mv "{}" "newdir/{}"

Using -print0 and -0 and quoting the replacement symbol {} make your script more robust. It will handle most situations where non-printable chars are presents. This basically says it passes the lines using a \0 char delimiter instead of a \n.

Lynch
  • 9,174
  • 2
  • 23
  • 34
  • @6pool Use `echo` to help you debug complex `xargs` commands. like this `... | xargs echo mv ...` – Lynch Mar 11 '14 at 08:26
0

mv is not powerfull enough by itself. It cannot work on patterns.

Try this approach: Rename multiple files by replacing a particular pattern in the filenames using a shell script

Either use a loop or a rename command.

Community
  • 1
  • 1
Piotr Zierhoffer
  • 5,005
  • 1
  • 38
  • 59
0

With loop and array,

Your script would be something like this:

#!/bin/bash
DIR=( $(file */ | grep ^[0-9]*/ | awk -F/ '{print $1}') ) 

for dir in "${DIR[@]}"; do
   mv $dir /path/to/DIRECTORY
done
MLSC
  • 5,872
  • 8
  • 55
  • 89