LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Command line: How do I filter for dotfiles only? (https://www.linuxquestions.org/questions/linux-general-1/command-line-how-do-i-filter-for-dotfiles-only-169723/)

unamiccia 04-13-2004 04:36 PM

Command line: How do I filter for dotfiles only?
 
I want to archive the "dotfiles" and "dot directories" in my /home directory. When I try tar -czvf dotfilebackup.tgz .* I get an archive of everything because the shell interprets the "." to mean "the current directory," not "filenames beginning with a dot."

What's the filter trick to nab everything beginning with a dot but not the rest of the current directory's contents?

Thanks for all help!

david_ross 04-13-2004 04:52 PM

Try this:
tar -cf ~/dotbackup.tar `find ~/ -name ".*" -type f`

unamiccia 04-13-2004 05:03 PM

That sounded good, but the following happened:

me@box:~$ tar -cf ~/dotbackup.tar `find ~/ -name ".*" -type f`
tar: Removing leading `/' from member names
tar: /home/me/.gnome/apps/OpenOffice.org: Cannot stat: No such file or directory
tar: 1.1.0/.order: Cannot stat: No such file or directory
tar: /home/me/.gnome/apps/OpenOffice.org: Cannot stat: No such file or directory
tar: 1.1.0/.directory: Cannot stat: No such file or directory
tar: /home/me/.kde/share/applnk/OpenOffice.org: Cannot stat: No such file or directory
tar: 1.1.0/.directory: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors

Is there a little edit that will get this working?

david_ross 04-13-2004 05:08 PM

It looks like there may be funny characters in some file names - what are they - can you post the output of:
ls -l /home/me/.gnome/apps/Open*

unamiccia 04-13-2004 05:18 PM

Hmm -- they look pretty tame:

me@box:~$ ls -la /home/gb/.gnome/apps/Open*
total 40
|drwxr-xr-x 2 me me 4096 Mar 11 00:18 .
drwxr-xr-x 3 me me 4096 Mar 11 00:18 ..
-r--r--r-- 1 me me 167 Sep 1 2003 .directory
-r--r--r-- 1 me me 91 Sep 1 2003 .order
-r--r--r-- 1 me me 208 Sep 1 2003 calc.desktop
-r--r--r-- 1 me me 208 Sep 1 2003 draw.desktop
-r--r--r-- 1 me me 217 Sep 1 2003 impress.desktop
-r--r--r-- 1 me me 208 Sep 1 2003 math.desktop
-r--r--r-- 1 me me 1118 Sep 1 2003 printeradmin.desktop
-r--r--r-- 1 me me 214 Sep 1 2003 writer.desktop

david_ross 04-13-2004 05:22 PM

Sorry - it is probably the directory iteself - what about:
ls -l /home/me/.gnome/apps/

Muzzy 04-13-2004 05:25 PM

One problem is the space in 'OpenOffice.org 1.1.0'.

Doing something like this might be better:

find ~/ -name ".*" -type f -exec tar rf dotbackup.tar {} \;

Not sure if that predicate is really what you want though?

Do:

find ~/ -name ".*" -type f

to check that the files it lists are the ones you mean.

Mark Byers.

unamiccia 04-13-2004 05:30 PM

Aha -- good call! I wonder why OpenOffice.org sticks a space in their directory?

me@box:~$ ls -la /home/me/.gnome/apps/
total 12
drwxr-xr-x 3 gb me 4096 Mar 11 00:18 .
drwxr-xr-x 6 me me 4096 Apr 13 16:11 ..
drwxr-xr-x 2 me me 4096 Mar 11 00:18 OpenOffice.org 1.1.0

Do you have a top-of-your-head modification of the find syntax to handle that? Sorry, I'm feeling like a lazy questioner -- I could do this work myself . . .

unamiccia 04-13-2004 05:35 PM

Muzzy: I didn't see your post before I posted the above. Here's what happens when I try your syntax:

me@box:~$ find ~/ -name ".*" -type f -exec tar rf dotbackup.tar {} \;
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names

(about thirty lines of this)

??

david_ross 04-13-2004 05:37 PM

I think that should still work it is just using relative names.

For spaces you could also pipe the fins string though sed:
tar -cf ~/dotbackup.tar `find ~/ -name ".*" -type f | sed 's/\ /\\ /g'`

Muzzy 04-13-2004 05:38 PM

Try this match:

find ~/ -path "/home/me/.*" instead?

Or for current directory:

find . -path "./.*" instead?

This matches both dotfiles and directories. Using path matches the full path, not just the base filename.

unamiccia 04-13-2004 05:39 PM

BTW, I'm eager to learn this application of find, but I could swear that I once knew a simple filter strategy for outputting the files and directories that started with a "dot" without using find -- maybe through quoting -- but all my little tests prior to posting here failed to jog my memory (and try searching forums or Google on the string ".*" !).

mikshaw 04-13-2004 05:40 PM

Quote:

Originally posted by unamiccia
Aha -- good call! I wonder why OpenOffice.org sticks a space in their directory?

me@box:~$ ls -la /home/me/.gnome/apps/
total 12
drwxr-xr-x 3 gb me 4096 Mar 11 00:18 .
drwxr-xr-x 6 me me 4096 Apr 13 16:11 ..
drwxr-xr-x 2 me me 4096 Mar 11 00:18 OpenOffice.org 1.1.0

Do you have a top-of-your-head modification of the find syntax to handle that? Sorry, I'm feeling like a lazy questioner -- I could do this work myself . . .

maybe.... (this is just top-of-my-drunk-head)...

mkdir /home/me/backup
for file in /home/me/.* ; do
cp "$file" /home/me/backup
done

and then tar this directory. Like I said, though, it's just a guess without testing it myself. Just make sure the quotes around "$file" are there in order to include spaces.

unamiccia 04-13-2004 05:41 PM

I am called to dinner! Will try the new suggestions and post back later.

Meanwhile, thanks for the blow-by-blow help!

Muzzy 04-13-2004 05:47 PM

Doesn't the sed need to be:
sed "s/ /\\\\ "
because sed converts "\ " into a literal " " so you need to specify "\\\\ " to make sed see "\\ " so that it writes "\ " ! Even then, it seems that bash then converts it back to " " after the backtick is performed??? Even with 4 slashes it still doesn't work - I don't know why.

What was wrong with my earlier suggestion? Using the -exec of find, combined with the append of tar seems to do the trick for me.

Mark Byers.

Shade 04-13-2004 09:42 PM

Sometimes the answer is less complicated than piping commands, or stacking them.
This should work :

tar -czvf dotfilebackup.tgz \.*

The \ takes out special meaning of .
In this case, the special meaning is current directory. :-D

Works with ls, at least. didn't want to try tarring my entire . contents.

--Shade

Muzzy 04-14-2004 03:45 AM

I didn't know about the \. trick, this useful to know... however, I have tested your suggestion and it isn't quite correct because the directories '.' and '..' are included. Doing \.??* fixes it, at the risk of missing files of the form '.a' . Possibly this could be fixed in a better way??

But, I have actually tried the following:

find . -path "./.*" -exec tar -rf backup.tar {} \;

and it works. I even tried untarring it again just to be sure.

I am sure we have thoroughly confused the poster by now with all these different answers! But it's good to share tricks. I actually learnt the 'find -exec' pattern a few weeks again on this very site and I have found it very useful for a number of tasks, and is well worth learning.

unamiccia 04-14-2004 01:26 PM

Original poster here. Thanks to all for the time and energy invested in confusing me!

I can confirm that Shade's backslash suggestion (post #16) works for me -- problem solved.

Muzzy's find suggestion (post #17) seems to execute the correct filter, but I tried to execute it with compression (tar -rzf) and it wouldn't append to a compressed file. Doing a tar -czf produced an archive file of only a few bytes' length, so that doesn't appear to work, either.

I would like to undergo additional confusion and deconfusion regarding the find strategy if any of you have further interest. Meanwhile, though, thanks again!

unamiccia 04-14-2004 01:32 PM

Oops -- to be more precise, Shade as modified by Muzzy works:

tar -czvf dotfilebackup.tgz \.??*

And yes, it appears that it would skip filename .a -- practically not an issue for me, but of course it would be great to know that one last tweak to establish full control over the operation . . .

Muzzy 04-14-2004 02:24 PM

Hehe between us we will get there! But half the fun is in the learning, and the other half is when it works :)

I agree that when you try to append + compress it doesn't work, but a solution to this could be to tar first and then use gzip afterwards. This gives:

find . -path "./.*" -exec tar -rf backup.tar {} \; ;gzip backup.tar

Not the most simple solution ever.

The ".??*" hack works 99.9% (based on sample size of 2) of the time and is less to type. Any further improvements from othe LQ'ers would be worth learning, but at least you have something that works.

AutOPSY 04-14-2004 03:12 PM

They should just create a switch for tar " -a " To archive all files begginning with a dot.

Not being a proggrammer though, I don't know what problems this would cause.

LinFreak! 04-14-2004 06:10 PM

I found this problem also but gave up and removed the leading dot... I was in a hurry. It took ages.
Confucious say: the slow method removes the need for haste!


All times are GMT -5. The time now is 11:14 AM.