You are not logged in.
Pages: 1
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
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
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.
...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.
Another option: Install "moreutils" and use "vidir". But that wouldn't be any good in a script.
Last edited by drcouzelis (2014-06-04 18:15:27)
Offline
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
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.
...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.
Another option: Install "moreutils" and use "vidir". 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
Offline
I'm so confused by your script... How about this to replace the whole script:
$ mv ~/.pouch/data/.??* .
Offline
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
$ 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
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
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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
must be one of those things you remember from days of yore seems to be fixed now
Offline
Pages: 1