#!/bin/bash
DIR=/home/user/files
for FILE in $DIR/*.jpg
do
F=${FILE%.jpg}
tar -cf $F.tar $FILE
gzip $F.tar
mv -f $DIR/$F.tar.gz /home/user/pictures/
done
Excuse me for asking, but what is the point of putting a single .jpg file into a tar archive? Als why gzip this archive that only contains this one, already compressed file (.jpg) ?
Just cd to the directory first and then tar without the pathnames. Alternatively, as there is no advantage to putting a single file into a tar archive, you might also just gzip the file without tar.
#!/bin/bash
DIR=/home/user/files
cd $DIR
ls *.jpg >/dev/null 2>&1
if [ $? == 0 ]; then
for FILE in *.jpg
do
F=${FILE%.jpg}
tar -cf $F.tar $FILE
gzip $F.tar
mv -f $F.tar.gz /home/user/pictures/
done
else
echo "No jpg files in $DIR"
fi