0

Here I want to remove all tilde files from my directories and subdirectories . How can I use the linux command here?

Tree Structure:

.
|-- Block_Physical_design_checklist
|   |-- Block_Physical_design_checklist.config
|   |-- Block_Physical_design_checklist.html
|   |-- Block_Physical_design_checklist.html~
|   `-- rev6
|       |-- rev6.config
|       `-- rev6.html
|-- CAD_checklist
|   |-- CAD_checklist.config
|   |-- CAD_checklist.html
|   |-- CAD_checklist.html~
|   `-- rev6
|       |-- rev6.config
|       `-- rev6.html
|-- Formality_DCT_Vs_ICC
|   |-- Formality_DCT_Vs_ICC.config
|   |-- Formality_DCT_Vs_ICC.html
|   |-- Formality_DCT_Vs_ICC.html~
|   `-- rev6
|       |-- rev6.config
|       |-- rev6.html
|       `-- rev6.html~

Expected Tree Structure:

.
|-- Block_Physical_design_checklist
|   |-- Block_Physical_design_checklist.config
|   |-- Block_Physical_design_checklist.html
|   `-- rev6
|       |-- rev6.config
|       `-- rev6.html
|-- CAD_checklist
|   |-- CAD_checklist.config
|   |-- CAD_checklist.html
|   `-- rev6
|       |-- rev6.config
|       `-- rev6.html
|-- Formality_DCT_Vs_ICC
|   |-- Formality_DCT_Vs_ICC.config
|   |-- Formality_DCT_Vs_ICC.html
|   `-- rev6
|       |-- rev6.config
|       |-- rev6.html
1

Your approach find . -type f -name '*~' -exec rm -f '{}' \; has couple of issues/improvement scopes:

  • -name '*~' matches only files ending in ~; if you want to match any file containing ~, use *~*

  • -exec rm -f '{}' \; is spawning rm for each file, which is clumsy and inefficient; instead, as rm can take multiple files as arguments, you can tell find ... -exec to get as many files in one go without triggering the ARG_MAX using the + argument to -exec

Putting these two together:

find . -type f -name '*~*' -exec rm -f {} +

If you happen to have GNU find, you can use the -delete action:

find . -type f -name '*~*' -delete

In zsh, you can do recursive pattern matching and removal in one go like:

rm -f -- **/*~*(.)

the glob modifier . matches only regular files.

| improve this answer | |
1

With bash's globstar option:

shopt -s globstar ; rm ./**/*~

globstar allows for recursive globbing with **, while ./ prevents issues with filenames that might have a leading - in them and *~ will match filenames ending with tilde

| improve this answer | |
0

Here is my answer,

find . -type f -name '*~' -exec rm -f '{}' \;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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