Logical Progression of building a command line trick
So lets take this step by step…
First list all the files
Lets get a recursive list of all files in the current directory…
$ find .
...LOTS of files...
Find filenames by Regex
Ok, that’s a big list, but I only need certain files.
$ find . | grep \.bak[0-9]*\.txt
Use sed to alter the list a bit
So for each row, were adding quoting it, and adding rm to the beginning. This will just output the list of commands, it won’t actually remove the files. MAKE SURE YOU DO THIS FIRST and you like the list it creates. Or don’t blame me if you delete everything on your whole computer.
find . | grep \.bak[0-9]*\.txt | sed -E 's/(.*)/rm "\1"/'
Now we get dangerous
You could just copy and paste that list into the terminal. Or you could be really cool and just pipe it to bash…
BUT LIKE I SAID ABOVE, RUN THE LIST FIRST AND MAKE SURE YOU LIKE THE RESULTS BEFORE YOU PULL THE TRIGGER.
find . | grep \.bak[0-9]*\.txt | sed -E 's/(.*)/rm "\1"/' | bash