Score:-2

remove certain files except others

in flag

I found so many suggestions as to how to remove files with exceptions. However, this case is a little different. Let's say, I have 6 files as follows in my Ubuntu 18.04:

text_1.json
text_2.json
video_1.mp4
video_2.mp4
video_1_notouch.mp4
video_2_notouch.mp4

The command should remove only video_1.mp4 and video_2.mp4 files while leaving other files as is.

I tried rm -v !(*.json|*_notouch.mp4) as suggested here, but it removed all 6 files.

Someone avatar
my flag
Please [edit] your question to include 0. Your Ubuntu version , 1. What is your problem 2. Now what do you want after removing those files
Someone avatar
my flag
What do you mean by removed everything ?
guiverc avatar
cn flag
[Ubuntu 16.04 LTS has reached the end of it's *standard* support life](https://fridge.ubuntu.com/2021/03/13/extended-security-maintenance-for-ubuntu-16-04-xenial-xerus-begins-april-30-2021/) thus is now off-topic here unless your question is specific to helping you move to a supported release of Ubuntu. Ubuntu 16.04 ESM support is available, but not on-topic here, see https://askubuntu.com/help/on-topic See also https://ubuntu.com/blog/ubuntu-16-04-lts-transitions-to-extended-security-maintenance-esm
bit_scientist avatar
in flag
oops, it's 18.04, not 16.04, sorry for a typo
bac0n avatar
cn flag
what does `shopt extglob` say?
bit_scientist avatar
in flag
@bac0n It was when I used the command, now it's off
bac0n avatar
cn flag
I think shopt extglob is set by bash-completion, but you may also append it to `~/.bashrc`, it's not inherited though, so you will have to re-set it when you intend to invoke a new shell, e.g., `bash -c 'shopt extglob'` may be written *bash -O extglob -c 'shopt extglob'*
user535733 avatar
cn flag
`rm` is permanent and not undo-able. If you have risk because files-to-keep and files-to-delete are mixed together, then implement basic risk management controls: Separate the steps (use `find`) or separate the files into tempdirs. Or take the time to go file-by-file. Read your output carefully and test your findings before deleting. It will save you many tears.
Score:7
cn flag

It is better and easier to use the find command:

find . -type f -name "*.mp4" ! -name "*_notouch.mp4" -exec rm -v {} \;

This command will match any file name ending with .mp4, but not with _notouch.mp4 and will run the command given after -exec.

Before such destructive operations, it always wise to prepend or replace the rm command with an echo and verify you are getting the commands you are expecting:

find . -type f -name "*.mp4" ! -name "*_notouch.mp4" -exec echo rm -v {} \;

[Edit] As mentioned in the comments; in your case, you can just use -print -delete in place of -exec echo rm -v {} \; (which will be more efficient if you have millions of files). And to list the files that will be affected (before running the destructive command) you can just run:

find . -type f -name "*.mp4" ! -name "*_notouch.mp4" (-print is optional)

Also, if you do not want to affect subdirectories, you can use the -maxdepth 1 option like this:

find . -maxdepth 1 -type f -name "*.mp4" ! -name "*_notouch.mp4".

bac0n avatar
cn flag
better use `+` instead of ```\;```
bac0n avatar
cn flag
or `find -type f -name '...' ... -print -delete`
Liso avatar
sd flag
+1 Neat trick !
user535733 avatar
cn flag
+1 for taking the time to explain a SAFE way of doing this.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.