LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Newbie needing help with tar command (https://www.linuxquestions.org/questions/linux-newbie-8/newbie-needing-help-with-tar-command-4175503706/)

jkpalmer 05-02-2014 09:28 AM

Newbie needing help with tar command
 
I have a tar file named foldera-files.tar that contains files from foldera/files that was created using:
tar -czvf foldera-files.tar foldera/files/

I want to extract the contents from this tar file to an empty folder named folderb/files

So I tried this command

tar -C /folderb/files -xvf foldera-files.tar

It shows the files being extracted, but the contents of folderb/files is still empty.

Help greatly appreciated.

rtmistler 05-02-2014 09:43 AM

It probably extracted the files at the point you were at when you ran that command. Is there a new foldera/files tree at the point where you ran that tar command? If so you should be able to move the file tree to where you want it to be.

But that put some question in my mind as to why this didn't work, or what would've worked correctly for you. I'm not too experienced at using the -C argument, but have done so. Therefore a quick test, in case your question has more to do with tar versus the fact that you didn't get the files where you wanted them.
Code:

mkdir temp
cd temp
echo 1 > 1.txt
echo 1 > 2.txt
cd ..
tar -cvf test.tar temp/*
temp/1.txt
temp/2.txt
mkdir temp2
tar -C temp2 test.tar
temp/1.txt
temp/2.txt
ls -R temp2
temp2:
temp

temp2/temp:
1.txt  2.txt

So I feel this does work; however one has to be aware that the structure of the top level, including that top level is to be considered when you tar and extract. What I feel would've worked cleaner in this example is if I say was "in" temp and executed the tar command, specifying that the tar file be "../test.tar" so as to not include the tar file in my wildcard search in creating the tar. This way the top-most directory level would not have been included and I'd solely see 1.txt and 2.txt.

So I'm curious then why you didn't see foldera embedded within folderb. I hope it wasn't as simple as you hadn't created folderb before you performed your extraction?

jkpalmer 05-02-2014 12:22 PM

To explain my environment a little more as well as my results:

I was using putty to login to my server.

There are two directories beneath the "root" of my hosting service
foldera
|---files
folderb
|---files

I was at the "root" directory of my hosting service when I executed my tar extract command.

I did not see any of the files that I knew were in my tar get extracted to either my "root" directory or the folderb > files directory.

My TRUE intent is to copy the contents of the files directory beneath foldera to the files directory beneath folderb, but I'm having trouble getting the cp command to work.

...Jim P.

rtmistler 05-02-2014 12:32 PM

Have you tried?
Code:

cp -rf foldera folderb

rtmistler 05-02-2014 12:37 PM

Let's say you have something weird and cp isn't working properly, such as an oddball Busybox implementation and/or a cp which doesn't support the -r option. To do this with tar, I would do the following:
Code:

cd foldera
tar -cvf ../files.tar *
cd ../folderb
tar -xvf ../files.tar

This would leave the top level directory out of that picture. Honestly though I'm very interested what troubles you're running into with cp. There are also options to follow symbolic links; I think for both tar and cp if that is some of the problems.

Note further that say the hierarchy under foldera is all your stuff and you do have symbolic links, it's probably better to have those links not be fully qualified paths, but rather referenced based paths. Say the link points to ../../file versus /<path>/<path>/<path>/file. I find that helps if I create symbolic links and cp a whole directory tree, including those.

szboardstretcher 05-02-2014 01:51 PM

Oneliner for same:

Code:

tar -cf - * | ( cd ../folderb; tar -xf -)
Or to use the old faithful cpio:

Code:

find . -print -depth | cpio -pdm ../folderb

Firerat 05-02-2014 04:28 PM

Off the top of my head your

Code:

tar -C /folderb/files -xf foldera-files.tar
Should work..
Potential pitfalls:
  • /folderb/files does not exist
    Try mkdir -p /folderb/files before the tar
  • the foldera-files.tar doesn't exist , full path?

However, should ethier of those be true sone error must have been presented.

And.. it dosn't fit with post #3

maybe permissions?

You would need read for foldera and write for b


cp should work, but I do find myself using rsync more, especially when dealing with many files

so you get no errors with tar or your original cp?

Seems odd

Edit, try cp with the -v flag
It does nothing special, just prints what it tries

jkpalmer 05-03-2014 06:32 AM

Sorry, I realized that I didn't describe what I was wanting to do, correctly.

I had previously stated:

foldera
|---files (I should have called this subfoldera)
|--data1.ext
|--data1.ext
folderb
|---files (I should have called this subfolderb)

The references to "files" above are both folders.

Using my new terminology, I'm trying to get the contents of subfolders to subfolderb.

Still not getting data1.ext and data2.ext in folderb>subfolderb

More help is requested.

Thanks to all who have provided feedback and I appreciate everyone's patience.

Regards,

Jim P.

Firerat 05-03-2014 04:31 PM

Checkout.
Code:

cp --help
.
And
Code:

man cp
I think this will work

Code:

cp -a /path/to/foldera/* /path/to/folderb/
Assumes folderb exists

-a is short for a bunch of stuff, see the --help and manpage for details

throw in a -v to see stuff

If you want progress ..
Code:

rsync -a --progress /path/to/foldera/* /path/to/folderb/
Same folderb exists assumption

rsync is probably overkill for what you need, I like to use it for large or/and lots of files... mostly for the feedback, if I knew total size I can guesstimate how long I have to wait

Firerat 05-03-2014 04:33 PM

Actually, my previous post is mostly redundant, just reiterated rtmistler's post #4

Shadow_7 05-04-2014 12:09 AM

Quote:

Originally Posted by jkpalmer (Post 5163310)
tar -czvf foldera-files.tar foldera/files/

tar -C /folderb/files -xvf foldera-files.tar

Well, -c is create -z is compress with gzip so technically the file should be .tar.gz, not that extensions matter much in linux. You should also include (or exclude) the "z" component in BOTH commands. With -x being extract.

Also your inclusion list is ONLY foldera/files/, since there's no wildcards, you will probably grab the directory and not the contents of that directory. Having foldera/files* will likely better suit your intentions? YMMV.


All times are GMT -5. The time now is 08:22 PM.