Let's say I have two files in /tmp
:
root@ubuntu:~# touch /tmp/hello.{pyc,py}
root@ubuntu:~# ls /tmp/
hello.py hello.pyc
Now, lets run rm -rf
command with [co] option
root@ubuntu:~# rm -rf /tmp/hello.py[co]
root@ubuntu:~# ls /tmp/
hello.py
Can someone please explain what's happening here? What is the [co]
parameter? How can we make it work for other extensions? Say I have foo.js
and foo.coffee
files, can we do something like rm -rf /tmp/foo.coffe[co]
to delete the /tmp/foo.js
?
[co]
isn't a parameter to therm
command - it's a shell glob that matches a pattern equal to a single character from the set[co]
- in other words, it matches either ac
or ano
a the end of the filename. Fromman bash
:To match both
foo.coffee
andfoo.js
, since the suffixes don't contain any common substrings at all, the best you could do isfoo.*
which would match any filename starting withfoo.
Instead you could use brace expansion e.g.It is not a parameter but a collection of letters (or a "shell glob"). This is the same:
is the same as
Similar ...
would delete anything from
/tmp/hello.pyc
up to and including/tmp/hello.pyo
following ASCII ordering.would remove ...
You can make rather fancy methods but for those 2 files I'd just remove them with 1 command for each. Another example getting as close as possible to those 2 files...
would remove files like this ...
so it would include far more than just these 2 files.
It's a shell glob, similar to wildcards
*
and?
...in bash, the filename pattern*.[co]
matches all filenames that finish with.c
or.o
.The difference with
*.[co]
versus*.c *.o
or*.{c,o}
is that the two latter patterns will expand to a dummy*.o
if no.o
files exist in the directory, while the*.[co]
version won't.Shell globs are useful for doing file operations in a non-case-sensitive way. For example, if you have a bunch of files with filenames that end with
jpg
,JPG
,Jpg
,JPg
, etc... and you want to remove all of them, you can do: