How to Move and Copy Files via SSH (2024)

Often you will need to move one or more files/folders or copy them to a different location. You can do so using an SSH connection. The commands which you would need to use are mv (short from move) and cp (short from copy).

The mv command syntax looks like this:

mv original_file new_name

By executing the above command you will move (rename) the file original_file to new_name.

You can also use mv to move a whole directory and its content:

mv includes/* ./

This will move all files (and folders) from the includes/ directory to the current working directory.

In some cases, however, you will need to only update and move only files that were changed, which you can do by passing -u as an argument to the command:

mv -u includes/* admin/includes

The copy (cp) command works the same way as mv, but instead of moving the files/folders it copies them. For example:

cp original_file new_file

The command will copy the original_file file to new_file and will preserve the original one (the file will NOT be removed after it is copied).

cp also accepts various arguments:

cp -R includes/ includes_backup/

-R instructs cp to copy files recursively (for example, a whole directory). To overwrite already existing files you should use the -f argument:

cp -Rf includes/ admin/includes/

A more convenient way to copy files/folders is to use a 3rd party application, such as Midnight Commander. All our servers have mc installed and it is available by executing the mc command using the SSH Shell. Once inside the application you will see two sections – left and right. You can easily copy/move files from the left side directory to the right side using a semi-visual approach. You can even use your mouse to select files and function keys to execute commands.

You can see a picture of it below:

How to Move and Copy Files via SSH (1)

As you can see on the screenshot, there are numbers from 1 to 10 at the bottom of the console screen. These represent shortcuts to certain commands and are activated using the corresponding function key (i.e F1 for help, F5 to copy, etc).

How to Move and Copy Files via SSH (2024)

FAQs

How to Move and Copy Files via SSH? ›

The commands which you would need to use are mv (short from move) and cp (short from copy). By executing the above command you will move (rename) the file original_file to new_name. This will move all files (and folders) from the includes/ directory to the current working directory.

How to move a file in SSH? ›

How to move and copy files using SSH?
  1. mv index.html /path/to/directory/index.html ( this command will move it)
  2. cp index.html /path/to/directory/index.html (this command will copy it) Copying a whole folder is done using the -r option of the cp command. ...
  3. cp -r /path/to/folder /destination/path. ...
  4. mv index.html newindex.html.

Which of the following SSH command is used to copy a file? ›

The scp command uses ssh for data transfer.

What is the cp command in SSH? ›

cp Command

If you provide two file names, the cp command will copy the content of the source file into the destination file. Thus, the destination file will be overwritten without any warning. However, if the destination file doesn't exist, then the command will create a new file.

How to scp across SSH? ›

You can copy all files in a remote directory in a way like this: [pineehad@localhost ~]$ scp yourusername@yourserver:/home/yourusername/* . And you can also just copy a whole directory by specifying the -r (recursive) option: [pineehad@localhost ~]$ scp -r yourusername@yourserver:/home/yourusername/ .

How to copy a folder remotely via SSH? ›

  1. Step 1 – Connection. First, enable SSH access and connect to your account using SSH. Open the directory, where the files and folders, which you want to copy, are located. ...
  2. Step 2 – Copying. You only need one command to copy files or folders, just chose the option the most suitable for you: Make a copy of the file:

How do you move a file in command-line? ›

To move files, use the mv command (man mv), which is similar to the cp command, except that with mv the file is physically moved from one place to another, instead of being duplicated, as with cp.

Can I copy files using SSH? ›

A client can use an SCP to upload files to a remote server safely, download files, or even transfer files via SSH across remote servers.

How does SSH copy work? ›

ssh-copy-id is a useful tool for SSH connections to a remote host without using a password. Basically, it copies the SSH key into the remote host's authorized_keys file, which is by default in the $HOME/. ssh directory. In this tutorial, we'll discuss how to automate the usage of ssh-copy-id.

Which command is used to copy files? ›

Use the cp command to create a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parameters into the file or directory specified by the TargetFile or TargetDirectory parameters.

How do I copy from SSH? ›

Copying files over SSH can be achieved using the scp command line that allows remote server authentication with username-password or an SSH key. One of the most confusing things using this command is understanding which server is local or remote, so pay attention to that and avoid copying a file in the wrong path.

How to use SSH command? ›

You can start an SSH session in your command prompt by executing ssh user@machine and you will be prompted to enter your password. You can create a Windows Terminal profile that does this on startup by adding the commandline setting to a profile in your settings.json file inside the list of profile objects.

How to copy files from one directory to another in terminal? ›

The cp command also copies entire directories into other directories if you specify the -r or -R flags. You can also copy special-device files using the -R flag. Specifying -R causes the special files to be re-created under the new path name.

How to copy a directory from SSH to local? ›

The commands which you would need to use are mv (short from move) and cp (short from copy). By executing the above command you will move (rename) the file original_file to new_name. This will move all files (and folders) from the includes/ directory to the current working directory.

How to SFTP via SSH? ›

How to Connect Using SFTP?
  1. Check your SSH access using one of these commands: ssh user@server_ipaddress ssh user@remotehost_domainname.
  2. Once that is done, leave the session if no errors occurred.
  3. Initiate an SFTP connection with the following commands: sftp user@server_ipaddress sftp user@remotehost_domainname.
Mar 20, 2024

Does SCP use SSH to transfer files? ›

The scp command uses SSH to transfer data, so it requires a password or passphrase for authentication. Unlike rcp or FTP, scp encrypts both the file and any passwords exchanged so that anyone snooping on the network cannot view them.

How to move a file from local to server using SSH? ›

4 Ways to Transfer Files Between Remote and Local Systems Over SSH
  1. scp: Legacy command which is being deprecated.
  2. rsync: Popular command for file synchronization.
  3. sshfs: Mounting remote directory over SSH.
  4. sftp clients: GUI tool for accessing file over SFTP.

How to change SSH file location? ›

ssh directory, you can edit the below settings:
  1. /etc/ssh/ssh_config: Edit the value for IdentityFile . A sample entry would look like this: IdentityFile /es2/ssh/id_rsa . ...
  2. /etc/ssh/sshd_config: Update the value for AuthorizedKeysFile , A sample entry would look like this: AuthorizedKeysFile /es2/ssh/authorized_keys.
Jun 5, 2023

How to move a file in Putty? ›

  1. To move and rename a file, use the command “mv filename. extension /dir/filename1. extension”.
  2. You can also move your file one folder back by using the command “mv filename/ ..”.

How do I move a file in Shell? ›

The shell command intended for moving files on Linux, BSD, Illumos, Solaris, and MacOS is mv. A simple command with a predictable syntax, mv <source> <destination> moves a source file to the specified destination, each defined by either an absolute or relative file path.

Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5988

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.