简体   繁体   中英

Linux alias question

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.

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.

You can't do that with an alias. You'll need to write a script.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM