Extract tar file after copy without success - linux

Im copy tar file form folder A to folder B and I want that when
it finish to copy it to unzip it, I try with the following in my shell script which doesnt work,any idea?
cp /home/i557956/A/file.tar /home/i557956/B
tar -xvf /home/i557956/B/file.tar
The copy was success but the tar is not extracted in B folder...

Try to move into the B folder before extracting:
cp /home/i557956/A/file.tar /home/i557956/B
(cd /home/i557956/B/ && tar -xvf file.tar)

Related

how to create tar file so that files at placed at root folder of tar.gz file

I need to create targ.gz file and contents of file should be at root folder
say. I want angle and Simple files at root of tarfile shown at bottom. Any suggestions. I tried as below but extra folder is created at root
tar -zcvf tarfile.tar.gz -C /example/tarFile .
./
./angle.txt
./Simple.jar
tar -zcvf tarfile.tar.gz -C /example/tarFile .
./angle.txt
./Simple.jar
Please try this
tar -czvf tarfile.tar.gz /example/tarFile
after creating tar file use cp or mv command to move anywhere you want.

how to tar all files in a directory and move that tar to another directory

So I have 2 directors A and B. I want to tar all the files in A and have the .tar file be sent to directory B. How can i do this?
I have tried
sudo tar -C /home/mine/A/ -cvf home/mine/B/test.tar
tar: Cowardly refusing to create an empty archive
tar -cvf /home/mine/B/test.tar /home/mine/A/
works fine for me.

tar extract all files from folder inside archive

Let's say I have a tar archive with the following structure:
"NAME/FOLDER/FILES"
e.g.
tarfolder.tar.gz/test123/[file1,file2,filex]
Now I wan't to extract all files from the folder test123 without extracting the "test123" folder itself. What's the command to do so?
Try:
tar -zxfv tarfolder.tar.gz test123/ --strip-components=1
Works for me.
Use the tar zxvf option, like this:
tar -zxvf tarfolder.tar.gz test123/file1

Tarring files exclude the path before the folder i want to tar

Im trying to tar a set of subfolders and then tar its parent folder afterwards via a ruby script.
The structure is as follows:
/x/y/z/ParentFolder/Subfolder1
/x/y/z/ParentFolder/Subfolder2
/x/y/z/ParentFolder/Subfolder3
/x/y/z/ParentFolder/Subfolder4
So what i want to end up with is Subfolder1.tar.gz,Subfolder2.tar.gz,Subfolder3.tar.gz,Subfolder4.tar.gz all contained in ParentFolder.tar.gz.
My problem at the moment is that im able to tar the parent folder with its subfolders but it structure remains as /x/y/z/ParentFolder/SubFolder1----4
tarParentFolder = "tar -zcvf /x/y/z/ParentFolder.tar.gz /x/y/z/ParentFolder 2>/dev/null"
`#{tarParentFolder}`
I have searched around but cannot seem to find a solution to this,
Anybody got any ideas?
Thanks
The answer to how to get the path to be relative to the right path is to use the -C tar option. That's a capital C. The parameter you pass is the directory from which you want the tar to start relative.
so you would do:
tar -zcvf /x/y/z/ParentFolder.tar.gz -C /x/y/z ParentFolder
But ... you should also probably think twice about putting tars in tars. You should be fine just tarring up the containing dir.
For creating tar archives containing multiple files/folders use this:
$ mkdir f1 f2
$ tar -czf tar.tgz f1 f2 # creates the tar
$ tar -tzf tar.tgz # lists tar contents
f1/
f2/
f3/
$
So you should write something like:
tar -zcvf /x/y/z/ParentFolder.tar.gz /x/y/z/Subfolder{1,2,3,4}

linux shell tar unwanted extra directories

I have the following problem:
I have directorties a/b/c and inside c many text files.
I want to make a .tar.gz file in drectory a/mydir with the c directory inside and then unzip it to that same directory to create a/mydir/c (with all the files inside)
I am at directory a and run: (shell)
~:$ tar -czf mydir/output.tar.gz b/c
~:$ tar -zxf mydir/output.tar.gz -c mydir
but the result is directories a/mydir/b/c (with the files inside)
The problem is I don't want directory b in the middle, just c with all its contents
This works for me. Create data
mkdir -p a/b/c
echo 42 > a/b/c/file.dat
Archive
tar zc -f c.tar.gz -C a/b c
created a/b/c directories, from directory a kindly try this command.
so the file under b/c/files were done out.tar.gz
new directory "mydir" create under "b" and files extracted too.
out.tar.gz removed from "a".
# tar -cvzf out.tar.gz b/c/* ; mkdir -p b/mydir ; tar -xvzf out.tar.gz -C b/mydir/ ; rm -rf out.tar.gz
Thanks!

Resources