Score:0

Bash: Removing Multiple Files

kg flag

Beginner here...

I have a fruit folder, named "apples," that has extra files I want to remove.

Remove all files that begin with "red_dk" and "red_lt"
Remove all files that end with ".gif"

If I do a single file, it works.

#!/bin/bash
rm "red_dk*" 

If I do multiple, it does not

#!/bin/bash
rm "red_dk" "red_lt*" "*.gif"
Zatigem avatar
ru flag
Your first command `rm "red_dk*"` does not work... It deletes a file named exactly "red_dk*" and won't delete for example "red_dk_hello".
Score:4
gw flag

The * character (as well as ? and [a-z]-style character groups) match all files with all the other non–wildcard parts in their name name, and expand the argument list to include all those matching filenames. This is called "glob file expansion".

You can preview it using echo.

eg:

$ echo red_dk* red_lt* *.gif
red_dk1 red_dk2 red_lt1 red_lt2 meme.gif answer.gif
$

To remove the files, replace echo with rm. I tend to stack them, like echo rm.

If you somehow ended up with a literal * in your filename, then you could remove that file specifically by quoting it. Quoting, single or double, avoids "glob file expansion".

$ echo "red_dk*"
red_dk*
$ rm "red_dk*"
rm: cannot remove 'red_dk*': No such file or directory
$
I sit in a Tesla and translated this thread with Ai:

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.