Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 25 down vote accepted

Your bash alias is not well written (the single quotes are not well used). Instead it should be:

alias rm~='find . -name "*~" -print0 | xargs -0 /bin/rm -f'

Now, I personally don't like useless uses of pipes and xargs, so your alias would be better written as:

alias rm~='find . -name "*~" -type f -exec /bin/rm -fv -- {} +'

The -type f option so as to find only files (not directories, links, etc.), the -v option to rm so as to be verbose (prints out what it's deleting). The + at the end so that find runs rm with all the found files (spawns only one instance of rm, instead of one per file).

Now from man bash:

For almost every purpose, aliases are superseded by shell functions.

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):

rm~() {
    find . -name "*~" -type f -exec /bin/rm -fv -- {} +
}

Also, as the other answer mentions, you can use the -delete command to find. In this case, your rm~ function will be:

rm~() {
    find . -name "*~" -type f -printf "Removing file %p\n" -delete
}

In fact, you can make a cool function that will take an argument, say --dry-run, that will only output what it will delete:

rm~() {
    case "$1" in
    "--dry-run")
        find . -name "*~" -type f -printf "[dry-run] Removing file %p\n"
        ;;
    "")
        find . -name "*~" -type f -printf "Removing file %p\n" -delete
        ;;
    *)
        echo "Unsupported option \`$1'. Did you mean --dry-run?"
        ;;
    esac
}

Then use as:

rm~ --dry-run

to only show the files that will be deleted (but not delete them) and then

rm~

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.

share|improve this answer
    
Thanks! It works nicely, and dry run is extremely helpful. –  Vinayak Garg Dec 28 '12 at 10:20
2  
find -type f -name '*~' -delete (weapon of choice, besides git clean -dfx .) –  sehe Dec 28 '12 at 11:59
2  
This is such an elegant solution, I had to login just to give you props. +1 to you, my good fellow! –  CodeChimp Dec 28 '12 at 13:47

*~ gets expanded by the shell before it gets assigned to your alias. The actual assignment is:

alias rm~='find .name some~ file~ 1n.in~ -print0 | xargs -0 /bin/rm -f'

I suggest to use functions instead of aliases, these are much more powerful and easier to handle in regard with quotes.

While we are at it, remove the superfluous . (current directory is implied if no parameters are given) and stop abusing xargs since a -delete option already exist.

rm~() { find -name '*~' -ls -delete; }

The -ls option is optional, but adding it will show you which files have been deleted.

share|improve this answer
1  
+1 for mentioning -delete –  don.joey Dec 28 '12 at 9:17
    
+1 Thanks! But where do I add this function? –  Vinayak Garg Dec 28 '12 at 9:21
    
@VinayakGarg You can add it to your .bash_aliases too, but I usually put them straight in my .bashrc. –  Lekensteyn Dec 28 '12 at 10:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.