How To Copy Files In MacOSX Terminal?

MacOSX provides the Terminal in order to execute commands. The cp command is used to copy files in the MacOSX terminal in different ways. A single file or multiple files can be copied to the specified destination path using cp command options.

Copy Single File

The cp command can be used to copy single file. The current file and destination path is provided as parameters to the cp command. In the following example we copy the file named database.txt into the /var/db path.

cp database.txt /var/db

Also the copied file name can be changed into different name than the original or source file. In the following example we set the new name as mydatabase.txt .

cp database.txt /var/db/mydatabase.txt

The source or original file can be also specified as complete or absolute path. In the following example we copy the file database.txt which is located in the /home/ismail/ is copied to the /var/db/ with a new name mydatabase.txt .

cp /home/ismail/database.txt /var/db/mydatabase.txt

Copy Multiple Files

The cp command can be also used to copy multiple source files to the specified destination. The source files are provided after the cp command by separating them spaces. The last parameter is the destination path where the source files are copied. In the following example, we copy the source files named database.txt , names.txt and sources.txt into the path /var/backup .

cp database.txt names.txt sources.txt /var/backup/

Copy Folder Recursively

Single folder may contain multiple files which should be copied to the specified destination. By default the cp command can not copy a folder with files but the -R option can be used to copy specified folders recursively.

cp -R /home/ismail/db/ /var/database/

Copy Files To/From Remote Systems via Network

The MacOSX also provides the scp command in order to copy files from/to remote systems by using the network. The scp command is fork of the cp command which is named as Secure cp . The scp command requires authentication for remote systems. In the following example we copy a local file named database.txt to the remote system 192.168.1.10 .

scp /home/ismail/database.txt [email protected]:/home/john/

Alternatively a file from the remote system can be copied by using the scp command. In the following example we copy the file named database.txt into the local system.

scp [email protected]:/home/john/ /home/ismail/database.txt

Leave a Comment