for file in *.7z; do
directory=${file/%.7z/}
if mkdir ${directory}; then
( cd ${directory}; 7z x ../${file} )
else
echo "Unable to create directory ${directory} for archive ${file}" 1>&2
fi
done
The way this works is first iterating through all the *.7z
files using the file
variable. Once it grabs a filename, it uses bash string manipulation to strip the .7z
off the end, and make a new variable, directory
, out of that.
It will then try to create the directory, and, if it can, it will open up a subshell, go into that directory, and extract the 7zip archive.
If it was not able to create that directory, a message is sent to standard error.
I use a subshell so as to not have to worry about losing track of where I am with changes to the working directory.
The most abstruse part of this is this line:
directory=${file/%.7z/}
This is the aforementioned bash string manipulation, which will look for .7z
at the end of a string, and if present, replace it with nothing. If I wanted to replace it with, say, .zip
, I could use ${file/%.7z/.zip}
.
@platinums Pretty hard to use context menus on a non-graphical operating system. – Moses – 2017-07-23T16:15:25.360