I am trying to write a shell scripts that will back up every file in a directory. In oder to get the files I use
FILENAMES="$(find -maxdepth 1 -not -path './.*' -type f)"
which returns something containing all the names of the files.
If none of the filenames contain spaces then using
tar --exclude='archive.tar.bz2' -cvpjf archive.tar.bz2 $FILENAMES
does the job and all is fine.
However if there is a file names "bad filename.txt" the above approach does not work and tar will complain that it cannot find "bad" of "filename.txt".
I tried to fix this by using an inline replacement as described here, but that does not work:
TOARCHIVE=${FILENAMES// /'\ '}
Basically what this does is to replace the spaces in the appropriate position, IF I echo
it. But tar
does not seem to see the inserted '\'
. If try the replacement code with two slashes, such as '\\'
or '\\ '
tar
will see both slashes and again complain.
Another solution to similar problems I saw was to make sure everything is in quotes, but that does not work for me since TOARCHIVE
contains the names of multiple files.