# Ordering the expansion in a documents name

Just how do I get the documents expansion from bash? Below's what I attempted:

filename=basename $filepath fileext=${filename##*.}


By doing that I can get expansion of bz2 from the course /dir/subdir/file.bz2, yet I have a trouble with the course /dir/subdir/file-1.0.tar.bz2.

I would certainly favor a remedy making use of just bash without exterior programs if it is feasible.

To make my inquiry clear, I was developing a bash manuscript to extract any kind of offered archive simply by a solitary command of extract path_to_file. Just how to extract the documents is established by the manuscript by seeing its compression or archiving type, that can be.tar.gz,. gz,. bz2 and so on. I assume this need to entail string adjustment, as an example if I get the expansion .gz after that I need to examine whether it has the string .tar prior to .gz-- if so, the expansion needs to be .tar.gz.

0
2019-05-04 05:26:42
Source Share

You could streamline issues by simply doing pattern matching on the filename as opposed to removing the expansion two times:

case "$filename" in *.tar.bz2) bunzip_then_untar ;; *.bz2) bunzip_only ;; *.tar.gz) untar_with -z ;; *.tgz) untar_with -z ;; *.gz) gunzip_only ;; *.zip) unzip ;; *.7z) do something ;; *) do nothing ;; esac  0 2019-12-01 16:14:44 Source $ echo "thisfile.txt"|awk -F . '{print $NF}'  . Talk about this below : http://liquidat.wordpress.com/2007/09/29/short-tip-get-file-extension-in-shell-script/ 0 2019-05-07 23:13:37 Source echo${filename#$(echo$filename | sed 's/\.[^[:digit:]].*$//g;')}  . As an example : % echo$filename
2.6.35-zen2.patch.lzma
% echo ${filename#$(echo $filename | sed 's/\.[^[:digit:]].*$//g;')}
.patch.lzma

.
0
2019-05-07 23:03:36
Source

i addressed it utilizing this :

filename=basename $filepath fileext=${filename##*.}
fileext2=${filename%.*} fileext3=${fileext2##*.}
if [ "$fileext3" == "tar" ]; then fileext="tar."$fileext
fi


yet this only benefit well-known archiving type, in this instance just tar

0
2019-05-07 23:02:47
Source

If the documents name is file-1.0.tar.bz2, the expansion is bz2. The method you're making use of to extract the expansion (fileext=${filename##*.} ) is flawlessly legitimate ¹ . Just how do you determine that you desire the expansion to be tar.bz2 and also not bz2 or 0.tar.bz2? You require to address this inquiry first. After that you can identify what shell command matches your requirements. • One feasible requirements is that expansions have to begin with a letter. This heuristic falls short for a couple of usual expansions like 7z, which could be ideal dealt with as a grandfather clause. Below's a bash/ksh/zsh execution : basename=$filename; fileext=
while [[ $basename = ?*.* && (${basename##*.} = [A-Za-z]* || ${basename##*.} = 7z ) ]]; do fileext=${basename##*.}.$fileext basename=${basename%.*}
done
fileext=${fileext%.}  For POSIX transportability, you require to make use of a case declaration for pattern matching. while case$basename in
?*.*) case ${basename##*.} in [A-Za-z]*|7z) true;; *) false;; esac;; *) false;; esac do …  • Another feasible requirements is that some expansions represent encodings and also show that more removing is required. Below's a bash/ksh/zsh execution (calling for shopt -s extglob under bash and also setopt ksh_glob under zsh ) : basename=$filename
fileext=
while [[ $basename = ?*[email protected](bz2|gz|lzma) ]]; do fileext=${basename##*.}.$fileext basename=${basename%.*}
done
if [[ $basename = ?*.* ]]; then fileext=${basename##*.}.$fileext basename=${basename%.*}
fi
fileext=${fileext%.}  Keep in mind that this takes into consideration 0 to be an expansion in file-1.0.gz. ¹ ${VARIABLE##SUFFIX} and also relevant constructs remain in POSIX, so they operate in any kind of non-antique Bourne-style shell such as ash, bash, ksh or zsh.

0
2019-05-07 21:16:35
Source