tar/tar.bz2 a file from a different directory - bash

Lets say I am currently in the folder
/home/user1234
Next, there is a file
/home/user1234/files/mylog.log
From the /home/user1234 folder I would like to tar.bz2 mylog.log . What I have done was run the command
tar cjf /home/user1234/files/mylog.log.tar.bz2 /home/user1234/files/mylog.log
This works however in the tar.bz2 file the compressed file is under its fully qualified path. I would like the compressed file to be in the 'root' of the tar.bz2. Can anyone point me in the right direction? I've been looking at the -C option you can pass to tar but I can't seem to get it to work.

Try:
tar cjf /home/user1234/files/mylog.log.tar.bz2 -C /home/user1234/files/ mylog.log
The -C essentially "changes" to that directory before compressing/decompressing the list of files (denoted by mylog.log).

Related

tar one folder to an other directory

I'm trying to create a bash script that creates an archive of one folder (or the folder content) to a specific directory.
My version works but instead to archive one folder it archives me the whole path which I don't want.
Is there a way to solve this problem without using cd? I saw some solutions using -C but I get the following error, no matter where I place it: refusing to create an empty archive
SRCDIR=~/Documents/sub1/sub2/sub3/source/*
DESTDIR=~/Documents/sub1/sub2/sub3/target/backup.tgz
tar czf $DESTDIR --absolute-names $SRCDIR
You can try this:
SRCDIR=~/Documents/sub1/sub2/sub3/source/*
DESTDIR=~/Documents/sub1/sub2/sub3/target/backup.tgz
tar czf $DESTDIR --absolute-names -C $(dirname $SRCDIR) $(basename $SRCDIR)
As you can see in my example I'm using -C option to change the directory in combination with dirname and basename for source directory
OTHER OPTIONS
-C, --directory DIR
change to directory DIR
Usually, when the '-C' is being used, the wild-card expansion does not work well - as it will try to expand the wildcard ('*' in this case) at the current directory where the command is invoked, and NOT at the SRCDIR location.
If you want all files to be included in the repository, use the '.' (it will also include hidden files!).
SRCDIR=~/Documents/sub1/sub2/sub3/source/*
DESTDIR=~/Documents/sub1/sub2/sub3/target/backup.tgz
tar czf $DESTDIR -C "$(dirname $SRCDIR)" .
As a side note, the original post is using '--absolute-names'. You might want to reconsider using this flag, as it make it extremely difficult to restore the files into any other location but the original location of the files.

How to extract tgz file "with" a folder

In OS X, double click X.tgz will generate a folder namedX, putting all files into the folder (the folder name follows the .tgz file's name).
I want to do the same thing in the terminal.
However, tar -xvzf /pathtofile/X.tgz will extract all files and put them in /pathtofile/.
And by tar -xvzf /pathtofile/X.tgz -C /newpath/ the newpath does not automatically follows the .tgz file name(i.e. in this case a folder named "X").
Does anyone know if there is a simple way to do this?
Use the strip-component flag in tar:
tar --strip-components=1
This bash function does what you asked to do, which is basically just creating the directory using the filename without the ".tgz"
extract_to_dir () {
filename=$1
dirname="${filename%.*}"
mkdir ./$dirname
tar -xvzf $filename -C $dirname
}
Usage would be:
extract_to_dir filename.tgz

How to GUNZIP and UNTAR a file in one directory and move to another directory in UNIX

I have a file named 2014-03-19_cis_digital.tar.gz. in a source directory, i will have to first GUNZIP the file and then UNTAR the file and move the untarred files to another directory.
Can anyone help me in writing the shell script commands??
change your working directory first then untar/ungzip.
cd $TARGET_DIR
tar xzf $PATH_TO_FILE
You don't need to gunzip separately. You can do everything in one command:
tar -xzf /source/dir/2014-03-19_cis_digital.tar.gz -C /target/dir

Finding differences between tarred folder and ordinary folder

I want to find the differences between tar folder and an ordinary folder. So I used "tar d folder2.tar folder" command in UNIX command line, but i didn't get any output. I couldn't even get the bash command prompt. How do i use "tar -d" command to find the differences between a tarred folder and an ordinary folder having files?
It's tar df folder2.tar folder. You have to add the f flag so it doesn't try to read the tar from stdin.

Automated unzipping of files

I have a folder full of zipped files (about 200). I would like to transform this into a folder consisting only of unzipped files. What would be the easiest and quickest way to do this?
Please note that I would like to remove the zipped file from the folder once it us unzipped.
Also, I'm on a Mac.
Thanks!
You can do something like:
for file in `ls *.zip`; do unzip -f $file; rm $file; done
We are looping through all the zip files in the directory, unzipping it and then deleting it.
Note that the -f option of zip will overwrite any file without prompting if it finds a duplicate.
You need to run the above one-line command on the command line from the directory that has the all the zip files. That one line is equivalent to:
for file in `ls *.zip` # ls *.zip gets the list of all zip file..iterate through that list one by one.
do # for each file in the list do the following:
unzip -f $file # unzip the file.
rm $file # delete it.
done
I found this answer which is a simple one liner to gunzip all .gz compressed files within a folder.
Basically you cd to the folder and then run
gunzip *.gz
If you want to only unzip files with a certain prefix you put that before the *
gunzip example*.gz
Easy as cake!

Resources