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.


All times are GMT -5. The time now is 06:25 PM.