Mandark
Mandark

Reputation: 828

Using xargs command and two move commands following it

OS: aix shell: bsh

Hi, ppl

I have two types of files, one type ends with .pdf.marker and the other ends with .pdf

There should be always a pair with the same name (only the extensions are different).

When I move a .pdf.marker file I must also move its corresponding .pdf file.

I tried something like this:

find ${INPUT_LOCATION}/ -name "*.pdf.marker" | xargs -I file mv file ${OUTPUT_LOCATION}/. mv $(basename file .marker) ${OUTPUT_LOCATION}/. 

Then I read this: xargs with multiple commands as argument and tried something like this:

find ${INPUT_LOCATION}/ -name "*.pdf.marker" | xargs -I file {mv file ${OUTPUT_LOCATION}/.; mv $(basename file .marker) ${OUTPUT_LOCATION}/.;} 

but it still didnt work.

I just need to execute 2 commands after xargs.

EDIT

Following the proposed answers I got i tried to put just 2 parameters into one move command instead of two separate move commands following xargs.

find ${INPUT_LOCATION}/ -name "*.pdf.marker" | xargs -I file mv file $(basename file .marker) ${OUTPUT_LOCATION}/. 

But now, the .pdf.marker is moved first, then when I try to remove the .marker from the filename to get the .pdf filename i get a warning no such file or directory.

Is there another way to get .pdf filename string?

SOLUTION

find ${INPUT_LOCATION} -name '*.pdf.marker' -exec sh -c 'mv $0 `dirname $0`/`basename $0 .marker` $1' {} ${OUTPUT_LOCATION} \;

Moved 200 000 files in cca. 25 min. without problems.

Thanks everyone who participated with their answers and a big thanks goes to you Nahuel Fouilleul!

Upvotes: 1

Views: 1253

Answers (5)

Ole Tange
Ole Tange

Reputation: 33685

If you have GNU Parallel installed:

find ${INPUT_LOCATION} -name '*.pdf.marker' | parallel mv {} {.} ${OUTPUT_LOCATION}

To learn more watch the intro videos: http://pi.dk/1

Upvotes: 0

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

try this (x option for debug)

find "${INPUT_LOCATION}" -name '*.pdf.marker' | xargs -i bash -cx 'pdf=`dirname {}`/`basename {} .marker`;[ -e "$pdf" ]&&{ mv {} "$pdf" "$0";}' "${OUTPUT_LOCATION}"

or shorter

find $INPUT_LOCATION -name '*.pdf.marker' | xargs -i bash -c 'mv ${0%.marker} $0 $1' {} $OUTPUT_LOCATION

or

find $INPUT_LOCATION -name '*.pdf.marker' -exec bash -c 'mv ${0%.marker} $0 $1' {} $OUTPUT_LOCATION \;

maybe more standard

find ${INPUT_LOCATION} -name '*.pdf.marker' -exec sh -c 'mv $0 `dirname $0`/`basename $0 .marker` $1' {} ${OUTPUT_LOCATION} \;

and for tests echo can be added

find ${INPUT_LOCATION} -name '*.pdf.marker' -exec sh -c 'echo mv $0 `dirname $0`/`basename $0 .marker` $1' {} ${OUTPUT_LOCATION} \;

Upvotes: 2

glenn jackman
glenn jackman

Reputation: 246807

for f in *.pdf.marker; do
  if [ -e "${f%.marker}" ]; then
    mv "$f" "${f%.marker}" "$OUTPUT_LOCATION"
  fi
done

Upvotes: 0

Ortwin Angermeier
Ortwin Angermeier

Reputation: 6183

You could try something like the following:

find ${INPUT}/ -name "*.pdf" -exec mv '{}' '{}'.marker ${OUTPUT} \;

To test if the marker file exists you could use something like the following:

find ${INPUT}/ -name "*.pdf" -exec test -e '{}'.marker \; -exec mv '{}' '{}'.marker ${OUTPUT} \;

Upvotes: 3

Jenny D
Jenny D

Reputation: 1245

No need to use xargs:

for i in `find ${INPUT_LOCATION} -name \*.pdf`; do mv $i ${OUTPUT_LOCATION}; mv $i.marker ${OUTPUT_LOCATION}; done

Upvotes: 1

Related Questions