Bash Script Automation for new Subdirectories.
I'm producing game-related videos, and I've made 2 scripts to make my work much easier. Lets call them Script A and Script B.
Script A run several copies of Script B (one in each subdirectory). And every time I have new directories, I manually copy Script B into them, and then edit Script A so that the pathways for the new Script Bs are correct. Sooo, I'd like help in remaking Script A so that it doesn't need editing each time new subdirectories are added. Right now Script A looks something like this. Code:
#!/bin/bash It's a simple script that just changes directory, run Script-B.sh in that directory, then goes to the parent directory and up the next subdirectory and runs Script-B.sh in that directory, and so on. Worth mentioning, the actual directories have more unique names, and each time I run the script the amount of subdirectories varies. So what I need for my new Script A, is that it copies Script-B.sh into all the current subdirectories(I normally do this manually), then executes Script-B.sh in each of these subdirectories. I'd also like help with Script B, right now, it outputs a standardised name as "Final-Video.mov" in the subdirectory the script is ran from. I'd really appreciate it if I could end the script with something like Code:
mv Final-Video.mov ../$1.mov |
As far as all of your subdirectories, you can use a for loop, and loop through all file (*), and then do what you need to do. As far as "script B," what exactly does it do? Maybe you can share it with us, and possible find a completely better way to tackle what you are trying to accomplish.
Cheers, Josh |
Quote:
Quote:
Code:
find tree-containing-directories -mindepth 1 -depth -type d -print0 | xargs -r0 path-to-Script-C Code:
find tree-containing-directories -mindepth 1 -depth -type d -print0 | bash -c 'while read -d "" DIR ; do path-to-Script-C "$DIR" & ; done ; wait' The basic structure of Script C could be something like the following: Code:
#!/bin/bash If you change a directory name while the script is still working, it will abort. You can then safely rerun the command. If the final movie already exists, the script will not regenerate it, just move (and rename) it. |
Quote:
Code:
#!/bin/bash So, when script B is done... the Subdirectory is still there, and it contains the original videofiles + Final-Video.mov. So, if I have like 10 subdirectories with videos, I will have 10 videos with the name Final-Video.mov (one in each Subdirectory). The name of the Subdirectory is descriptive of what the video inside it, so thats why I'd like to name "Final-Video.mov" after the directory it's in. |
Quote:
I found that "basename `pwd`" didn't handle spaces too well. Code:
filer@nellie:~/Video Edit/custom$ basename `pwd` Code:
find tree-containing-directories -mindepth 1 -depth -type d -print0 | xargs -r0 path-to-Script-C |
Quote:
Code:
basename "`pwd`" Or you may prefer Code:
basename "$( pwd )" |
Instead of basename+pwd, you can use bash's already supplied PWD variable and parameter expansion.
Code:
path="${PWD%/*}" |
Nominal Animal, you are bash script crazy!
Tried your solution and it worked. Thank you very much! Also, thank you all for taking your time to assist me. I'll mark this thread as solved, but I'm still curious of how the PWD thing works and how variables work, and what the correct syntax is between the two. Would this be correct Code:
$mydir=basename "`pwd`" |
Quote:
Let's go step by step, starting with basename. And as an example we'll even use a filename with, not one, but three spaces in it. Code:
$ basename /home/david/test/my file.txt So we quote the filename: Code:
$ basename "/home/david/test/my file.txt" Now, we want to create a variable to store that value in. First of all, the syntax for setting a variable is very simply name=value. You don't put a $ in front of the name when setting it, only when retrieving the value. And there are no spaces around the equals sign either. Let's try to set it directly first: Code:
$ name=my file.txt Code:
$ name="my file.txt" In this case we insert the output of basename into the variable setting. Code:
$ name="$( basename "/home/david/test/my file.txt" )" (Actually in this specific situation quoting isn't needed, as substitutions get copied over without word-breaking, but it doesn't hurt, and they are necessary elsewhere). Now let's echo back the variable to check its value. Code:
$ echo $name Code:
$ echo "$name" In fact, let's do something funky and try passing the output of basename directly to echo. Code:
$ echo $( basename "/home/david/test/my file.txt" ) So as you can see, it's vital to understand how the shell handles arguments and whitespace. Read and absorb these three links. http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/WordSplitting http://mywiki.wooledge.org/Quotes And don't forget the suggestion in my last post. $PWD is a variable automatically set by the shell that holds the full path to the current directory, in the same format as the pwd command. Code:
$ pwd Code:
$ name="/home/david/test/my file.txt" |
All times are GMT -5. The time now is 03:28 AM. |