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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
panni
  • 369
  • 1
  • 4
  • 5

2 Answers2

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/ .

αғsнιη
  • 40,939
  • 15
  • 71
  • 114
  • it says no such file or directory – panni Dec 07 '14 at 08:22
  • Thank you. And can u help me with how to display the welcome message every time i log into unix? – panni Dec 07 '14 at 11:18
  • @panni Please ask that as new question. thank you. and please [read here](http://unix.stackexchange.com/help/accepted-answer) and don't forget accept answer. – αғsнιη Dec 07 '14 at 11:55
  • http://unix.stackexchange.com/questions/171938/how-to-display-welcome-message-in-unix – panni Dec 07 '14 at 12:25
  • @panni please [read here](http://unix.stackexchange.com/help/accepted-answer) and don't forget to mark as accept answer :) – αғsнιη Dec 09 '14 at 07: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.

jherran
  • 3,869
  • 3
  • 22
  • 34