问题
I want to create an alias for this command "rmi" so that if I execute
rmi File.txt
it will actually execute
ls * | grep -v File.txt | xargs rm -rf
Basically I want to reorder arguments.
回答1:
Script:
#!/usr/bin/env bash
ls * | grep -v $1 | xargs rm -rf
-Save this as rmi.
-do: chmod a+x rmi
-Then add to path.
回答2:
You can't do that with an alias. You'll need to write a script.
回答3:
You don't need a script. Instead of alias, you can write a shell function:
myfunc() {
ls * | grep -v $1 | xargs rm -rf
}
# usage: myfunc <filename>
store it in ~/.bashrc or ~/.zshrc, or a separate aliases file, eg. using the idiom
test -f ~/.zaliases && source ~/.zaliases
in your dotrc file.
回答4:
thanks for clarifying this. In tcsh it's easy:
alias rmi 'ls * | grep -v \! | xargs rm -rf'
this should do it...
\!
expands all arguments following "rmi"
you could also use "find" to do this..
find . -type f | grep -v \! | xargs rm -rf'
... be careful with that axe! (rm -rf)
来源:https://stackoverflow.com/questions/5600899/linux-alias-question