Rm command with hidden files

I’m not a stranger to the rm command. I’ve used it many times to remove files. But I’ve just come across a weird situation when I tried to remove hidden files, using wildcards. The hidden files remain. Is there some special option that I don’t know about when trying to remove hidden files?

Summary

This text will be hidden

[paulb@paulb-vostro470 ~]$touch dwhelper/.hiddenfile
[paulb@paulb-vostro470 ~]$ls -a dwhelper
. .. .hiddenfile
[paulb@paulb-vostro470 ~]$touch dwhelper/.hiddenfile2
[paulb@paulb-vostro470 ~]$ls -a dwhelper
. .. .hiddenfile .hiddenfile2
[paulb@paulb-vostro470 ~]$touch dwhelper/nothidden
[paulb@paulb-vostro470 ~]$ls -a dwhelper
. .. .hiddenfile .hiddenfile2 nothidden
[paulb@paulb-vostro470 ~]$ls dwhelper
nothidden
[paulb@paulb-vostro470 ~]$ls -a dwhelper
. .. .hiddenfile .hiddenfile2 nothidden
[paulb@paulb-vostro470 ~]$rm dwhelper/*
[paulb@paulb-vostro470 ~]$ls -a dwhelper
. .. .hiddenfile .hiddenfile2
[paulb@paulb-vostro470 ~]$rm -f dwhelper/*
[paulb@paulb-vostro470 ~]$ls -a dwhelper
. .. .hiddenfile .hiddenfile2
[paulb@paulb-vostro470 ~]$

Hi @bendipa1

You need -r for recusive

rm -r
or force remove recusive
rm -rf
$ rm --help
 -r, -R, --recursive
         remove directories and their contents recursively

I don’t want to remove the directory. Just its contents.

try rm .*

thats correct, this call remove hidden files, but not the standard files. To remove both, you must run rm twice:

rm -f dwhelper/*
rm -f dwhelper/.*

Try it first.

rm -r dir/.*

This will delete only .*. Dot or no dot that does not matter.

-f = force

please read the help.

-f, --force
         ignore nonexistent files and arguments, never prompt

That one does remove the hidden files, but as mentioned you then have to run the command again (without a period) to get rid of non-hidden files. I’m fairly sure in the past on another distro that wasn’t necessary, and the use of one wildcard got rid of both, otherwise I’d have picked up on this long before now.

$ mkdir -p help/help
$ mkdir -p help/.help

# remove both .help aswel as help dir in one go
$ rm -r help/.* help/

rm -rf help/

Removes the directory and contents…

The -r is the key.

If you want to delete both unhidden and hidden files in the current directory in one command, I would personally go with rm -rf * .*

Like this:

~ ❯ mkdir test                                            

~ ❯ test/                                           

~/test ❯ touch test.md

~/test ❯ touch .test.md

~/test ❯ ls   
 .test.md   test.md

~/test ❯ rm -f * .*                                             
zsh: sure you want to delete the only file in /home/erne/test [yn]? y

~/test ❯ ls

~/test ❯ 

P.S. The reason I was able to enter test directory without using cd is I use zsh with auto_cd enabled.

reminder if newbie are testing this commands..
be careful of using rm -rf as miss typing it will ruin your day.. much better to use graphical way of removing/deleting items. Just open your file manager set to view hidden item and run it in admin/root mode to delete.