2

I have a folder A with many small files. In the parent folder there are many folders with other files in them. now in folder A there are files that have the same name as in the other folders, but they additionaly have .xmp at the end.

Thats the structure now:

  • ~/A/foo.xmp
  • ~/A/bar.xmp
  • ~/folder1/foo
  • ~/folder2/bar

and that is how I want it to be:

  • ~/folder1/foo
  • ~/folder1/foo.xmp
  • ~/folder2/bar
  • ~/folder2/bar.xmp

How do I find the coresponding file to foo.xmp (the coresponding file is: foo) in the parent directory and move my file there?

How do I put this in a loop so it goes through all files in my current directory?

  • 1
    Bash is very trendy at this time of the year. A shame that google and man aren't... – user123418 Jul 18 '15 at 1:44
1
for dir in folder*/
do
    for f in "$dir"*
    do
        base=${f#$dir}
        [ -f "A/$base.xmp" ] && mv "A/$base.xmp" "$dir"
    done
done
0

Something like this can be done:

#Assuming there are N folders folder1,folder2...folderN
for folder in folder* 
do
cd $folder
for file in *
do
#if file with .xmp extension exists in ~/A folder
[[ -f $file ]] && [[ -f ~/A/$file.xmp ]] && mv ~/A/$file.xmp .
done
cd ..
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.