Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all files NOT ending with certain formats?

Tags:

linux

shell

So to remove all files ending with .lnx, the cmd would be rm *.lnx, right?

If I want to remove all files that do NOT end with [.lnx], what command should I use?

Is there such a thing?

like image 312
user113454 Avatar asked Mar 03 '12 18:03

user113454


People also ask

How do I delete all files except a specific file extension?

Highlight all the files you want to keep by clicking the first file type, hold down the Shift key, and click the last file. Once all the files you want to keep are highlighted, on the Home Tab, click Invert Selection to select all other files and folders.

How do you delete all files except the latest three in a folder windows?

xargs rm -r says to delete any file output from the tail . The -r means to recursively delete files, so if it encounters a directory, it will delete everything in that directory, then delete the directory itself.

What command will remove all files that end in?

The rm command can remove multiple files at once either by passing it more than one file or by using a pattern. In the following example a pattern is used to remove all filenames ending in '. txt'. Be careful that no unwanted files are removed using this method.


3 Answers

You can use this:

$ rm !(*.lnx)

!(pattern-list)
    Matches anything except one of the given patterns. 
    A pattern-list is a list of one or more patterns separated by a ‘|’.
like image 117
kev Avatar answered Sep 20 '22 01:09

kev


find . -depth 1 -type f -not -name '*.lnx' -delete

find all files (-type f) in the current directory (-depth 1) which do not match the filename (-not -name '*.lnx'), and delete them (-delete)

As always, test this first. Run it without the -delete to see all the files that match.

like image 22
nachito Avatar answered Sep 23 '22 01:09

nachito


ls | grep -v '\.lnx$' | xargs rm
like image 38
Manish Avatar answered Sep 20 '22 01:09

Manish