Filenames and directory names are merely strings, and we do a lot coding that has to work with them. Here are 2 of the most common and useful commands.
mediaPath="/Volumes/echo1/Media backup/Plex/Documentaries/Arctic Live [1of3] (2016)/Arctic Live [1of3] (2016).mp4" basename "$mediaPath" echo "Assign to a variable ..." fileMedia=$(basename "$mediaPath") echo "\$fileMedia: $fileMedia"
gives me
The companion to basename. It works with absolute and relative pathnames.
pwd dirPath="/Volumes/echo1/Media backup/Plex/Documentaries/Arctic Live [1of3] (2016)/Arctic Live [1of3] (2016).mp4" echo "\$dirPath: -->$dirPath<--" echo "Assign to a variable ..." dirVar=$(dirname "$dirPath") echo "\$dirVar: -->$dirVar<--" echo "Now cd to that directory ..." cd "$dirVar" pwd dirName=$(dirname "$dirVar") echo "\$dirName: -->$dirName<--"
This results in
Practice using these 2 commands - they will become familiar and more useful.
Sometimes we want to know details about a file that isn't readily available, but we would like to use it somehow in a script. Since we are mostly dealing with text (ASCII) files in scripting, what tools may be useful to us?
Here are a few that may find their way into your toolbox:
This one is a native command-line tool used on text (ASCII) files - word count, and used like this:
See SS64 for specifics.
Now you can get this information and put it into a bash variable, right?
Another native CLI application, tells us the file format of a file.
As you can see, all types of files (binary as well as text) have information that can be useful - if you can get at it. These tools may be just what you need.
We've discussed the use of ExifTool several times as an excellent non-native tool that can be used in bash (or any) scripting to pull metadata (ID3) tags from several digital cameras, as well as several audio and video file types.
If the task you are trying to do doesn't seem to have anything available natively, don't be afraid to look elsewhere, preferably at reliable sources.