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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-29-2007, 05:01 PM
|
#1
|
LQ Guru
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852
|
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.
|
|
|
11-29-2007, 05:19 PM
|
#2
|
Member
Registered: Mar 2007
Location: Nebraska
Distribution: CentOS
Posts: 82
Rep:
|
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
|
|
|
11-29-2007, 05:31 PM
|
#3
|
LQ Guru
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852
Original Poster
|
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.
|
|
|
11-29-2007, 05:55 PM
|
#4
|
Member
Registered: Mar 2007
Location: Nebraska
Distribution: CentOS
Posts: 82
Rep:
|
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
|
|
|
11-29-2007, 06:25 PM
|
#5
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Something like
Code:
find -name .\* | tar czf test.tgz -T -
should do the trick ....
Cheers,
Tink
|
|
|
11-29-2007, 06:36 PM
|
#6
|
LQ Guru
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852
Original Poster
|
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.
|
|
|
11-29-2007, 06:41 PM
|
#7
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Of course ... my bad, and my apologies.
Code:
find -type f -name .\* | tar czf test.tgz -T -
There you go.
Cheers,
Tink
|
|
|
11-29-2007, 06:55 PM
|
#8
|
LQ Guru
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852
Original Poster
|
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.
|
11-29-2007, 07:05 PM
|
#9
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
And another shot :} ... this time tested :D
Code:
find -regex './\..*' | tar cvf ./test.tar -T -
Cheers,
Tink
|
|
|
11-29-2007, 08:18 PM
|
#10
|
LQ Newbie
Registered: Nov 2007
Posts: 22
Rep:
|
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.
|
|
|
11-29-2007, 09:12 PM
|
#11
|
LQ Guru
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852
Original Poster
|
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.
|
|
|
11-29-2007, 09:31 PM
|
#12
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by MS3FGX
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
|
|
|
01-13-2014, 07:49 AM
|
#13
|
LQ Newbie
Registered: Jan 2014
Posts: 1
Rep:
|
Here's a nice one:
Code:
find . -maxdepth 1 -regex "\./\..*" -print0 | tar cvfz test.tgz --null -T -
|
|
|
01-13-2014, 11:46 AM
|
#14
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,797
|
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.
|
|
|
All times are GMT -5. The time now is 05:16 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|