A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label delete. Show all posts
Showing posts with label delete. Show all posts

Delete all files except one on linux

Glob

  • shopt -s extglob
  • rm -v !("filename")
  • rm -v !("filename1"|"filename2")
  • rm -v !(*.zip|*.odt)
  • shopt -u extglob
Find
  • find /directory/ -type f -not -name 'PATTERN' -delete
  • find /directory/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm {}
  • find /directory/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I {} rm [options] {}
  • find . -type f -not \(-name '*gz' -or -name '*odt' -or -name '*.jpg' \) -delete
Glob ignore
  • cd test
  • GLOBIGNORE=*.odt:*.iso:*.txt
  • rm -v *
  • unset GLOBIGNORE

Batch deleting files in Windows

List all the jpg files:

dir /s *.jpg /b > jpgs.txt

Delete all the jpg files:

del /s /q /f /a *.jpg



Delete a file starting with a dash

You accidentally created a file called "-.log"

To delete it, instead of
rm -.log
try
rm ./-.log