LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-29-2007, 05:01 PM   #1
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
tar ONLY hidden files


I have been looking online for a bit and can't seem to find any information on this, so I thought I would ask.

It is easy enough to include hidden files in a tar archive, but what I want to do is have only the hidden files placed into the archive. I want to backup the configuration files from my /home directory, but not the ~30 GB of downloads/music/randomness that also lives there.

Doing:

Code:
tar cvf /tmp/backup.tar ~/.*
Does indeed start to get the hidden files/directories, but then for some reason also includes the normal directories and files.

I am assuming there is some Bash expression I am missing here. I was thinking I could script it so that a list of the hidden files/directories is manually fed into tar directly, but that seems a bit hackish; I am sure there is a proper way to do it.
 
Old 11-29-2007, 05:19 PM   #2
wit_273
Member
 
Registered: Mar 2007
Location: Nebraska
Distribution: CentOS
Posts: 82

Rep: Reputation: 15
Try this,
Code:
tar cvf /tmp/backup.tar ~/\.*
I tried it to test what I thought was happening and it worked for me.

What I believe is happening the way you have it this-- the (.) is a wildcard for any one character. So the way you are entering it you are saying to backup any files in your home directory that begins with any character followed by any series of characters. The (\) protects the (.), making bash read it as a literal (.) and not a wildcard.

George
 
Old 11-29-2007, 05:31 PM   #3
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Original Poster
Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Seems like that is doing the same thing.

Here is the output I get:

Code:
tj@T-Bird:~$ tar cvf /tmp/backup.tar ~/\.*
tar: Removing leading `/' from member names
/home/tj/./
/home/tj/./.nx/
/home/tj/./.nx/cache-unix-console/
/home/tj/./.nx/cache-unix-default/
/home/tj/./.nx/cache-unix-default/C-1950E4C4D24FC0CB12AA311E1F8D781E
/home/tj/./.nx/cache-unix-application/
/home/tj/./.nx/cache-unix-application/C-2391AE872CE0DE6F5716D1B93FB425CE
/home/tj/./.qt/
So far, so good. But then after awhile it goes to:

Code:
/home/tj/./.vlc/cache/
/home/tj/./.vlc/cache/plugins-04041e.dat
/home/tj/./.vlc/cache/CACHEDIR.TAG
/home/tj/./.vlc/vlcrc
/home/tj/./ISOs
/home/tj/./Temp/
/home/tj/./Temp/SuperCard/
/home/tj/./Temp/SuperCard/Patcher/
/home/tj/./Temp/SuperCard/Patcher/setupnewV261en.exe
/home/tj/./Temp/SuperCard/Patcher/setupnewV261en.zip
It starts archiving the normal directories as before.
 
Old 11-29-2007, 05:55 PM   #4
wit_273
Member
 
Registered: Mar 2007
Location: Nebraska
Distribution: CentOS
Posts: 82

Rep: Reputation: 15
Sorry about my previous post-- after closer examination what I tried did get unhidden files and directories. I just did it with a user that has very few unhidden files in the home directory. I also noticed though, that my suggestion included files one directory up (included ../), which on this system was only one other folder with mainly hidden files. So, my suggestion is not close to what you are looking for. Hopefully someone will come along soon that can help.

George
 
Old 11-29-2007, 06:25 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Something like
Code:
find -name .\* | tar czf test.tgz -T -
should do the trick ....



Cheers,
Tink
 
Old 11-29-2007, 06:36 PM   #6
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Original Poster
Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Same result with that one.

Running it in a test directory:

Code:
tj@T-Bird:~/Temp/PlayPen$ tree -a
.
|-- .hat.xm
|-- .hidden.tmp
|-- test
|   `-- abc
|       `-- 123.tmp
`-- test.tmp

2 directories, 4 files
tj@T-Bird:~/Temp/PlayPen$ find -name .\* | tar czf test.tgz -T -
tar: ./test.tgz: file changed as we read it
And the resulting archive:

Quote:
tj@T-Bird:~/Temp/PlayPen$ tar tvf ./test.tgz
drwxr-xr-x tj/users 0 2007-11-29 18:33 ./
drwxr-xr-x tj/users 0 2007-11-29 18:32 ./test/
drwxr-xr-x tj/users 0 2007-11-29 18:32 ./test/abc/
-rw-r--r-- tj/users 0 2007-11-29 18:32 ./test/abc/123.tmp
-rwxr-xr-x tj/users 380806 2007-11-29 18:31 ./.hat.xm
-rw-r--r-- tj/users 114688 2007-11-29 18:33 ./test.tgz
-rw-r--r-- tj/users 0 2007-11-29 18:32 ./test.tmp
-rw-r--r-- tj/users 0 2007-11-29 18:32 ./.hidden.tmp
-rwxr-xr-x tj/users 380806 2007-11-29 18:31 ./.hat.xm
-rw-r--r-- tj/users 0 2007-11-29 18:32 ./.hidden.tmp
Though I think this is the right track, I just need to pipe ONLY the hidden files/directories to tar, and let it run through that way.
 
Old 11-29-2007, 06:41 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Of course ... my bad, and my apologies.

Code:
find -type f -name .\* | tar czf test.tgz -T -

There you go.



Cheers,
Tink
 
Old 11-29-2007, 06:55 PM   #8
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Original Poster
Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Yes, that works for the files. I can't seem to get it working for hidden directories though. Giving "-type d" returns all files, for some reason.

But I managed to figure this out while poking around with variations on the pipe:

Code:
ls -A | egrep '^\.' | tar cvf ./test.tar -T -
I have to play with it a bit more, but so far that seems to get me all hidden folders and directories alone in the archive.

I am running this against my home directory to see what comes out. Thanks all.
 
1 members found this post helpful.
Old 11-29-2007, 07:05 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
And another shot :} ... this time tested :D


Code:
find -regex './\..*' | tar cvf ./test.tar -T -


Cheers,
Tink
 
Old 11-29-2007, 08:18 PM   #10
damonhart
LQ Newbie
 
Registered: Nov 2007
Posts: 22

Rep: Reputation: 15
directory '.' vs hidden file '.' gotcha

regarding the repeated mysterious reappearance of all files -

keep in mind that in every directory the current directory is '.' and for all but the root directory '..' means the parent directory. I'll bet tar suppresses following '..' up the directory hierarchy, but I'll also bet that explicitly listing ~/.* makes tar include ~/./regular_file.txt for instance.
 
Old 11-29-2007, 09:12 PM   #11
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Original Poster
Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Quote:
keep in mind that in every directory the current directory is '.' and for all but the root directory '..'
Yes, this seemed to be the problem in everything I was trying. ls was returning "./" and "../", and tar was then trying to add them to the archive (which was very confusing, having tar take files from the parent directory when I explicitly said to use the current directory).

I avoided this by calling ls with -A rather than -a. It turns out (the things you learn from the man pages, eh?) that -a is literally all files, and -A is all files without the current and parent directory symbols. When this is piped into tar, everything works the way it should.

Tinkster, looks like this one has hit the mark as well.

Though I noticed it seems to take quite some time to complete; I also hear a lot of thrashing from the drive. I take it that it must be looking for hidden files/directories under the non-hidden directories? That is a nice trick to keep in my list of useful commands, but for this particular situation I just want it to check 1 directory deep to get my user configuration files. But I can think of a few uses for finding hidden files anywhere in the tree.

In fact, this whole topic has been quite illuminating on a few concepts and regexes. Gotta love the community here at LQ.
 
Old 11-29-2007, 09:31 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by MS3FGX View Post
Tinkster, looks like this one has hit the mark as well. :)

Though I noticed it seems to take quite some time to complete; I also hear a lot of thrashing from the drive. I take it that it must be looking for hidden files/directories under the non-hidden directories? That is a nice trick to keep in my list of useful commands, but for this particular situation I just want it to check 1 directory deep to get my user configuration files. But I can think of a few uses for finding hidden files anywhere in the tree.

In fact, this whole topic has been quite illuminating on a few concepts and regexes. Gotta love the community here at LQ.
Well, if you want to search only one level add a
-maxdepth 1 to the find....


Glad you're enjoying the "brain share" at LQ :}


Cheers,
Tink
 
Old 01-13-2014, 07:49 AM   #13
oolekk
LQ Newbie
 
Registered: Jan 2014
Posts: 1

Rep: Reputation: Disabled
Here's a nice one:

Code:
find . -maxdepth 1 -regex "\./\..*" -print0 | tar cvfz test.tgz --null -T -
 
Old 01-13-2014, 11:46 AM   #14
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,797

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Easiest way is
Code:
tar -czf whatever .[^.]* ..?*
The first pattern matches names that begin with a dot, followed by any character other than a dot, followed by anything. The second pattern matches the (unlikely) names that begin with two dots followed by at least one more character.
 
  


Reply

Tags
backup, hidden, tar


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a tough question 4 u, problem in extracting tar & tar.gz files p_garg Linux - General 5 11-08-2010 12:02 PM
Can I back up hidden files using tar? opto Linux - Newbie 6 11-05-2006 10:35 AM
excluding hidden directories when using tar djgerbavore Slackware 3 01-05-2006 04:49 PM
home directory files gone, hidden files remain Grasshopper Linux - Security 12 04-10-2005 09:23 PM
tar and hidden files jjthomas Linux - General 4 04-07-2005 09:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration