0

I know questions regarding this error have been asked before - but I can't seem to figure out why this isn't working in this case: for aliases.

I have the following

alias scpip='scp $1 user@ip.edu:~'

I want the use case to be scpip file so that it copies the file into user's home directory in ip.edu. Instead I get the error

scp: /home/user: not a regular file

It works if I do it 'manually' ie scp file user@ip.edu:~ . How can I make this work as an alias with an argument?

  • 3
    shell aliases don't have arguments; the $1 in your alias expands to the first arg (if it exists) of the script in which you invoke it, or if used interactively (as aliases usually are) to nothing. It is followed by any arguments given in the command. Thus you are actually running scp user@ip.edu:~ file which tries to copy the remote directory to a local directory confusingly named file, but fails. You may want a function or script instead. – dave_thompson_085 Apr 06 '22 at 02:09
  • 2
    An alias replaces text with text, there is no logic. Use a function instead. And quote right when defining the function. And then quote even more right at runtime because scp is so cumbersome it requires you to quote for the remote shell (see this answer from where it states "better don't use scp at all"). – Kamil Maciorowski Apr 06 '22 at 14:02

1 Answers1

1

If when running the command, the first argument or $1 is a directory, which it looks like it is, then you'll need to use the -r switch in order to copy it whether it contains data or not. It can be run as an alias but it's more complicated and not worth the effort. It's better to just create a script.

Create a file called scpip in /home/directory

Have the contents as:

scp -r $1 user@ip.edu:~

Add the execute bit:

chmod u+x /home/directory/scpip

In your ~/.bashrc, add the line

export PATH=/home/directory:$PATH

Source the init:

. ~/.bashrc

Lastly, run the script with:

scpip file

That will use the first argument. If it's a file,

scp -r file user@ip.edu:~

If a directory

scp -r directory user@ip.edu:~
Nasir Riley
  • 11,422