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.

marked as duplicate by muru command-line Jun 24 '16 at 11:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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. – Hohmannfan Jun 24 '16 at 11:56

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 ~

  • 1
    Not working, got following error: rm: cannot remove ‘.’: Is a directory rm: cannot remove ‘..’: Is a directory – Vicky Dev Jun 24 '16 at 11:46
  • 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. – Wayne_Yux Jun 24 '16 at 11:52
  • Confirmed to work. – Hohmannfan Jun 24 '16 at 12:02
  • Still not working for me, I have hidden files with name ending in ~, but they aren't deleted. – Vicky Dev Jun 24 '16 at 12:08
  • are they found with ls *~? are they write-protected? – Wayne_Yux Jun 24 '16 at 12:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.