Extracting a Bunch of .gz Files in One Go using Bash’s For Loop While Keeping Originals

While compressing a set of single files is fairly easy (one only needs to add ‘.gz’ or similar to the end of “file name strings”) the oposite is not so easy. Nevertheless it can be done doing:

for i in *.gz; do echo extracting $i ...; sudo su -c "gzip -dc $i > ${i:0:${#i}-3}"; done && ls *xpm

sudo su -c "" is for Ubuntu systems, ${#i}-3 sais “length of i minus 3” which makes ${i:0:${#i}-3} to become each file name without trailing .gz.


  • Bash by Example
  • man 8 bash section “Parameter Expansion” -> “Substring Expansion”