tar all files to a directory - tar

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 \;

Related

How to append tar without full path

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

tar excluding parent directories and creating tarball on remote host

I have a host where an application creates model directories/files. I want to tar up those files and send it over to another host for DR backup.
So far I have the following:
sudo tar -zcvf - /mnt/nfsshared | ssh app#<ip-address> "cat > /data/models/models_$(date +\%Y-\%m-\%d).tar.gz" > /dev/null 2>&1
This command will tar up all directories and files in /mnt/nfsshared and create the tarball on the remote host under /data/models/
However, I want to exclude the the /mnt/nfsshared/ parent directories and just tar up the directories/files in the /nfsshared directory.
I know I could cd to /mnt/nfshared and tar up all files.
I can also do this command
sudo -C /mnt/nfsshared -zcvf | ssh app#<ip-address> "cat > /data/models/models_$(date +\%Y-\%m-\%d).tar.gz" > /dev/null 2>&1
which would only tar up directories/files under nfsshared. However, what argument would I pass to the command: -zcvf
I can't use a tarball name since the tarball gets created on the remote host.
Within /mnt/nfsshared/ I have multiple directories containing additional subdirectories and files.
/mnt/nfsshared/model1/files
/model2/files
/model3/submodel1/files
/submodel2/files
/submodel3/files
/model4/submodel1/files
I want only to tar up the model, submodel directories and the files I want to exclude the /mnt/nfsshared directories
Cheers,
Roland

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

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