2

my script works for a single file perfectly, but if I process multiple files, it deletes my workingfolder too early, and only one file is processed. If I don't delete the workingfolder, the script works with multiple files. Here is my script:

#!/bin/bash
cd /storage/sort_tv/
mkdir workingfolder
for i in *.mp4;
  do name=`echo "$i" | cut -d'.' -f1`
  echo "$name"
sudo ffmpeg -i "$i" -map_metadata -1 -c:v copy -c:a copy -map 0:a -map 0:v "workingfolder/${i%.*}.mp4" &&
mv -f workingfolder/* /storage/sort_tv
rm -rf workingfolder
done

How can I get all files to be processed AND moved before the working folder is removed?

4
  • Why are you using sudo? And why rm -rf instead of rm -r?
    – terdon
    Aug 5 at 18:09
  • Just out of habit. For years I've always used rm -rf. I know its technically not correct.
    – Alfke
    Aug 6 at 15:27
  • 1
    It's not wrong, as such, just unnecessarily dangerous since you might be missing useful error messages or deleting write-protected files. The sudo is the one you should really avoid: that is creating files you don't own. And a generally good habit to get into is to never use sudo unless it is necessary.
    – terdon
    Aug 6 at 15:30
  • Technically not correct is not the issue. if you have mounts/ hard links (not sure about the latter, but I know for sure that hard links do traverse blocks) on a dual boot system, you can say toodle-oo to your windows partition. This is what rm -rf will do for you. I highly doubt that is what you were shooting for. And that is just from a couple of weeks ago. It just happened to be in my history.
    – Nate T
    Aug 11 at 16:18
4

You can try moving the rm commands out of the loop. Like this:

#!/bin/bash
cd /storage/sort_tv/
mkdir workingfolder
for i in *.mp4;
  do name=`echo "$i" | cut -d'.' -f1`
  echo "$name"
sudo ffmpeg -i "$i" -map_metadata -1 -c:v copy -c:a copy -map 0:a -map 0:v "workingfolder/${i%.*}.mp4" &&
done
mv -f workingfolder/* /storage/sort_tv
rm -rf workingfolder
3
  • Thank you so much. I got it working!
    – Alfke
    Aug 5 at 17:32
  • Welcome to Ask Ubuntu, @Alfke! If one of the answers here solved your issue, please take a moment and accept it by clicking on the checkmark on the left. That is the best way to express your thanks on the Stack Exchange sites.
    – terdon
    Aug 5 at 18:10
  • You're welcome. You can mark the question solved so others wont waste time on it. Have a nice day Aug 5 at 18:25

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.