LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to Copy files from local directory to a particular directory using alias (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-copy-files-from-local-directory-to-a-particular-directory-using-alias-754282/)

dynamics 09-10-2009 07:25 PM

How to Copy files from local directory to a particular directory using alias
 
Hi

I want to copy files and folders from my current directory to a particular directory say target. How can I accomplish this using aliases?

Example:

The file to be copied is at this location
~/dir1/dir2/dir3/myfile.txt

The target directory where I want to copy the file is
~/fol1/fol2/fol3/target_dir

I know how to do it using this command assuming that my current location is dir3

$cp myfile.txt ~/fol1/fol2/fol3/target_dir/.

What I want is something like this

$cp myfile.txt target_dir

What I tried
in .bashrc I created this alias
alias cpt='cp $1 ~/fol1/fol2/fol3/target_dir'

then in the current folder I run
$cpt myfile.txt

But this is not working. I tried replacing the single quotes ' with double ones ". No use.

How can I do this?

Thanks in advance....

adamben 09-10-2009 08:32 PM

I believe you'll need to use a shell function instead of an alias to solve this problem.

GrapefruiTgirl 09-10-2009 08:49 PM

adamben's idea to use a shell function does look to be the better choice for that situation to me too. You could define it right there in the shell, or add it to your ~/.bashrc or ~/.bash_profile or whatever file is the appropriate one on your system.

However, to address at least one angle of your current approach:

When using your new alias, you don't use it like a variable, with the $ sign in front. The command you made would simply be cpt. (err :scratch: is that $ just indicating a shell prompt? Heh, if so, ignore this line :) )

Sasha

PS - this works:

alias cpt="cp $1 -t ./test"

where test is the name of the destination directory.

adamben 09-10-2009 09:15 PM

Where you specify folders as well, the function I mentioned earlier is likely still your best bet... here is an example...

CORRECTION - adding a -a flag to the copy command in addition to the -t target mentioned above will probably do this. My apologies :) - should have verified on the cp man page first.

<code>
function copyme {
if [ -z "$1" ]; then
echo "Only You Have The Force...er Source Rather"
fi
DESTINATION="/var/tmp/";
while( [ ! -z "$1" ] );
do
if [ -d "$1" ]; then
echo "Copying Directory $1 Recursively to $DESTINATION ..."
cp -r "$1" "$DESTINATION" && echo "DONE" || echo "FAILED"
elif [ -r "$1" ]; then
echo "Copying File $1 To $DESTINATION ..."
cp "$1" "$DESTINATION" && echo "DONE" || echo "FAILED"
else
echo "I Can't Read, Let Alone Copy!";
fi
shift
done
}
</code>

GrapefruiTgirl 09-10-2009 09:17 PM

@ adamben -- nice example!

For your info, if you use [square brackets] around your code tags, the result will be much better ;)

Sasha

tonyfreeman 09-10-2009 09:48 PM

copy using alias for destination folder
 
How about this:

Code:

TARGET=~/temp
cp myfile.txt $TARGET


Of course ~/temp needs to actually exist as a directory in your home directory.

-

Marko Hyvärinen 09-11-2009 06:21 AM

or this:

make script for shell, put in
vi /bin/myarch (for example)

#!/bin/bash
dst=/folder (put your own)
cp -a $1 $dst
<press esc>
:wq
chmod +x /bin/myarch

usage:
myarch test.txt


Marko

dynamics 09-11-2009 04:19 PM

Thanks :-)
 
Quote:

Originally Posted by GrapefruiTgirl (Post 3678115)
adamben's idea to use a shell function does look to be the better choice for that situation to me too. You could define it right there in the shell, or add it to your ~/.bashrc or ~/.bash_profile or whatever file is the appropriate one on your system.

However, to address at least one angle of your current approach:

When using your new alias, you don't use it like a variable, with the $ sign in front. The command you made would simply be cpt. (err :scratch: is that $ just indicating a shell prompt? Heh, if so, ignore this line :) )

Sasha

PS - this works:

alias cpt="cp $1 -t ./test"

where test is the name of the destination directory.

Hi friends,

I tried GrapefruiTgirl's suggestion because it was the simplest to try out first :-)
And the good news is it did work the way I wanted. When I want to copy a folder I use

alias cpt='cp $1 -r -t ~/target_folder'

Note that the single quote worked for me.
This will save a lot of time (and work) for me.

I will try all other options too including how to use shell function to accomplish this.

Thanks to everyone :-)


All times are GMT -5. The time now is 03:19 AM.