You are not logged in.

#1 2008-12-16 07:17:48

colbert
Member
Registered: 2007-12-16
Posts: 809

[SOLVED] Trying to make command into bash alias

Here is a command I would like to make into an easy to use bash alias:

#Copy results of a find to somewhere
find -iname "*something*" -exec cp '{}' /path/to/copy/results/to \;

I realize there are usually several ways to do something in the shell, so if a better way of course I'm all for any tips but if I wanted to make this an alias so that I just had to do:

alias pattern pathtocopyto

How would I? I have tried:

alias findcp="find -iname "$1" -exec cp '{}' $2/ \;"

But this does not work, says it cannot find the search term when I ran it in a test dir full of properly named files and dirs.

TIA for any help smile

Last edited by colbert (2008-12-17 23:19:09)

Offline

#2 2008-12-16 07:26:13

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: [SOLVED] Trying to make command into bash alias

Of course it does not wink. Your definition breaks at the second double quote.

This is how it should be:

alias findcp="find -iname \"$1\" -exec cp '{}' $2/ \;"

Then source your .bashrc (or log out and in again) and run alias to see if it's parsed correctly.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#3 2008-12-16 13:51:44

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] Trying to make command into bash alias

That's not correct, before defining this alias $1 and $2 are expanded.

aliases can only put arguments at the back. For this construct you will need to use a function

function findcp() { find -iname "$1" -exec cp '{}' $2/ \; ;}

Offline

#4 2008-12-17 04:43:12

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: [SOLVED] Trying to make command into bash alias

Hmm, I tried that Procyon, I could see the HD light on the case go solid so it was running the find but it did not work. I have a file called "layla.mp3" and a dir called "test" in the current dir, ran it like this:

findcp layla test

Gave no result and test was still empty sad Thanks for the help guys hopefully can get this to go..

Offline

#5 2008-12-17 08:44:03

syntaxerrormmm
Member
From: Italy
Registered: 2008-10-22
Posts: 80
Website

Re: [SOLVED] Trying to make command into bash alias

That's clear. Check find manpage.

findcp "layla.*" test

should work.

Last edited by syntaxerrormmm (2008-12-17 08:44:21)


syntaxerrormmm - Homepage

Offline

#6 2008-12-17 16:23:45

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: [SOLVED] Trying to make command into bash alias

Procyon wrote:

That's not correct, before defining this alias $1 and $2 are expanded.

I cannot tell... I have no aliases defined with other aliases embedded. I guess I should have tried roll


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#7 2008-12-17 23:18:57

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: [SOLVED] Trying to make command into bash alias

Thanks! That worked syntaxerrormmm smile

Offline

Board footer

Powered by FluxBB