1

I have a scenario where I have a script that monitors a file location and that file location has files copied to it, i.e. /home/matt/thefile.

I want an alias that if I do place myfile.txt it will overwrite /home/matt/thefile with myfile.txt.

  • 2
    What do you mean by saying “copy it on top” – should thefile be overwritten or the content of myfile.txt attached to its end? – dessert Sep 18 '17 at 19:10
  • It should be overwritten. – NolanPower Sep 18 '17 at 19:14
  • The monitor script is already running and working. It's just that a lot of people are using it and copying files to the target directory that it would be really useful to have an alias for it. – NolanPower Sep 18 '17 at 19:28
3

A function would be more appropriate.

In Bourne-like shells:

place() { cp -- "$1" /home/matt/thefile; }

In shells other than bash, yash and posh you can simplify it to:

place() cp -- "$1" /home/matt/thefile

In fish:

function place
  cp -- $argv[1] /home/matt/thefile
end

in rc/es:

fn place {
  cp -- $1 /home/matt/the/file
}

It's in (t)csh that you'd need to use an alias as those shells don't have functions (it's also csh that introduced aliases in the first place). In (t)csh you can use history substitution to allow some kind of argument passing to aliases.

alias place 'cp -- \!:1 /home/matt/the/file'

When called as place myfile.txt, they would copy myfile.txt to ~/thefile

If you wanted something that works regardless of the shell of the user, rather than having them add a shell-specific function/alias to their shell customization file, you'd create a script which you'd add in a directory that is in their command search path. Something like:

#! /bin/sh -
exec cp -- "${1?Please give the file to copy as argument}" /home/matt/thefile

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.