You are not logged in.

#1 2014-06-04 17:50:52

publicus
Member
Registered: 2014-01-01
Posts: 129

How to remove just the hidden files?

Hello,

Recently I tried doing this in a script:

$ rm -rf .*
rm: cannot remove directory: `.'
rm: cannot remove directory: `..'

Hmm... this isn't quite what I wanted.  Should there be any .log.txt files (for example), I'd like to remove those but not touch . and .. which gives me an annoying error.

How can I do that?

Offline

#2 2014-06-04 18:10:03

dmbrown2520
Member
From: Akron, OH, USA
Registered: 2014-06-01
Posts: 14

Re: How to remove just the hidden files?

That should still remove all the hidden files. If you want to hide the error, you can do

rm -rf .* 2&>1 > /dev/null

Offline

#3 2014-06-04 18:14:48

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: How to remove just the hidden files?

publicus wrote:

How can I do that?

No problem! Check out this little trick:

$ rm .??*

Which will remove all files that begin with a dot and have at least two other characters in their filename. I use that syntax all the time. big_smile

...Hmmm, now that I think about it, it won't catch filenames with only one character, such as ".a". But I've never seen a filename like that, so whatever. tongue

Another option: Install "moreutils" and use "vidir". wink But that wouldn't be any good in a script.

Last edited by drcouzelis (2014-06-04 18:15:27)

Offline

#4 2014-06-04 18:19:58

publicus
Member
Registered: 2014-01-01
Posts: 129

Re: How to remove just the hidden files?

dmbrown2520 wrote:

That should still remove all the hidden files. If you want to hide the error, you can do

rm -rf .* 2&>1 > /dev/null

Thanks.  I'll keep this in mind.  I don't want to hide the error though.

I have something else cooking at the moment.

Offline

#5 2014-06-04 18:21:15

publicus
Member
Registered: 2014-01-01
Posts: 129

Re: How to remove just the hidden files?

drcouzelis wrote:
publicus wrote:

How can I do that?

No problem! Check out this little trick:

$ rm .??*

Which will remove all files that begin with a dot and have at least two other characters in their filename. I use that syntax all the time. big_smile

...Hmmm, now that I think about it, it won't catch filenames with only one character, such as ".a". But I've never seen a filename like that, so whatever. tongue

Another option: Install "moreutils" and use "vidir". wink But that wouldn't be any good in a script.

*raises eyeybrows*

Interesting...

Here is what I have:

#!/bin/sh

# copy all information that is in our data directory to the current directory,
#   afterwards delete it (again, first copy, then delete, same as the pouch
#   utility.)  Do this for the hidden and not hidden files.
HIDDEN_FILES=`ls -a1 ~/.pouch/data/`

for i in $HIDDEN_FILES
do
  if [ $i != '.' ] && [ $i != '..' ]
  then
    cp -r ~/.pouch/data/$i .
    rm -rf ~/.pouch/data/$i
  fi  
done

You're more than welcome to comment on suggestions and improvements.

Offline

#6 2014-06-04 18:25:21

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: How to remove just the hidden files?


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#7 2014-06-04 18:29:23

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: How to remove just the hidden files?

I'm so confused by your script... How about this to replace the whole script:

$ mv ~/.pouch/data/.??* .

Offline

#8 2014-06-04 18:44:58

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: How to remove just the hidden files?

I'd use find:

find <dir> -mindepth 1 -maxdepth 1 -name ".*" -exec ls -ld {} +

But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#9 2014-06-05 16:54:03

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: How to remove just the hidden files?

publicus wrote:
$ rm -rf .*

you should avoid doing this to prevent it from moving up the directory tree (i.e. ../anotherFolder/). instead use something like

for f in .*; do rm -rfv "$f"; done

Last edited by HiImTye (2014-06-05 16:55:46)

Offline

#10 2014-06-05 17:30:38

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: How to remove just the hidden files?

HiImTye wrote:
publicus wrote:
$ rm -rf .*

you should avoid doing this to prevent it from moving up the directory tree (i.e. ../anotherFolder/). instead use something like

for f in .*; do rm -rfv "$f"; done

Why? Both his way and your way have the same result:

$ rm -rf .
rm: cannot remove directory: `.'

$ rm -rf ..
rm: cannot remove directory: `..'

Offline

#11 2014-06-05 17:46:41

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: How to remove just the hidden files?

HiImTye wrote:
publicus wrote:
$ rm -rf .*

you should avoid doing this to prevent it from moving up the directory tree (i.e. ../anotherFolder/). instead use something like

for f in .*; do rm -rfv "$f"; done

That does exactly the same thing. .* expands to ., .. and any dot files in the directory. Removing them with a single command or multiple commands doesn't change that.


*IF* . and .. are always listed first when expanding .* then the following will work. I came up with this quickly. I suggest that you check the official documentation of your shell to be sure that the assumption is valid.

dot_files=(.*)
rm -fr "${dot_files[@]:2}"

My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#12 2014-06-05 17:52:03

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: How to remove just the hidden files?

must be one of those things you remember from days of yore wink seems to be fixed now

Offline

Board footer

Powered by FluxBB