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?
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.
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.
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.
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 ‘|’.
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.
ls | grep -v '\.lnx$' | xargs rm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With