I need to remove all files with .gif extension except one file with name say "filename.gif". What is the optimal way to go about doing this in terminal?
The command rm
*.gif removes all gif files including the file
Here's the simple solution you should probably use:
There's nothing special about the
If you must not rename the file (and there are scripting situations where this is important):
Or you can write it shorter like this:
You may prefer to use
I've used the
One handy thing about
Please note that I've used
All these solutions only delete files residing immediately in the current directory. They don't delete files not contained in the current directory, and they don't delete files that reside in subdirectories of the current directory.
If you want to delete files everywhere contained within the current directory (that is, including in subdirectories, and in subdirectories of those subdirectories, and so forth--files contained within the current directory or any of its descendants), use
Be careful with this!
You can also set other values with
Just make sure you never put
Open a Terminal with Ctrl + Alt + T and type:
The difference between
If you're using
(I include that line in my
Among other things, it grants access to the
More information is available in
If you can't use
You can adapt this for other needs, such as removing all but the ten latest log files:
But beware, this doesn't take proper account of spaces; to make it more robust I'd rewrite it as a Python script.