linux zip and exclude dir via bash/shell script
Because backslash quotings in a variable after word splitting are not evaluated.
If you have a='123\4'
, echo $a
would give
123\4
But if you do it directly like echo 123\4
, you'd get
1234
Clearly the arguments you pass with the variable and without the variables are different.
You probably just meant to not quote your argument with backslash:
ignorelist="$ignorelist $SYNC_WEB_ROOT_BACKUP_DIR/$ignoredir/***"
Btw, what actual works is a non-evaluated glob pattern:
zip -r 12-08-2014_072810.website.zip sync_test5 -x 'sync_test5/dir_to_ignore/***' 'sync_test5/dir2_to_ignore/***'
You can verify this with
echo zip -r 12-08-2014_072810.website.zip sync_test5 -x sync_test5/dir_to_ignore/**\* sync_test5/dir2_to_ignore/**\*
And this is my suggestion:
#!/bin/bash
SYNC_WEB_ROOT_BASE_DIR="/home/www-data/public_html"
SYNC_WEB_ROOT_BACKUP_DIR="sync_test5"
SYNC_WEB_ROOT_IGNORE_DIR=("dir_to_ignore" "dir2_to_ignore")
IGNORE_LIST=()
if [[ -n $SYNC_WEB_ROOT_IGNORE_DIR ]]; then
for IGNORE_DIR in "${SYNC_WEB_ROOT_IGNORE_DIR[@]}"; do
IGNORE_LIST+=("$SYNC_WEB_ROOT_BACKUP_DIR/$IGNORE_DIR/***") ## "$SYNC_WEB_ROOT_BACKUP_DIR/$IGNORE_DIR/*" perhaps is enough?
done
fi
FILE="$SYNC_BACKUP_DIR/$DATETIMENOW.website.zip" ## Where is $SYNC_BACKUP_DIR set?
cd "$SYNC_WEB_ROOT_BASE_DIR";
zip -r "$FILE" "$SYNC_WEB_ROOT_BACKUP_DIR" -x "${IGNORE_LIST[@]}" >/dev/null
echo "Done"
bash shell script: Zip folder and all the content under not any directories above
change working directory to folder which contains ZIP_THIS_FOLDER
first
cd ~/Desktop
zip -r ZIP_THIS_FOLDER ZIP_THIS_FOLDER
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.
Zip with ignore within Bash script
The zip-man-page says
Note that currently the trailing / is needed for directories (as in
zip -r foo . -i dir/
to include directory dir).
So, likewise exclusion seems to work: Replacing the marked line in your script by
IGNORE_LIST+=("$DIRECTORY_TO_COMPRESS/$IGNORE_DIR/")
should do the trick.
(my zip is version 3.0 on Ubuntu-Linux)
How to zip files of a directory excluding absolute path in bash?
Within your script, you should be able to do something like this:
cd /student
zip -r class1.zip class1/
If you have several of these to do under /student
you could:
cd /student
for class in class* ; do
zip -r $class.zip $class
done
Create zip file and ignore directory structure
You can use -j
.
-j
--junk-paths
Store just the name of a saved file (junk the path), and do not
store directory names. By default, zip will store the full path
(relative to the current directory).
Zip - How do you exclude files from a zip using a database black-list?
With Zip 3.0 and Ubuntu:
zip -r -x@exclude.db archive.zip /home/user/directory/
Example exclude.db
, if the files are located in subdirectories:
*/exclude1.txt
*/exclude2.jpg
*/exclude3.db
How to use 'cp' command to exclude a specific directory?
rsync
is fast and easy:
rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude
You can use --exclude
multiples times.
rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude
Note that the dir thefoldertoexclude
after --exclude
option is relative to the sourcefolder
, i.e., sourcefolder/thefoldertoexclude
.
Also you can add -n
for dry run to see what will be copied before performing real operation, and if everything is ok, remove -n
from command line.
Related Topics
How Does Gdb Start an Assembly Compiled Program and Step One Line at a Time
Is Ethernet Checksum Exposed via Af_Packet
How to Print the Nth (5Th) Line of Every File Preceded by the Filename Using Any Linux Tool
Sed: Matching on 2 Patterns on the Same Line
Average of Multiple Files Without Considering Missing Values
Will Process Lost Wake-Up Chance in a Preemptive Kernel
Ssh Connection to Ubuntu Open Ssh-Server Requires Login on (Physical) Server via Password First
Jupyter Lab - Suppress Console Output
Executable Object Files and Virtual Memory
Start Docker-Compose Automatically on Ec2 Startup
How to Set Firefox Binary Path of Firefox in Selenium in Linux
How to Delete the Matching Pattern from Given Occurrence
How to Print Current Time in Kernel
Error When Installing Opencv on Ubuntu 14.04
Dockerfile Cmd 'Command Not Found'
Preventing to Bash Script from Running in Parallel or Overlap Using Cron
How Can a Program Detect If It Is Running as a Systemd Daemon