Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Sunday, April 16, 2023

Convert XZ File to GZ File

I've been having some trouble with XZ files when modified through KDE's ark program. So I decided to convert them all to GZIP files. To do that one at a time, by hand, would take forever. Hello shell scripting.

#!/bin/bash

#DEBUG="echo";
CWD=$(pwd);

XZFILE="$1";
if [ -z "$XZFILE" ]; then
   echo "No file given!";
   exit 1;
fi

XZFILE=$(realpath "$XZFILE");
TARGETDIR=${XZFILE%/*};
GZFILE=${XZFILE##*/};
GZFILE="${GZFILE%xz}gz";
NEWDIR="/tmp/xz2gz.$$";

echo $XZFILE;
$DEBUG mkdir "$NEWDIR";
$DEBUG tar -Jxvf "$XZFILE" -C "$NEWDIR";
$DEBUG pushd "$NEWDIR";
$DEBUG tar -czf "$GZFILE" *;
$DEBUG mv "$GZFILE" "$TARGETDIR";
$DEBUG popd;
$DEBUG rm -Rf "$NEWDIR";


Tuesday, February 28, 2023

Bash: Looping Over Filenames With Spaces

 The common:

for F in *; do ...; done

will fail if the files contain whitespace in their names. At least for files with just spaces, this works:

find -iname GLOB | while read F; do
   ....
done

https://stackoverflow.com/questions/7039130/iterate-over-a-list-of-files-with-spaces

 


UPDATE: It appears that newer versions of bash solve this problem and do what expect it to do:

#> for i in h*; do echo $i; done
ho ho