In addition to those insightful answers, there's numerous patterns you can use to match groups of files. With filename expansion and pattern matching, you can tell bash to match things like all files in a directory, all files starting with something, all files with the same extension, etc.
Of course, if your using rm, you have to be very careful not to delete things you don't want to. I recommend using ls first, to list what the path argument will show.
Here's a few examples.
Say you want to match all files in a directory, to look at what an rm
command would process, look with ls
. You could use the asterisk (*
) glob operator to match any amount of characters.
ls /some/path/*
Or maybe all files starting with test:
ls /some/path/test*`
How about all files starting with test, with one character, and ending with a .sh
. You could use the question mark (?
) character to match one character.
ls /some/path/test?.sh
Let's say you have nine files test1.sh
, test2.sh
, etc, up to test9.sh
, and want to delete these test scripts. You also have a testa.sh
you want to keep. You could search for all files starting with test, the numbers one through nine or non-alphabetic classes, and ending with .sh
. These use the brackets ([]
) to match groups of characters.
ls /some/path/test[1-9].sh
ls /some/path/test[!:alpha:].sh
Matches such as these can be scripted fairly simply too, enabling very fine tuned or broad groupings of files within scripts for automation.
{..,..}
syntax with a wildcard (*
)? – Melroy van den Berg Feb 14 '23 at 21:05foo {bar,bar}*
is the same asfoo bar* baz*
. The result of the brace expansion is subject to further pathname expansion. – chepner Feb 15 '23 at 17:01