How to copy a file to a directory?

Hello all,

I've been researching this problem for days, and have gotten no luck . =/

How do you copy a file to another directory without being in the same directory as the file? So, for example, say I wanted to copy the file 'my.txt' that is in the directory ' /export/hom0/user/asdf ' to the directory ' /export/hom0/user/abcd '. without actually being in the directory ' /asdf '. I know this sounds a little confusing, but it has something to do with the 'cp' command.

Thanks for your help in advance.

Go through these two links:
cp: Copy
cd: Change Directory

Compare, then you tell us how it should be done
Clue:

Unless Im unlucky I doubt that directory exist, why?
When you do a ls -al in a subdir you see usually the first three lines like:

total 264
drwxrwxr-x   3 root       bin           2048 Jun 22 12:47 .
dr-xr-x--x  14 root       bin           1024 Aug 26 10:51 ..

Does . , .. not mean anything to you?

Good luck

Hi
Give the complete path while copying
instead of giving simply myfile.txt
give

cp myfile.txt /export/hom0/user/abcd/myfile.txt

A file xxx.yyy is called an entry name, because it goes in an entry of a parent directory. It has a full or absolute path of: $PWD/xxx.yyy

A file zzz/xxx.yyy is called a relative path, because it has directories but is still relative to the current working dir $PWD that the process is sitting in: $PWD/zzz/xxx.yyy, which is a blessing or curse depending on what you need. It is great for portability between users or environments like developement, test, production on the same host.

Any file path starting in / is absolute or full. Using them means that what happens is not dependent on $PWD, which is again a blessing or curse depending on what you need. Only one file or device can have that path.

I like you answer very much! You beat me here...
I tried hard not to give a straight solution not knowing if this (looks very much like) is homework but to help the person to search using some deduction... Your explanation on PWD is quite challenging for someone wanting effortless solution to be given...

The solution is the carrot on the string that the mule rarely gets?

You can't have the conversation if they have no cultural referrant?

I stifled myself before getting to inodes, links, file systems, sym-links, device inodes, device and inode numbers, lost+found, swap, /tmp, mmap(). :smiley:

I am intrigued that nobody has posted a correct solution or a correct hint so far.

Hint: You are allowed to use the absolute path name to a file in a unix "cp" command in either the source filename or the destination filename ... or both.

This will work anywhere, and ensures that the target is an existing directory, by globbing with a trailing slash. If the target was missing or a file, it would be created or overwritten, respectively. The scp/scp2 commands have a -d option to demand that the target be a directory, for safety.

cp /export/hom0/user/asdf/myfile.txt /export/hom0/user/abcd/

add a bit of fun :smiley:

myfile=/export/hom0/user/asdf/myfile.txt 

cp $myfile /export/hom0/user/abcd/${myfile##/*/}

Now that the solution is out, I'd have gone for:

cp -p /export/hom0/user/asdf/myfile.txt /export/hom0/user/abcd