How to delete all files/directories with exception/s using RM command in Linux

[root@testserver temp]#ls  | grep -v "<name>\|<name>" | xargs rm -rf
[root@testserver /tmp]# ls
dir1 dir2 dir3 file1.txt file2.txt file3.txt
[root@testserver /tmp]#
[root@testserver /tmp]# ls | grep -v "file1.txt\|dir2" | xargs rm -rf
[root@testserver /tmp]# ls
dir2 file1.txt
[root@testserver /tmp]#

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aaron Medina

Aaron Medina

More from Medium

Auto mount disk when restart by UUID

Linux: how to add a filesystem to fstab the right way

Automated Backups with cron and RClone

Ingesting and processing multiple lines in Linux using an array and loops.