Bash Script - "Tar Czf ..." Command Ignore Parameters

Shell command to tar directory excluding certain files/folders

You can have multiple exclude options for tar so

$ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .

etc will work. Make sure to put --exclude before the source and destination items.

linux bash script: how to execute command with variable as parameter

The better solution is to use an array:

        exclude=(--exclude "aenlever/*")
fi
tar -czf /backups/sites/$siteonly.tar.gz ${dirs[i]} --exclude "tmp/*" --exclude "temp/*" --exclude "cache/*" "${exclude[@]}"

Also I think you need to reset the variable for every loop, but that depends on your intention.

for((i=1;i<=${#dirs[@]};i++))
do
exclude=()

And I would suggest this simplified format as a whole:

#!/bin/bash

dirs=(/home/test/*)

# Verify that they are directories. Remove those that aren't.
for i in "${!dirs[@]}"; do
[[ ! -d ${dirs[i]} ]] && unset 'dirs[i]'
done

echo "There are ${#dirs[@]} dirs in the current path."

for d in "${dirs[@]}"; do
exclude=()
siteonly=${d##*/}
[[ $siteonly == choubijoux ]] && exclude=(--exclude "aenlever/*")
tar -czf "/backups/sites/$siteonly.tar.gz" "$d" --exclude "tmp/*" --exclude "temp/*" --exclude "cache/*" "${exclude[@]}"
done

Cannot use bash variable for tar's exclude

The best way to store arguments that may contain spaces is in an array. Each argument can be a separate array entry, with or without spaces.

$ excludes=(--exclude='./do-not-backup')
$ printf '%q\n' "${excludes[@]}"
--exclude=./do-not-backup
$ tar -czf backup.tgz "${excludes[@]}" ./

The single quotes here are optional, exactly as they would be if you typed the --exclude argument normally. As you can see, Bash parses the quotes when the array is created and they're not actually stored in the array value.

Bash: Get command output with variables inside the command

FOLDERS='folder1 folder2 folder3'

BUPATH='/opt/mypath'

CONFIG_BACKUP="/storage/backup.tar.gz"

The expected command should be this:

tar -C "/opt/mypath" --exclude="/opt/mypath/folder1" --exclude="/opt/mypath/folder2" --exclude="/opt/mypath/folder3" -czf "/storage/config.tar.gz" .

Well, that's simple. I like the xargs printf to transform a list, ex. prepend something:

# creates a bash array from newline separated output
IFS=$'\n' tarexcludes=($(<<<"$FOLDERS" xargs -n1 printf "--exclude=%s/%s\n" "$BUPATH"))
echo tar -C "/opt/mypath" "${tarexcludes[@]}" -czf "$CONFIG_BACKUP" .

But alternatively you could pick a unique sed command separator and:

tarexcludes=($(<<<"$FOLDERS" tr ' ' '\n' | sed "s~^~--exclude=$BUPATH/~"))

Or use awk:

tarexcludes=($(<<<"$FOLDERS" tr ' ' '\n' | awk -v BUPATH="$BUPATH" '{print "--exclude=" BUPATH "/" $0}')

Notes:

  • Don't use backticks `. They are discouraged and I don't like them. bash obsolete and deprecated syntax
  • The <<<"$FOLDERS" is bash's here string
  • The tr ' ' '\n' substitutes spaces for newline. So it is parsable with sed.
  • The tarexcludes=( ... ) creates a bash array. bashguide arrays
  • You command does not work because echo $FOLDERS will output on a single line, and sed parses lines. So just replace spaces with newlines. Alternatively, use xrandr -n1 or printf "%s" $FOLDERS
  • By convention, upper case variable names are by convention reserved to environmentally exported variables. Use lowercase variable names in your scripts.

How do I turn off the output from tar commands on Unix?

Just drop the option v.

-v is for verbose. If you don't use it then it won't display:

tar -zxf tmp.tar.gz -C ~/tmp1


Related Topics



Leave a reply



Submit