So, I got this question recently from a new Linux user, but I’m pretty sure even people who’ve been using Linux for some time will have a question or two regarding copying files on the command line.
Unix, and by extension, Linux isn’t known to be very chatty. Most commands are two or at the most three characters long with a hundred options, so a lot of people who’ve come to Linux by way of easy to use distributions such as Ubuntu or Mint have never really used the command line to get done work. I, on the other hand, am an old Linux hand and, frankly, am the most comfortable on the command line.
So here are the instructions on how to use the ‘cp’ command to copy a file or a folder from one location to another.
Copying a file
user$ cp file1 /path/for/file2
This command will copy the file named file1 with a new name, file2, to the folder at /path/for.
Copying a directory
Not surprisingly, the same cp command in Unix can also be used to copy directories from one location to another.
user$ cp -r directory1 /path/for
Notice the ‘-r‘ ? That’s the option for recursive copying. So, everything in the folder directory1 is copied to /path/for and the directory structure is kept intact.
Copying over a network
user$ scp filename [email protected]:/tmp
This command uses the SSH protocol to copy a file name filename securely from your local machine to the remote server named remoteserver.com. It uses the root user account to copy files to the /tmp directory of the remote server. Of course, you will be prompted to enter a password for the root user.
Did this article clarify things for you ? Or are the terse Unix commands still a mystery?