tar works on cmd but not work on bat file - windows

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.

Related

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

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.

What happens when you try to move a file to a non-existant location MAC?

On executing the following lines via the terminal on a MAC,
mv terraform2 ~/bin
cd ~/bin
I get the error that '/Users/myname/bin: Not a directory`. However, I can't see the file terraform2 in its original location. Where did it go?
Given
mv terraform2 ~/bin
If ~/bin doesn't exist before you run that command, your file terraform2 will be renamed to a file called ~/bin.
Thus, when you try
cd ~/bin
you get
/Users/myname/bin: Not a directory
because it's a file - your original terraform2 file.
The command
mv terraform2 ~/bin/.
is much better when you're trying to mv a file into a directory.

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 -zxvf cannot unzip file

here's the problem:
First step
transfer the *.gz file to the remote host using ftp, the code below
open $IP
user nfc nfc123
bin
passive
cd /nfc/APPBAK
put $FULLNAME $DESTFILE
cd $DESTDIR
tar -zxvf $local_filename
quit
FTPIT
Second step
tar -zxvf $local_filename
but it says:
"?Invalid command. "
Should I change the mode of of the *.gz file first, any help will be appreciated.
You are trying to run the tar command inside FTP, as far as I can see, rather than in the shell after you've fetched the file with FTP. It is confusing since some shell commands, like cd, seem to work in FTP too, but the cd command actually attempts to change directory on the remote machine (you need lcd to change directory on the local machine).
Put simply, tar isn't a valid FTP command, which is why you get the ?Invalid command error.
try this one::
tar -xvf $local_filename
Please make sure that file has right permissions.

How can I compress a directory, and convert soft to hard links?

I would like to compress a directory.
tar -cvzf mydir.tar.gz mydir
but this retains symlinks so that it is not portable to a new system.
How can I convert symlinks?
I have tried
tar -cvzfh
since man tar says
-h, --dereference
don’t dump symlinks; dump the files they point to
but this results in an error
tar: Error exit delayed from previous errors
and creates a file called "zh"
My files are on a RHEL server.
Your tar.gz file name must follow immediately after the -f flag, merely reordering the flags may work.
tar -cvzhf mydir.tar.gz mydir

Resources