Solved

How to tar these dirs?

Posted on 2011-03-07
7
536 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 11

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 11

Author Closing Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

744 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

11 Experts available now in Live!

Get 1:1 Help Now