Solved

How to tar these dirs?

Posted on 2011-03-07
7
541 Views
Last Modified: 2012-05-11
Hi Experts,

Occasionally I want to backup these kinds of paths to a tar file:
- All files in my home dir (top level only).  I don't care if this includes dirs.
- All files in say ~/abc and it's subdirs, recursively.

How can I do this with a SINGLE tar command?

I know how to do it with find and tar, or 2 tar commands, but I'm wanting to know how I can do it more simply (i.e. a single tar only).

Points for positive working answers only.  If you don't think it can be done, please don't comment.

Thanks.
0
Comment
Question by:tel2
7 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 35054890
did you try this

tar -cvf mytar /dir1 /dir2 /dir3

I did not try tar with ~/dirname but I think it should work
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 35054913
Hi,

It is not possible to backup only the ifrst level without directories unless you've excluded them exclusively in the command line. However it is possible to backup several directories with a sigle tar command. So the command you're looking for is:

tar --exclude ./dir1 --exclude ./dir2 --exclude ./dir3 -cvzf /tmp/backup.tgz . /dir2

Cheers,
K.
0
 
LVL 17

Expert Comment

by:Sikhumbuzo Ntsada
ID: 35054979
tar -c -f archive.tar -C  / home (home being the top directory in question)

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:Maciej S
ID: 35057789
Simple answer: it's impossible with just one tar command (well, actually it is possible, if you are going to accept manually passing all file names in command line :))
As you wrote in your question - you need something more: another tar command or find.
0
 
LVL 12

Author Comment

by:tel2
ID: 35062601
Hi omarfarid,
The problem is, that will not limit the home dir to the top level only.  Have another read of my requirements, especially the 1st bullet point.

Hi KeremE,
That's been my understanding, too.

Hi Santasi24,
See my response to omarfarid, above.

Hi oklit,
That's been my understanding, too.

Hi All,
For months I've been using the --exclude and --no-recursion tar switches, but I've had to have 2 tar commands, (the 2nd appending the 1st tar file), then a bzip2 command to zip the combined tar file.  I was hoping that I could do it in a single (concise) command (tar -jcvf ...), but it seems not.

Despite the fact that I said "Points for positive working answers only.  If you don't think it can be done, please don't comment.", I may be willing to accept an answer which includes a single 'find' and a single 'tar' command, to backup what I have asked for.  Any takers?
0
 
LVL 23

Accepted Solution

by:
Maciej S earned 500 total points
ID: 35063306

tar cvf arch.tar $(find /your/home -maxdepth 1 -type f) /your/home/abc

Open in new window

0
 
LVL 12

Author Closing Comment

by:tel2
ID: 35063329
Thanks oklit!
Nice work.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now