Output A tar archive into a specific path in a bash script - bash

I have a part in my script that uses tar to archive some folders. It should output the archived result to a specified folder.
The following tar command outputs the file to the right folder but makes the resulted archive nested with the full path leading to it.
e.g.
Inside my tar file I have the following folders:
full/path/to/file
The folder structure shouldn't look like that it should be relative to the parent folder not the root folder.
Here is the code:
...
local PROJECTS=(~/path/to/folder/*)
...
local PROJECT_PATH="${PROJECTS[$i]}"
local BACKUP_NAME=`date +%d%b%y`.tar.gz
echo Making folder "${PROJECT_PATH}"/backups
tar -czvf $PROJECT_PATH/backups/$BACKUP_NAME $PROJECT_PATH --exclude="${PROJECT_PATH}"/node_modules --exclude="${PROJECT_PATH}"/backups

If you want tar to save paths relative to some directory, use -C to change to that directory and provide relative paths:
tar -czvf "$PROJECT_PATH/backups/$BACKUP_NAME" -C "$PROJECT_PATH" . --exclude=./node_modules --exclude=./backups
-C "$PROJECT_PATH" tells tar to change to the $PROJECT_PATH directory, and the following . tells it to archive its current directory.

Related

Name not matched in creating zip folder with bash

I'm trying to create a zip folder in a bash script that contains the current date (YYYY_MM_DD).
I have this code:
currentArchive=$(date '+%Y_%m_%d')
zip -r ./aktuell ./Archive/${currentArchive}-bkt
But when I run the script I get the following error:
zip warning: name not matched: ./Archive/2017_03_30-bkt
I want the folder "aktuell" as a zip folder named "2017_03_30-bkt.zip" in the folder "Archive". The current folder "aktuell" exists.
What am I doing wrong?
You have the source and destination directories backwards in your zip command. Also, make sure the destination directory exists before executing zip.
currentArchive=$(date '+%Y_%m_%d')
mkdir -p ./Archive/
zip -r ./Archive/${currentArchive}-bkt ./aktuell

Zip folder created by shell script not opening

I am using the below script to zip the folder and it contents :-
cd /home/fs/Inbnd/
pwd
tar -cvf Image_test_new.zip Image
chmod 777 *
chown fusionc:staff *
The file image.zip is getting created successfully. But the file is not opening and showing an error :
Is there an error in the ststement I am using to zip?
tar -cvf makes a tar ball, not a zip archive. You can verify this in Linux, before trying to open it in Windows.
touch not_going_to_be_a_zip
tar -cvf not_really_a.zip not_going_to_be_a_zip
unzip not_really_a.zip
Archive: not_really_a.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of not_really_a.zip or
not_really_a.zip.zip, and cannot find not_really_a.zip.ZIP, period.
The zip utility does a good job at making zip archives.
touch will_be_a_zip
zip i_am_a.zip will_be_a_zip Archive: i_am_a.zip
testing: will_be_a_zip OK
No errors detected in compressed data of i_am_a.zip.
unzip -t i_am_a.zip
Archive: i_am_a.zip
testing: will_be_a_zip OK
No errors detected in compressed data of i_am_a.zip.
Note: unzip -t will test the archive only, make sure its okay before trying it in Windows.
If you cannot use the standard approach like zip/unzip and if you have JDK installed on your machine then you can use the jar utility from JDK's bin folder.
To zip a file
jar cvf zip_file_name.zip image.jpg
The only overhead is that it will add a META-INF folder with a file named MANIFEST.MF in it, which you can delete after extracting the zip file.
Try to use zip command instead of tar command.

tar works on cmd but not work on bat file

Windows tar command works on cmder.
tar -zxvf D:\backup\a.tar.gz
But when I add it to bat file, it doesn't work. I tried these versions
call tar -zxvf D:\backup\a.tar.gz
tar -zxvf D:\backup\a.tar.gz
call tar -zxvf ./a.tar.gz
tar -zxvf ./a.tar.gz
no one worked and I get an error
'tar' is not recognized as an internal or external command,
operable program or batch file.
call tar from the directory
e.g. tar is in the same folder as your batch file
"%~dp0tar" -zxvf "D:\backup\a.tar.gz"
otherwise i suggest you use the full path for tar within quotes.
"C:\users\yourname\Desktop\tar" -zxvf "D:\backup\a.tar.gz"
I had the same problem. My issue was caused because I created a variable called path in my batch file which overwrote the windows path variable which contains the directory where tar is stored. Maybe you did the same thing.
This worked for me. First set the current path to the directory were the zip file is located, then issue the tar on the file.
cd /d "F:/some/path/to/zip" & tar -zf <file_name>.zip
Use relative paths or if you need absolute paths then use forward slash / instead of backslash \ (e.g. D:/backup/a.tar.gz)
It doesn't take anyone a second to try it, and it is simply the solution that worked for me after many trials.

tar extracted archive removes version

I have a zipped archive version 0.0.1: myarch_0.0.1.tar.gz
When I extract it with tar, everything is unzipped and extracted in a myarch folder, stripping the version number.
ls
myarch_0.0.1.tar.gz
tar -zxvf myarch_0.0.1.tar.gz
ls
myarch/ myarch_0.0.1.tar.gz*
I want the extracted folder to be named: myarch_0.0.1/
How do I keep my version number stuck to the extracted folder name?
The name of an archive file, and the name of the files inside, have nothing to do with each other in general. If you want extracted directories to have a certain name, with a version number, then you have to create the archive with so named directories.
In this example, the extracted content is a directory named myarch, instead of your desired myarch_0.0.1. You can rename the directory and recreate the archive:
mv myarch myarch_0.0.1
tar zcf myarch_0.0.1.tar.gz myarch_0.0.1
That's it. When you untar this new archive, you will get a directory named myarch_0.0.1, simply because that's what you put inside. Even if you rename this file to mickeymouse.tar.gz, when you untar it, you will still get a directory named myarch_0.0.1, simply because that's what's inside the archive. Nothing to do with the filename of the archive.

Tar Directory Contents Without Creating A Root Folder In Archive

I am running the following snippet in a bash script in a folder. This creates an archive where there is no root folder since I am running the script within the folder whose contents I am archiving.
tar -pczf $ARCHIVE_NAME --exclude=${ARCHIVE_NAME} --exclude=$(basename ${0}) *
Archive Contents:
/a/
/b/
/c/
/a_normal_file
works great, but since I am using the * it is not archiving hidden files in the immediate directory. I did some searching and found archiving (ubuntu tar) hidden directories I have then changed it to:
tar -pczf $ARCHIVE_NAME --exclude=${ARCHIVE_NAME} --exclude=$(basename ${0}) .
Archive Contents:
/./a/
/./b/
/./c/
/./.a_hidden_file
/./a_normal_file
still works, but now there is a root folder being created with the name being the '.' character. The contents are then within that. How do I get my previous result, but where it still archives hidden files in the immediate directory?
Thanks in advance!
Use -C, --directory DIR
--directory=$(dirname ${0})
Example:
$ ls -a xxx
. .. aaa .bbb
$ tar cvf test.tar --exclude test.tar -C xxx .
./
./.bbb
./aaa
While it doesn't affect the unpacking of the archive (since . just refers to the same dir), if it bothers you, you can change the wildcard from * to .[^.]* * to include all the hidden files as well.
In addition, if you have hidden files beginning with .., such as ..a, you'll need to add ..?* to the list as well.

Resources