Home > Software design >  delete slash from variable
delete slash from variable

Time:10-16

With help, i have this script but i am not sure how i get rid of "./" from the variable so i can zip the folder. Please can you advise me.

thanks Nick

#/bin/sh
BASEDIR=/tmp/

cd $BASEDIR
find . -type d | sort > newfiles.txt
DIFF=$(comm -13 oldfiles.txt newfiles.txt)
echo $DIFF
if [ "$DIFF" != "" ]
then
    #echo "A new dir is found"
   tar -czvf "$DIFF".tar.gz --verbose
fi

mv newfiles.txt oldfiles.txt

failed output:

  tar -czvf ./file2.tar.gz --verbose
tar: Cowardly refusing to create an empty archive
Try `tar --help' or `tar --usage' for more information.

CodePudding user response:

The error stems from this command:

tar -czvf "$DIFF".tar.gz --verbose

You specify the archive name, but not the files/folders to be added to said archive.

You would need it to change to:

tar -czvf "$DIFF".tar.gz "$DIFF"

Note that you don't need --verbose since you already specified -v with -czvf.


However, your code won't work like intended if DIFF contains multiple items (i.e. lines). You probably want something like this:

#/bin/sh
BASEDIR="/tmp/"

cd "$BASEDIR"
find . -type d | sort > newfiles.txt
DIFF=$(comm -13 oldfiles.txt newfiles.txt)
echo "$DIFF"
IFS=$'\n'
for item in $DIFF
do
    #echo "A new dir is found"
   tar -czvf "$item.tar.gz" "$item"
done

mv newfiles.txt oldfiles.txt

That being said, if you want to remove ./ from find output, you can use:

find . -type d -printf "%P\n" | sort > newfiles.txt

but you don't actually have to do this for your script to work.

  • Related