How to append tar without full path - tar

Is there a way to append a file into a specific dir inside a tar file?
I have dir1/TarFile.tar. Inside the TarFile.tar there is dir2.
I want to append file.txt (in dir 1) to TarFile.tar under dir2.
Thanks in advance.

As far as i know you can't do this on the fly. You can create directory dir2, move there the file file.txt and update the archive:
cd dir1
mkdir -p dir2
mv file.txt dir2/
tar uvf TarFile.tar dir2/file.txt

Related

How do I tar the certain files of a directory without retaining the directory structure?

I typically do :
tar czf $directory_path/file_${1}.tar.gz --directory=$specified_path/ *${1}.csv .
What if I just want to include only certain files of a directory, but not the directory itself?
cd [dir]; tar cz ./* >x.tgz
or
cd [dir]; tar cz file1 file2 >x.tgz

In KSH I'm trying to grep for string inside a file name and if there move to a different directory

I've got a script that takes all files with extension of .123 Some of them start with ABC others start with XYZ. I want to take all the files that have XYZ in the PREFIX and move them to a different directory.
For example, XYZothertext.123 and ABCothertext.123 both come in to a directory /test I want to evaluate if any file contains XYZ in the name and if so, move it to /TEMP instead of /test. AND delete from original folder.
if grep -q XYZ "$*.123"; then
mv *.ABC /TEMP
fi
This is probably terribly incorrect. But I'm learning.
mv XYZ*123 /TEMP
mv ABC*123 /test

Copy list of files with tar command. Bash

I have the directory A/a A/b where both a and b are files.
When I run these commands I get the outputs.
-> tar --include A -v -cf A.tar A
a A
a A/a
a A/b
-> tar --include A/a -v -cf A.tar A
I don`t understand in the 2nd evocation why file A/a is not archived. I believe I do not understand how include works.
I am trying to give tar a list and have it create an archive with the contents. Please help.
Thank you.

tar all files to a directory

The following will extract the files in /root/ directory. But it also creates the parent directories under root. What I need is that the files should be exactly under root folder and not in /root/data/mysql/...
# tar -xvf company_raw_2012-02-22.tgz --directory=/root/
data/mysql/company_raw/data_archive_r_20120222.MYD
data/mysql/company_raw/data_archive_r_20120222.MYI
data/mysql/company_raw/data_archive_r_20120222.frm
If that is not possible, how do I write a program to move these files to the required folder?
I have tried the following and it does work.
--strip-components=3
But I do not know how many folder will be there. So the number 3 may change.
Extract everything to the temp directory with full path and then just walk it moving files to the desired destination?
destdir=/root
tmpdir=/root/tmp
rm -rf $tmpdir
mkdir $tmpdir
tar xf archive.tar.gz -C $tmpdir
find -H $tmpdir -type f -exec mv '{}' $destdir \;

tar create rename directories

I have a directory named dir with files file1 file2 file3 in it and I want to create a gzipped tar directory from it called dir.tar.gz but with the hierarchy as:
anotherdir/file1
anotherdir/file2
anotherdir/file3
There's always the option of writing a shell script to rename dir to anotherdir then tar it, but I was hoping for a more elegant solution.
Use a symlink and dereference during tarring.
$ mkdir dir && touch dir/file{1,2,3} && ln -sf dir anotherdir
$ tar -hcvpf a.tar anotherdir
anotherdir/
anotherdir/file1
anotherdir/file2
anotherdir/file3
$ tar -tf a.tar
anotherdir/
anotherdir/file1
anotherdir/file2
anotherdir/file3
You can make a symlink and then force tar to follow it:
ln -s dir anotherdir
tar -czf dir.tar.gz -L anotherdir
Beware that will also follow any symlinks inside the tree.
The following seems to work fine. Tar will not follow links, but the resulting tar will have additional ./ for the files (which I think is not a big deal).
mkdir dir another_dir && touch dir/file{1,2,3} another_dir/another_file{1,2,3}
tmp_dir=`mktemp -d /tmp/dXXXX` && ln -s `pwd`/dir $tmp_dir/another_dir &&
tar -C $tmp_dir -cvf result.tar another_dir/. &&
rm $tmp_dir/another_dir && rmdir $tmp_dir

Resources