LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How does Linux cp command determine the source and destination locations in command? (https://www.linuxquestions.org/questions/linux-general-1/how-does-linux-cp-command-determine-the-source-and-destination-locations-in-command-874993/)

linuxman2 04-14-2011 09:21 AM

How does Linux cp command determine the source and destination locations in command?
 
When you run the following cp command in the BASH terminal, how does Linux know which files are the source and which are the destination when copying multiple files from one location to another?

Example:
cp /etc/services /etc/motd /etc/fstab /etc/hosts /home/fred/my_dir

How does Linux know that the services, motd, fstab, and hosts files are the source and the /home/fred/my_dir is the destination?

This question came up in a Linux class and I was not sure of the answer. I was thinking it is based on the source path entered ending with a file path and the destination being a directory, but was not sure.

Thank you.

AlucardZero 04-14-2011 09:28 AM

Because that's how it was coded. There can be only one destination.

Code:

CP(1)                                    User Commands                                    CP(1)

NAME
      cp - copy files and directories

SYNOPSIS
      cp [OPTION]... [-T] SOURCE DEST
      cp [OPTION]... SOURCE... DIRECTORY
      cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
      Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.


TobiSGD 04-14-2011 09:48 AM

Quote:

Originally Posted by linuxman2 (Post 4324525)
I was thinking it is based on the source path entered ending with a file path and the destination being a directory, but was not sure.

At first, AlucardZero is totally right with his answer.
Just a side note, you can also copy multiple directories into one target directory. for example:
Code:

cp -r /etc /home /mnt/backup
which will copy the /etc and /home directories to a (previously mounted) backup directory.

MTK358 04-14-2011 11:28 AM

Quote:

Originally Posted by linuxman2 (Post 4324525)
When you run the following cp command in the BASH terminal, how does Linux know which files are the source and which are the destination when copying multiple files from one location to another?

The last file is the destination, all the files before that are the sources.

linuxman2 04-14-2011 07:13 PM

Thanks
 
Thanks for the quick reply.

That is what I thought but I just wanted to confirm it.


All times are GMT -5. The time now is 06:57 AM.