With just rm
:
rm simulations/posterior_predictive_sim_*/seq\[[1-4]\].nex
The escaped \[
and \]
are literal square brackets. The unescaped [1-4]
inside them is a glob pattern that matches the digits 1 to 4.
This only works if the glob doesn't expand to more filenames than will fit into one command-line, the ARG_MAX limit - this varies depending on the OS, but is about 2 million characters on modern Linux. ARG_MAX applies to external commands like rm
, not to shell built-ins like for
or echo
.
Also, test this with /bin/echo
instead of rm
first. Because: 1. /bin/echo is safe, won't delete anything. and 2. /bin/echo
is an external command, not the shell built-in version of echo so will also test if the total command length will be <= ARG_MAX.
Using GNU find
:
find simulations/ -type f -name 'seq\[[1-4]\].nex' -delete
I suggest running this with -ls
or -print
before running it with -delete
(or -exec rm
as below), just to verify that it will do what you want.
Alternatively:
find simulations/ -type f -name 'seq\[[1-4]\].nex' -exec rm {} +
Some ancient versions of find don't support +
at the end of the -exec
predicate, so use \;
instead:
find simulations/ -type f -name 'seq\[[1-4]\].nex' -exec rm {} \;
The difference between the +
and the \;
is that with +
, find tries to fit as many filename arguments into the rm
command as it can (ARG_MAX again). With \;
, it runs rm
once per filename - this is obviously much slower, but it still useful (e.g. when you need to run a program that only takes one filename argument).
BTW, \;
isn't special to find
. In fact, find
itself uses just ;
as a terminator for -exec
. The \
is to make sure the shell passes the semi-colon on to find
, i.e. prevent the shell from interpreting the ;
as the end of the find
statement. shell statements are separated by newlines OR semi-colons.
+
isn't useful when there is only one filename as argument? :-D – Peregrino69 Mar 31 '23 at 13:53+
if you want to run, say, a shell script - e.g.find ... -exec sh -c 'for f; do something with "$f"; done' sh {} +
– cas Mar 31 '23 at 13:53find
is too old to support+
? – Peregrino69 Mar 31 '23 at 13:55