How to Copy, Move and Rename Files & Folders Using SSH

In my previous article, I have explained “How to Create, Edit and Delete Files & Folders Using SSH”. Here, I am going to show you, how to copy/move files and folders from one place to another place, rename a file and folders using the SSH command.

SSH Command to Move Files & Folders:

The mv command is used to move the files or folders from one place to another place. Here is the example SSH commands to move the files and folders from one place to another place,

Command to move the file

mv MyFileOne.php directory_one

Command to move the folder

mv directory_one new/path/

The mv command has the following option,

  • -i: interactive. If the selected file already exists in the destination directory, it will prompt you to confirm whether to overwrite or not. This is a good option because it can help prevent you from making mistakes. To overwrite the file if that already exist in the destination directory, press Y and then Enter. If you don’t want to overwrite the file, press N and Enter.
  • -f: force. If the selected file already exists in the destination directory, it will overwrite without prompting.
  • -v: verbose. It will show a list of the files being moved.

SSH Command to Copy Files & Folders:

The cp command is used to copy the files and folders, which is similar to mv command, except that the mv command will physically move the files and folders from one place to another place and the cp command will make the duplicate of files and folders.

Command to copy the file

cp MyFileOne.php directory_one

Command to copy the folder

cp -r directory_one new/path/directory_one

The cp command has the following option,

  • -i: interactive. It will prompt you to confirm if the file is going to overwrite a file in your destination. This is similar to the -i option of the mv command.
  • -r: recursive. Rather than just copying all the files and directories, copies the whole directory tree, subdirectories and all, to another location.
  • -f: force. If the selected file already exists in the destination directory, it will overwrite without prompting.
  • -v: verbose. It will show the progress of the files being copied.

SSH Command to Rename Files & Folders:

When you copy or move files, you can also rename it. Here is the SSH command to rename the files and folders,

If you just want to rename the file, use the below SSH command,

mv MyFileOne.php MyFileTwo.php

The above command is used to rename the file from MyFileOne.php to MyFileTwo.php.

Use the below SSH command to copy and rename the file from MyFileOne.php to MyFileTwo.php,

cp MyFileOne.php MyFileTwo.php

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *