Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.6: Create a new Service to 'tar' directories or files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Create a new Service to 'tar' directories or files
Authored by: FiXato on Jan 06, '12 04:04:28PM

Very nice services script! It was a useful start for me!

I would recommend changing the 'h' argument to 'H' though. With 'h', you could end up trying to tar your entire harddisk if a folder contains a symlink to the root of your harddisk.
Lowercase 'h' traverses all symlinks encountered and includes the actual files in the archive.
Capital 'H' will only follow the symlink passed on the commandline, in other words: if the folder you try to archive is actually a symlink. It will retain the original symbolic links as links though.
(I discovered this problem when I tried to archive an .app that basically was a Wine wrapper. Since Wine usually contains symlinks to your homedir, it tried archiving more than I intended).

I also added 2 environment variables to my version that should prevent archiving of Mac Resource Fork files, such as .DS_Store.
COPYFILE_DISABLE=true is the current env var for it, while COPY_EXTENDED_ATTRIBUTES_DISABLE=true is a legacy env var that used to do the same thing. Included both for portability.

I also added automatic timestamping, because it helps keeping track of backups. Since the timestamp makes it rather unique, I guess the whole $count part of the script could've been removed, but I've kept it for safety anyway.

First my version of the TAR script:

export COPYFILE_DISABLE=true
export COPY_EXTENDED_ATTRIBUTES_DISABLE=true
Tarfile="$1-$(date +%Y%m%d_%H%M%S).tar"
count=1
cd "${@%/*}"
if [ $# -eq 1 ]; then 
  while [ -e "$Tarfile" ]
  do
    let count++
    Tarfile="$1-$(date +%Y%m%d_%H%M%S)-$count.tar"
  done
else 
  Tarfile="Archive-$(date +%Y%m%d_%H%M%S).tar"
  while [ -e "$Tarfile" ]
  do
    let count++
    Tarfile="Archive-$(date +%Y%m%d_%H%M%S)-$count.tar"
  done
fi
/usr/bin/tar -cHf "$Tarfile" "${@##*/}"

And since another commenter asked about tar.gz, here is my TGZ version too:

export COPYFILE_DISABLE=true
export COPY_EXTENDED_ATTRIBUTES_DISABLE=true
Tarfile="$1-$(date +%Y%m%d_%H%M%S).tgz"
count=1
cd "${@%/*}"
if [ $# -eq 1 ]; then 
  while [ -e "$Tarfile" ]
  do
    let count++
    Tarfile="$1-$(date +%Y%m%d_%H%M%S)-$count.tgz"
  done
else 
  Tarfile="Archive-$(date +%Y%m%d_%H%M%S).tgz"
  while [ -e "$Tarfile" ]
  do
    let count++
    Tarfile="Archive-$(date +%Y%m%d_%H%M%S)-$count.tgz"
  done
fi
/usr/bin/tar -cHzf "$Tarfile" "${@##*/}"

And for completeness-sake, one using bzip2 compression: the TBZ version:

export COPYFILE_DISABLE=true
export COPY_EXTENDED_ATTRIBUTES_DISABLE=true
Tarfile="$1-$(date +%Y%m%d_%H%M%S).tbz"
count=1
cd "${@%/*}"
if [ $# -eq 1 ]; then 
  while [ -e "$Tarfile" ]
  do
    let count++
    Tarfile="$1-$(date +%Y%m%d_%H%M%S)-$count.tbz"
  done
else 
  Tarfile="Archive-$(date +%Y%m%d_%H%M%S).tbz"
  while [ -e "$Tarfile" ]
  do
    let count++
    Tarfile="Archive-$(date +%Y%m%d_%H%M%S)-$count.tbz"
  done
fi
/usr/bin/tar -cHjf "$Tarfile" "${@##*/}"



[ Reply to This | # ]