Delete all hidden files recursively in current directory

5,877

Not really one single command, but why make it more complicated than necessary?

find -type f -name ".*" -delete; rm *~

The first command removes all files starting with . and the second one all ending with ~

Share:
5,877

Related videos on Youtube

Vicky Dev
Author by

Vicky Dev

B.E. COMPUTER ENGG. WEB DEVELOPER (PHP/Magento/Wordpress/jQuery/Core Javascript/PrototypeJs/)

Updated on September 18, 2022

Comments

  • Vicky Dev
    Vicky Dev over 1 year

    I have a directory which contains hidden files and by that I mean, not just the files whose name starts with . but also the files whose name contains ~ character at the end like somefile.txt~.

    I want to delete only those files (the remaining files need to be unaffected) in an efficient way.

    How I can achieve that with a single command ?

    Ubuntu 14.04 Trusty Tahr.

    • SE - stop firing the good guys
      SE - stop firing the good guys almost 8 years
      One way of doing this is rm $(ls -a | grep -e "^\.[a-zA-Z0-9_ ].*" -e ".*~") Perhaps a bit wordy, but is does the job without deleting anything it should not.
  • Vicky Dev
    Vicky Dev almost 8 years
    Not working, got following error: rm: cannot remove ‘.’: Is a directory rm: cannot remove ‘..’: Is a directory
  • Wayne_Yux
    Wayne_Yux almost 8 years
    ah yes, I overlooked this. This is because .* is also expanded to the directories . and ...which cannot be deleted. See my edit for an alternative command that does not cause the error.
  • SE - stop firing the good guys
    SE - stop firing the good guys almost 8 years
    Confirmed to work.
  • Vicky Dev
    Vicky Dev almost 8 years
    Still not working for me, I have hidden files with name ending in ~, but they aren't deleted.
  • Wayne_Yux
    Wayne_Yux almost 8 years
    are they found with ls *~? are they write-protected?
  • Vicky Dev
    Vicky Dev almost 8 years
    Nope, not getting by that command either. Not even with ls -R *~ .
  • Wayne_Yux
    Wayne_Yux almost 8 years
    what about ls *~*?