Well also be doing more to confirm our code is more copy/paste friendly.
His code is available at onhis GitHub page.
Lets take a look at what he uses to pull apart the filename.
He uses the pipe command to take that output and use it as input for the next part.
After the pipe, Myles calls on the awk command, which is a powerful pattern scanning program.
In this case, thats a period.
Now, awk see a file named tastyfile.mp3 as being composed of two fields: tastyfile and mp3.
The file extension is automatically added in because of the variable that references Line 2.
Note that there is no reference to $2, a second argument for the script.
This particular script will copy said file into your current directory instead.
Great job Samuel and Myles!
I found out why: many modern Linux distros create a special file in the users home directory .profile.
So, that mystery is cleared up.
Today, well be discussing for loops.
Create a new script in your ~/bin/ folder entitled loopscript.
Pretty simple, right?
Lets see what happens if we change things up a little bit.
Now, lets change the echo command into something more useful say, the zip command.
Namely, well add files into an archive.
And, lets gets some arguments in the mix!
for i in $@; do
zip archive “$i”
Theres something new!
$@ is a shortcut for $1 $2 $3 … $n.
In other words, its the full list of all arguments you specified.
Now, watch what happens when I fire off the script with several input files.
it’s possible for you to see which files are in my folder.
I ran the command with six arguments, and each file was added to a zipped archive named archive.zip.
For loops are pretty wonderful.
Now you’re free to execute batch functions on lists of files.
Using for-loops makes it easy to do a bunch of actions for all files in a directory.