TAR operation not working with variable in filename - shell

# Current date
now=`date +%Y-%m-%d:%H:%M`
# Compress folder
tar czf "$now.tar.gz" dump/
does not work. No tar is created. But
tar czf someName.tar.gz dump/
works fine. Can someone point out the problem? It seems that
tar czf "$now.tar.gz" dump/
is not accepted as filename. Any ideas?

Found the problem:
If the archive file name includes a colon (‘:’), then it is assumed to
be a file on another machine[...]
which lays here
now=`date +%Y-%m-%d:%H:%M`
I replaced the : with - and it works fine. More info here: http://www.gnu.org/software/tar/manual/html_section/tar_46.html#file

Related

edit a text file in an uncompressed .tar

I have created some .tar folders that for the most part contain some text files. Is is possible to edit programmatically a text file from the command line (bash and related tools) without fully un-packing the tar?
Context: these .tars were created by a script and I realized I made a mistake. I'm looking for the most efficient and simple solution to edit a part of a single line of a text file.
You could use tar --delete and tar --update to replace a file inside a tar
i think that you won't be able do it with out descompressing. You could to do a loop for descompress only your text file, edit with sed and compress again. In other case, i think that it is impossible...
maybe something like this
xzcat blah | sed /../../ | xz blah
within the loop that runs through all the files
Thanx for the inspiration Grammy, here is a minimal example in case someone comes looking for the same.
Create some data in a folder:
cd /tmp;
mkdir dir1;
echo "foo" >> ./dir1/test_txt;
echo "bar" >> ./dir1/test_txt;
Pack in a tar:
tar -cf test_tar.tar dir1;
Delete the directory that we are going to decompress and edit.
rm -rf dir1;
And unpack only the relevant file from the tar folder and edit:
WARNING:
Tar updates only if the timestamp changed since the last edit!
This is why there is a sleep command here (to change the timestamp.).
sleep 1
tar -xf test_tar.tar dir1/test_txt
sed -i 's/foo/baar/' dir1/test_txt ;
Update the relevant file.
tar -uf test_tar.tar dir1/test_txt
Check if this worked:
tar -xf test_tar.tar dir1
cat ./dir1/test_txt
[...]$
baar
bar

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

tar/tar.bz2 a file from a different directory

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).

Searching a file inside a .tar.gz file by date in unix

I would like to ask if there is a way to search for a file inside a .tar.gz file without extracting it? If there is, is there a way to search for that file by date?
My OS is AIX.
Thanks!
tar can be instructed to preserve atimes on files it archives, but not all tars do this, and I am unfortunately not familiar with AIX-specific tar in this case. What you need to know is whether tar was invoked with --atime-preserve (AIX tar may not support this; be sure to check), and when you call an extraction you must use the -p flag. So, you'd have something like this:
tar zxpf file.tar.gz the/file/you/want.txt
You will likely find that Unix (cf Linux) tar won't support the -j and -z so you would have to use:
gzip -dc file.tar.gz | tar xf - the/file/you/want.txt
to run the command from a pipe. In this case, you would need to know the name of the file you want extracted, which you can get from:
tar tf file.tar.gz
using compression as required. Obviously you can tack on a | grep foo if you are looking for a file named foo.
It is not, I do not think, possible to extract a file from tar based upon the modification date of the file in the tarball – at least I was not able to find support for such in the documentation. Remember, tar is just the tape archiver and is not meant to do such fancy things. :-)
Lastly, you can do this:
tar xvf file.tar `tar tf file.tar | grep foo`
if you want to pull out all the files matching 'foo' from file.tar (compression above yada yada). I do not suggest running that command on an actual tape drive!
$ tar tzf archive.tar.gz | grep "search"

Bash create .tar.gz on Solaris

I'm writting a bash script, which should create a .tar.gz archive from specified directory including file structure. This is a homework and I need it to work on Solaris system my school uses. That means I can't use tar like this
tar xvzf archive.tar.gz ./
because this system uses some older version of tar without -z option. I use it like this
tar xvf - ./ | gzip > archive.tar.gz
It works fine except a strange fact, that when I examine contents of the archive, it contains itself (in other words, it contains "archive.tar.gz"). How can I prevent this?
This works:
tar cvf - | gzip > archive.tar.gz
Thing is, that file named "archive.tar.gz" is created immediately when you run your command. Meaning, before gzip is called. It's just blank file, but it is in directory. To prevent including it into resulting archive, you can try to modify your script in one of following ways:
tar xvf - ./ | gzip > ../archive.tar.gz
tar xvf - {path_to_dir_you_want_to_compress_files_from} | gzip > archive.tar.gz
Sadly, I can't check if either of this scripts works, because I don't have Solaris anywhere. Please, let me know if any of that works.

Resources