miriam_e: from my drawing MoonGirl (Default)
[personal profile] miriam_e
The Linux shell can use the commands dirname and basename to separate out the name of a file from the path to it. If an extension is given with basename then you can get filename without the extension, however you need to know the extension ahead of time in order to specify it. If you want to strip the extension from a lot of files with many different extensions then this is of no use.

There is, however another, much better way. It is built into the bash command shell so no time and memory is wasted pulling in another command to do the job, and it is much more general-purpose, able to do far more than dirname and basename.

Here is a small script to show how it works. Seeing is much better than explaining:
#!/bin/sh

# get path, filename, basename, extension
# example: /mnt/drive/dir/file.tar.gz
pname="${1%/*}" ; # /mnt/drive/dir
fname="${1##*/}" ; # file.tar.gz
bname="${fname%%.*}" ; # file
b2name="${fname%.*}" ; # file.tar
ename="${fname##*.}" ; # gz
e2name="${fname#*.}" ; # tar.gz

echo "pname: $pname"
echo "fname: $fname"
echo "bname: $bname"
echo "b2name: $b2name"
echo "ename: $ename"
echo "e2name: $e2name"
Save the little script somewhere as someting like "testingstr". Make sure its permission bits are set to executable (chmod u+x testingstr) then run the script with a filename as its parameter, like this:
  ./testingstr /mnt/work/writing/back.tar.gz

The path and file name gets substituted into the script as "$1". The "%" tells to chop off from the end. The "#" tells to chop off from the beginning. A single "%" or "#" means to chop the shortest matching string. Doubled ("%%" or "##") means to match the longest possible string. The star ("*") is a wildcard; it stands in for any number of any characters. In the examples above, the dot or slash is the character being searched for to limit the string being chopped.

Read them like this:
${1%/*} in the parameter string ($1) chop the smallest possible string from the end that matches a slash followed by any other characters.

${1##*/} in the parameter string ($1) chop the longest possible string from the beginning that matches any characters followed by a slash.

${fname%%.*} in the previously computed string ($fname) chop the longest possible string from the end that matches a dot followed by any other characters.

You get the picture...

The bash shell can do many more string tricks too. For instance if you want to know the length of a string stored in a variable $x you can print it this way:
  echo "${#x}"

If you want to print a substring of a string variable from character 3 and going for 5 characters then use this form ${parameter:offset:length}:
  echo "${x:3:5}"

If you want to print from that point to the end of the string you don't need the length:
  echo "${x:3}"

You can do neat tricks using the length and substring to print the last n characters. For instance if you want the last single character then subtract one from the length and print the substring starting from that point onwards.
  x="blah"
  y=${#x}-1
  echo "${x:$y}"

prints the result:
  h


Or you can chop off the last number of characters from the string like this:
  x="gobble"
  y=${#x}-3
  echo "${x:0:$y}"

prints the result:
  gob


Lots of cool stuff... :)

Profile

miriam_e: from my drawing MoonGirl (Default)
miriam_e

December 2025

S M T W T F S
 123456
7 8 910 111213
1415 1617181920
21222324252627
28293031   

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 26th, 2025 03:49 am
Powered by Dreamwidth Studios