Bash scripting: my script deletes a working folder prematurely. How do i fix?

Solution 1:

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