I want to copy a file to another disk, when completed delete it, and create a symlink back to it in the original location.

example: copyandlink.sh somefile.iso

set -e
cp $1 /media/storage/linked/ &&
rm $1 &&
ln -s /media/storage/linked/$1 ./

or something like that.

  • 6
    You do know that hard links cannot work across devices, don't you? – AlexP Jan 2 '17 at 0:39
  • Since the current question is impossible to answer, as pointed out by AlexP, you can either change it to use soft links or similar alternatives, or delete it. – Julie Pelletier Jan 2 '17 at 4:48
  • My mistake, confusing hardlinks and symlinks. – jakethedog Jan 3 '17 at 1:00
  • So what is the question that remains? – icarus Jan 3 '17 at 5:45
  • Maybe this is as good as it gets, but it is not very elegant. I thought there would be a better, more robust way. – jakethedog Jan 3 '17 at 6:10

You're on the right track since your script does exactly what you want.

It can be simplified and improved a little like:

mv $1 /media/storage/linked/ &&
ln -s /media/storage/linked/$1 .

The set -e has no impact since your commands are already conditional with the &&.

I also replaced the cp and rm for mv which handles both automatically, whether on the same partition or not.

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.