Thursday, May 2, 2024
15
rated 0 times [  15] [ 0]  / answers: 1 / hits: 7363  / 2 Years ago, sun, june 12, 2022, 1:29:35

Many times I need to extract different kinds of archived files using commad-line. But not all the time I remember the exact command for any type of file archive. So, I have to waste time and search again. How can I avoid this?


More From » command-line

 Answers
2

You can use the following shell script (I named it extract and I put it in ~/bin):



#!/bin/bash

if [ $# -lt 1 ];then
echo "Usage: `basename $0` FILES"
exit 1
fi

# I found the following function at https://unix.stackexchange.com/a/168/37944
# which I improved it a little. Many thanks to sydo for this idea.
extract () {
for arg in $@ ; do
if [ -f $arg ] ; then
case $arg in
*.tar.bz2) tar xjf $arg ;;
*.tar.gz) tar xzf $arg ;;
*.bz2) bunzip2 $arg ;;
*.gz) gunzip $arg ;;
*.tar) tar xf $arg ;;
*.tbz2) tar xjf $arg ;;
*.tgz) tar xzf $arg ;;
*.zip) unzip $arg ;;
*.Z) uncompress $arg ;;
*.rar) rar x $arg ;; # 'rar' must to be installed
*.jar) jar -xvf $arg ;; # 'jdk' must to be installed
*) echo "'$arg' cannot be extracted via extract()" ;;
esac
else
echo "'$arg' is not a valid file"
fi
done
}

extract $@


Don't forget to make the script executable:



chmod +x ~/bin/extract


Usage:




extract file_1 file_2 ... file_n

[#29713] Monday, June 13, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anatta

Total Points: 326
Total Questions: 128
Total Answers: 96

Location: Jordan
Member since Sun, Jun 26, 2022
2 Years ago
anatta questions
Sun, Jul 17, 22, 07:13, 2 Years ago
Sun, Jun 6, 21, 12:17, 3 Years ago
Sat, Jun 12, 21, 20:43, 3 Years ago
Thu, Jan 13, 22, 20:49, 2 Years ago
Sat, Jun 5, 21, 05:39, 3 Years ago
;