LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-10-2009, 08:25 PM   #1
dynamics
LQ Newbie
 
Registered: Aug 2009
Location: Guildford
Distribution: Ubuntu Jaunty
Posts: 12

Rep: Reputation: 0
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....
 
Old 09-10-2009, 09:32 PM   #2
adamben
Member
 
Registered: Feb 2007
Distribution: slackware,gentoo,ubuntu
Posts: 50

Rep: Reputation: 17
I believe you'll need to use a shell function instead of an alias to solve this problem.
 
Old 09-10-2009, 09:49 PM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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 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.

Last edited by GrapefruiTgirl; 09-10-2009 at 10:03 PM. Reason: edited unneeded extra quotes
 
Old 09-10-2009, 10:15 PM   #4
adamben
Member
 
Registered: Feb 2007
Distribution: slackware,gentoo,ubuntu
Posts: 50

Rep: Reputation: 17
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>

Last edited by adamben; 09-10-2009 at 10:25 PM.
 
Old 09-10-2009, 10:17 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
@ adamben -- nice example!

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

Sasha
 
Old 09-10-2009, 10:48 PM   #6
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
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.

-
 
Old 09-11-2009, 07:21 AM   #7
Marko Hyvärinen
LQ Newbie
 
Registered: May 2009
Posts: 24

Rep: Reputation: 15
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
 
Old 09-11-2009, 05:19 PM   #8
dynamics
LQ Newbie
 
Registered: Aug 2009
Location: Guildford
Distribution: Ubuntu Jaunty
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks :-)

Quote:
Originally Posted by GrapefruiTgirl View Post
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 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 :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you copy hidden files from one directory to another? Akhran Linux - Newbie 22 12-13-2019 06:20 PM
How to copy files after search in array from this directory to another directory! phanvinhgiap Programming 4 01-04-2009 07:48 PM
How to get name of files in local directory using C++ tnjones Programming 7 08-14-2008 11:24 PM
copy 3 files to a directory and all subdirectories? snip128 Linux - Newbie 9 08-12-2005 10:18 PM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 12:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration