LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   tar ONLY hidden files (https://www.linuxquestions.org/questions/linux-software-2/tar-only-hidden-files-603340/)

MS3FGX 11-29-2007 04:01 PM

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.

wit_273 11-29-2007 04:19 PM

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

MS3FGX 11-29-2007 04:31 PM

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.

wit_273 11-29-2007 04:55 PM

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

Tinkster 11-29-2007 05:25 PM

Something like
Code:

find -name .\* | tar czf test.tgz -T -
should do the trick ....



Cheers,
Tink

MS3FGX 11-29-2007 05:36 PM

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.

Tinkster 11-29-2007 05:41 PM

Of course ... my bad, and my apologies.

Code:

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

There you go.



Cheers,
Tink

MS3FGX 11-29-2007 05:55 PM

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.

Tinkster 11-29-2007 06:05 PM

And another shot :} ... this time tested :D


Code:

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


Cheers,
Tink

damonhart 11-29-2007 07:18 PM

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.

MS3FGX 11-29-2007 08:12 PM

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.

Tinkster 11-29-2007 08:31 PM

Quote:

Originally Posted by MS3FGX (Post 2975076)
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

oolekk 01-13-2014 06:49 AM

Here's a nice one:
 
Code:

find . -maxdepth 1 -regex "\./\..*" -print0 | tar cvfz test.tgz --null -T -

rknichols 01-13-2014 10:46 AM

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 10:47 PM.