That's not necessarily an invertible mapping (what if the original file is named high_scores
for instance? is that encoded specially, e.g., with double underscore as high__scores.gz
?) but if you just want to take a name and translate _
to /
and remove .gz
at the end, sed
will do it:
for name in /backup/*.gz; do
newname=$(echo "$name" |
sed -e 's,^/backup/,,' \
-e 's,_,/,g' \
-e 's/\.gz$//')
echo "zcat $name > $newname"
done
Make sure it works right (the above is completely untested!) then take out the echo, leaving:
zcat "$name" > "$newname"
(the quotes protect against white space in the names).