2

I want to copy a file from /home/e_empid/file1 to /home/e_empid /dir1/dir2/dir3/Backupfile1. User does not want to write a long command each time. Write a command to set an alias for above action?

I tried the below code

alias a= 'cd ~/e_empid/'
alias b= 'cd ~/e_empoid/dir1/dir2/dir3/'

I want to copy the file1 to backupfile1. How should I do it after I create an alias?

3

Change your aliases to variable like below without cd command and without space after = sign and remove last / because the aliases won't work inside cp command or you can use function instead; As @jherran's answer which it's more flexible with function.

sorc=~/e_empid
dest=~/e_empoid/dir1/dir2/dir3/Backupfile1

Then use that like:

cp $sorc/file1 $dest/

If you run above command, the file1 file will be copy from ~/e_empid directory into ~/e_empoid/dir1/dir2/dir3/Backupfile1 directory.

You can add them to end of .bashrc file to using for next time:

echo 'sorc=~/e_empid' >> .bashrc
echo 'dest=~/e_empoid/dir1/dir2/dir3/Backupfile1' >> .bashrc

and use cp $sorc/Any_File_inside_it $dest/ .

  • it says no such file or directory – panni Dec 7 '14 at 8:22
  • Thank you. And can u help me with how to display the welcome message every time i log into unix? – panni Dec 7 '14 at 11:18
  • @panni Please ask that as new question. thank you. and please read here and don't forget accept answer. – αғsнιη Dec 7 '14 at 11:55
  • @panni please read here and don't forget to mark as accept answer :) – αғsнιη Dec 9 '14 at 7:46
2

If you are using bash, you can create a function instead of an alias.

bcopy () {
    cd ~/e_empid/
    cp file1 /home/e_empid/dir1/dir2/dir3/Backupfile1
    cd ~/e_empoid/dir1/dir2/dir3/
}

Then you just need to call using bcopy.

You could also use $1 and $2 instead of file1 and Backupfile1 if your files does not have the same names every time.

cp $1 /home/e_empid/dir1/dir2/dir3/$2

In this case, you need to call using bcopy orig-file dest-file.

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.