سؤال

I have a directory "images" with nested structure, and there are .png files somewhere in it. I also have a plain directory "svg" with a bunch of .svg files in it. I need to move each .svg file into the same dir where lies the .png file with the same name.

this command for a single given .svg file works:

find /images -name 'sample.png' | grep -v thumb | xargs -i{} dirname {}|xargs -0 -I {}  mv /svg/sample.svg {}

grep -v thumb is applied because for each .png file there is the thumbnail file with the same name in other subdir named "thumb".

I tried to write this command for all the files:

find /svg/ -name "*.svg" -exec basename {} '.svg' \; | xargs -O -I {} "find /images/ -name {}"

but i recieve and error: `basename' terminated by signal 13

Moreover, if this second command works, the next step is to combine it with the first command, but here is another problem: how can i send given filename to the final command "mv" (see {???} in a code)?

find /svg/ -name "*.svg" -exec basename {} '.svg' \; | xargs -O -I {} "find /images/ -name {}" | grep -v thumb | xargs -i{} dirname {}|xargs -0 -I {}  mv /svg/{???} {}
هل كانت مفيدة؟

المحلول

This bash script should work. Sometimes a solution cannot be easily stuffed into a pipeline:

#! /bin/bash
for svg in svg/*.svg ; do
    name=${svg##*/}                                  # Remove the path.
    name=${name%.svg}                                # Remove the extension.
    png=$(find -name $name.png -not -path '*thumb*')
    dir=${png%$name.png}                             # Remove the filename.
    mv "$svg" "$dir"
done
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top