How to move files and folders via SSH

The command mv can be used for moving files and folders through SSH. To move a file type the command followed by the path to the file (the source path) and then the new path for the file (the destination path). For example:

mv /home/username/public_html/files/example.txt /home/username/public_html/example.txt

will move the file example.txt from a folder called files to the public_html folder. Of course, username has to be replaced with the actual master username. When you type the command separate the two paths with a single space, and also add the extension type to the file (.txt in our example). If the name of a file or folder has spaces in it (in any of the two paths), put quotation marks around it. If you don't want to change the name of the file that you're moving, you don't have to type its name in the destination path. Just include the destination folder (e.g. /home/username/public_html in our example above). Otherwise, you can simultaneously move and rename the file. To do this just change the name of the destination file.

If you want to move a file that's in your current working directory, you don't have to use the full source path but just the name of the file. If we use the above example and the current working directory is /home/username/public_html/files (and we don't want to change the name of the file), then we can use the command:

mv example.txt /home/username/public_html

In this case you can also move several files from the current working directory to the new destination folder. Just list the files followed by the destination path (e.g. mv example.txt example2.txt /home/username/public_html).

You also don't have to type the full destination path, if it's your current working directory. If we use the same example and the current working directory is /home/username/public_html, then we can use the command:

mv /home/username/public_html/files/example.txt example.txt

And since the source path is actually a subfolder of the destination path (our current working directory), we can simplify the command further:

mv files/example.txt example.txt

The things that we've mentioned so far also apply when moving a whole folder with everything inside it.

Was this answer helpful?

 Print this Article

Also Read