Wednesday, 27 January 2016

Deleting Files

Sometimes, you want to delete all but one of a file from a folder. Using a GUI, it’s fairly straightforward—Ctrl-A will normally select the files, Ctrl-click on the file you don’t want to delete to de-select it, and ... delete.

What about from the command line? Sometimes (like on a headless server or when you ssh into the computer at work) you need to do the same thing from a shell. Here’s the best way I’ve found to do that:

find . ! -name 'file.txt' -type f -exec rm -f {} +

This will delete every file from the current directory except file.txt.  Nice and easy.

No comments:

Post a Comment