Domanda

I frequently copy to a specific directory on my computer. I'm trying to find a way to alias this command in my bashrc. That is, I'd like to define an alias like "cpdraft" and be able to type something like "cpdraft ./Draft.md". Here, cpdraft means something like "cp - dir". In this case, "-" is replaced with "./Draft.md". Is anyone familiar with this kind of alias? In other words, I want to replace an intermediate argument in the command "cp" with a desired input, instead of the usual rightmost one.

È stato utile?

Soluzione

This can be done using a bash function. Add this function to your .bashrc file and source it. e.g. source .bashrc. Note the function below copies to Draft.md directory from HOME directory.

function cpdraft()
{
    cp "$1" ~/Draft.md
}

Your requirements may be to copy to a ./Draft.md in whatever working directory in which case I would suggest always creating the Draft.md directory first if it does not exist:

function cpdraft()
{
    [ -d ./Draft.md ] || mkdir ./Draft.md
    cp "$1" ./Draft.md
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top