LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Shell and batch operations on hidden files but not on ".." & "." (http://www.linuxquestions.org/questions/linux-software-2/shell-and-batch-operations-on-hidden-files-but-not-on-and-584117/)

danielsbrewer 09-12-2007 05:50 AM

Shell and batch operations on hidden files but not on ".." & "."
 
In the shell, is it possible to perform a recursive batch action on all of the hidden files but not on ".." & "."?

Let me give you an example of what I mean. Acting as root, I wanted to change the all the hidden files in a user's home directory including the contents of any hidden directory. So I did this:

chown -R user.group .*

This ended up changing not only the hidden files/directories but the normal files and directories and worst of all /home and everything within it.

I am assuming that I similar thing would happen if I tried the command rm -rf .* i.e. the contents of all parent directories up to "/" would be deleted.

The problem is that ".." and "." are picked up by .*.

Any ideas of how to perform these sort of actions correctly?

blackhole54 09-12-2007 06:22 AM

Use .[^.]*

for example, to list all hidden files and directories but not . and ..

Code:

ls -d .[^.]*
Try it out with something where you can't do any damage first.

danielsbrewer 09-12-2007 06:46 AM

Thanks that worked great

blackhole54 09-12-2007 07:42 AM

I just realized that .[^.]* will miss files that start with two dots! But you probably don't have too many of those. .*[^.]* is a little better, but will still miss files that consist of nothing but dots (for example, 3 dots only). I can't think of anything that completely meets your criterion.

CORRECTION: (albeit probably just an academic one!) You can cover all bases by using two globbing expressions:

Code:

ls -d .*[^.]* ...*
CORRECTION2: Drat! The above will (potentially) list some files twice. For this (probably) totally academic excercise, lets see if the third time is the charm:

Code:

ls -d .[^.]* ..?*

trashbird1240 09-12-2007 07:59 AM

I'd just like to put in a plug for my favorite shell, the Z Shell,
which you can still program in if you're running as root. One of the
nice things about the file-globbing in Z Shell is that if you know the
situation you want to solve, you can look it up on Google: somebody
has definitely already done it.

Whichever shell I'm using, I deal with the dots by quotation marks:
"."* only gets hidden files for me.

Joel

blackhole54 09-12-2007 08:06 AM

Quote:

Originally Posted by trashbird1240 (Post 2889622)
Whichever shell I'm using, I deal with the dots by quotation marks:
"."* only gets hidden files for me.

I am not familiar with Z shell, but in bash, the quotation marks won't make any difference. You will still pick up . and .. which the OP did not want.


All times are GMT -5. The time now is 11:34 PM.