Many days ago, I found this useful bash alias (contents of my ~/.bash_aliases)
# aliases
# finds temporary files ending with '~' and deletes them
alias rm~='find . -name '*~' -print0 | xargs -0 /bin/rm -f'
Now I tried to use rm~
after cd
to some directory containing 3 files ending in ~
I got this error in terminal, and files did not get deleted
find: paths must precede expression: 1n.in~
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
Also when I tried rm~
from /home/me
, it seems to be doing nothing. Or it might be taking a lot of time.
Please tell me why am I getting error, and how to fix it.
Thanks!
Best Answer
Your bash alias is not well written (the single quotes are not well used). Instead it should be:
Now, I personally don't like useless uses of pipes and
xargs
, so your alias would be better written as:The
-type f
option so as to find only files (not directories, links, etc.), the-v
option torm
so as to be verbose (prints out what it's deleting). The+
at the end so thatfind
runsrm
with all the found files (spawns only one instance ofrm
, instead of one per file).Now from
man bash
:Instead of an alias, it's better to use a function: comment your alias in the
.bash_aliases
file (i.e., put a#
in front of that line), and in the file.bashrc
, put this function (anywhere in the file, at the end is fine):Also, as the other answer mentions, you can use the
-delete
command to find. In this case, yourrm~
function will be:In fact, you can make a cool function that will take an argument, say
--dry-run
, that will only output what it will delete:Then use as:
to only show the files that will be deleted (but not delete them) and then
when you're happy with this.
Adapt and extend to your needs!
Note. You'll have to open a new terminal for the changes to take effect.