LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   rm: Recursively erasing all hidden directories but not the other ones? (https://www.linuxquestions.org/questions/slackware-14/rm-recursively-erasing-all-hidden-directories-but-not-the-other-ones-4175444885/)

stf92 01-09-2013 07:13 PM

rm: Recursively erasing all hidden directories but not the other ones?
 
What's said. Bear in mind this is a weird question and, as such, I could not find references in either LQ or google.

stf92 01-09-2013 07:32 PM

Thinking it over again, I came with
Code:

$ rm -r .[0-z]*
It worked.

Thad E Ginataom 01-10-2013 12:54 AM

Quote:

It worked.
Did it?

But what else did it do? How many files did it rm as well as directories? Was that what you really wanted?

Executing that command in a home directory, for instance, could be disastrous.

kabamaru 01-10-2013 02:43 AM

I'd use something like this:

Code:

find . -type d -name ".*" -mindepth 1 -maxdepth 1 -exec rm -rf '{}' \;

stf92 01-10-2013 03:41 AM

Quote:

Originally Posted by Thad E Ginataom (Post 4866740)
Did it?

But what else did it do? How many files did it rm as well as directories? Was that what you really wanted?

Executing that command in a home directory, for instance, could be disastrous.

It was like this: user1 (me) started X and the GUI was in some way corrupted. It was not X, as /etc/Xorg.0.log showed. I then created user2, copied two .* file from /home/user1 which I cared about, and started X as user2. At least now I could use the GUI.
From here I started the mail client and it downloaded all of the mail from my ISP sever. But now user1 settings had no importance to me. .bashhistory, .vimrc (I have vim's settings in a global vimrc) and so on. The only thing important were some big trees in /home/user1. So, in this dir I had the .* files and dirs, and the big directories, not hidden I wanted to preserve. Then I executed 'rm -r .[0-z]*' from /home/user1.

GazL 01-10-2013 04:47 AM

Quote:

Originally Posted by kabamaru (Post 4866810)
I'd use something like this:

Code:

find . -type d -name ".*" -mindepth 1 -maxdepth 1 -exec rm -rf '{}' \;

Just a word of caution for anyone reading this:

That -mindepth 1 option is VERY IMPORTANT as otherwise it'll process the '.' (current directory)

In these sorts of situations I always prefer to use explicit paths rather than relative ones as it is a little less accident prone.
find /home/gazl -name '.*' ...... wouldn't be nearly as accident prone if one were to forget the "-mindepth 1" option or be in the wrong CWD..


edit:

Actually, having just tried it on /tmp/something it's not quite as bad as I thought, as it will result in a "rm: cannot remove directory: '.'", but it clearly shows that it tried to remove it so its still something to be wary of.

Thad E Ginataom 01-16-2013 06:48 AM

find is the tool of choice for all but the simplest such tasks. We can also play safe by using -ls, -print, or even -exec ls -r '{}' \; to be sure about what find is finding, before inserting the actual command. The -r option to rm, of course, adds an extra level of danger. When one has to use rm with highly dangerous wildcards, there is always the -i option.

perbh 01-16-2013 09:07 PM

I am always surprised of the number of people that regard /home/$USER as a holy grail and even put it on a separate filesystem. Ok, if you run only _one_ distro, I can see the point - if you have more - absolutely not. Dot-files should not be common between distros - let /home be part of the root filesystem (with all its dot-files for each user - even if there is only one) and make a big filesystem for your private files. For myself, I typically use around 20 gigs for each distro and its root filesystem, a small partition (#1) for grub-legacy where I chainload to each distro which has its own bootloader on the root partition, and keep one big partition for my private files. This way, anything OS-dependent (which the dot-files are) are kept where they belong - under the OS. Also, it means you can replace one distro with another without touching your private files. The downside is that each time you install a new distro - you have to go through the setup (generating dot-files) to make it look the way you want - however, as far as _I_ am concerned, that is a small price to pay.
These days it's common to have Documents, Pictures, Music, etc in your home directory - what I do is to make links to where I keep my private files - keeping them private across distros.
For example, my big private partition is called '/disk', then I make links like so:
Code:

dirs="Documents,Downloads,Music,Pictures,Public,Templates,Videos"
# as root:
host:~ # mkdir -m 0777 /disk/{$dirs}
# as user, from my home:
host:~ $ for d in $(echo $dirs | tr ',' ' '); do rmdir $d; ln -s /disk/$d $d; done

... just my 2c-worth

Thad E Ginataom 01-17-2013 01:10 AM

I can add my 2c to yours.

Since I "returned" to the *nix fold a couple of years ago, I found that the number of .something files has hugely increased since my Unix green-screen days, and also that some of the stuff that gets stored in the home directory, such as mail files, wine, etc, can get rather large. Added to which, I want to be able to access Thunderbird, for instance, from multiple versions/distros, and I want settings and data for Firefox to be the same (yes, these days, FF sync does a good job) just to name two programs.

Thus, in my mind, the concept of $HOME as a place where one "lives" is now outdated and has a become a place where configuration files are kept --- and, where these configuration files are cross-version, links do the job just fine.

I actually have two big file systems, one called Library (of the human, rather than the development, kind) and one called Data where all the other shared stuff goes. Library is available to the rest of the family on the net, Data is not.

Old habits have made me still have a separate /home, but, in future, I'll take your advice and not even do that.

Sorry... OT to the thread [Blush]

wstewart 01-17-2013 07:35 AM

Provided you are using the same version of ls as I am, you could use

for i in $(ls -al|grep -vE '\s\./$|\s\.\./$'|grep -e '\/$'|awk '{print $9}'); do rm -r $i;done

But I'm sure there's a simpler way to go about doing it.

unSpawn 01-18-2013 12:06 PM

Quote:

Originally Posted by wstewart (Post 4872134)
I'm sure there's a simpler way to go about doing it.

Not only that, see the 'rm' and 'find' commands above, but http://mywiki.wooledge.org/ParsingLs to start with...

qweasd 01-18-2013 12:35 PM

I don't think the notion of $HOME as the place where one "lives" is dead. Not for me, at least,
not on my laptop. Data, configuration, and odd things like dosbox environment all belong there, IMHO.
What drives me bananas is the glut of .whatevers littering the place. I really like ~/.config/
and I wish more developers used it.

Thad E Ginataom 01-19-2013 11:47 AM

I have never before answered any of your posts --- so perhaps you'll not mind me agreeing that
Quote:

there's a simpler way to go about doing it.


All times are GMT -5. The time now is 02:29 AM.