Score:2

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

in flag

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?

terdon avatar
cn flag
Why are you using `sudo`? And why `rm -rf` instead of `rm -r`?
Alfke avatar
in flag
Just out of habit. For years I've always used rm -rf. I know its technically not correct.
terdon avatar
cn flag
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.
Nate T avatar
it flag
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](https://askubuntu.com/questions/1354555/i-just-deleted-everything-on-my-windows-system-through-the-ubuntu-subsystem-can/1354571#1354571) 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.
Score:4
in flag

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
Alfke avatar
in flag
Thank you so much. I got it working!
terdon avatar
cn flag
Welcome to Ask Ubuntu, @Alfke! If one of the answers here solved your issue, please take a moment and [accept it](//askubuntu.com/help/someone-answers) by clicking on the checkmark on the left. That is the best way to express your thanks on the Stack Exchange sites.
Muhammed Özen avatar
in flag
You're welcome. You can mark the question solved so others wont waste time on it. Have a nice day
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.