Linux alias question

此生再无相见时 提交于 2019-12-25 03:36:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!