How to Move Files Using Terminal

In the previous section we learned how to duplicate and rename a file. In this Learn Terminal tutorial, we will learn how to move files from one folder to another.

To move a file from a folder to a different folder, you can do this by using the command mvjust like the command used to rename a file.

The reason why in the previous tutorial this command can also be used to rename a file is actually the command mv it creates a duplicate of a file with a new filename, and then deletes the original file.

That’s how commands work mv.

To move files using the command mv actually quite easy, almost the same as the command cp. Only on command cp the base file does not delete, whereas in the command mv the base file will be deleted.

For example you have a file named “footer.php” and a folder named “folder1” which has sub folders named “sub” as shown below:

Viewing Folder Contents Using Terminal
Viewing Folder Contents Using Terminal

You can move the file “footer.php” into a sub folders from “folder1” named “sub” using the following command:

mv footer.php folder1/sub

By running the command above, the file named “footer.php” will be moved to the “sub” folder in the “folder1” folder.

To make sure if the file has moved you can check it by using the command ls like the following picture:

How to Move a File Using Terminal
How to Move a File Using Terminal

Make sure the file that we moved previously has successfully moved to the “sub” folder as shown above.

Furthermore, if you want to restore the file from sub folders “sub” which is in the “folder1” folder to the folder where you are currently (originally), you can do this by using the command:

mv folder1/sub/footer.php .

Dot “.” in the command represents the folder where you are currently. Then check again whether the folder was successfully moved by using the command ls like the picture below:

How to Move Files to Current Folder
How to Move Files to Current Folder

Make sure the file named “footer.php” is back in your current folder as shown above. In the example above, the folder you are currently in is the Home directory.

Actually, you can also move a file directly to the Home directory in an easier way, you can do it from any directory, by using the worm flag (~) as a representation of the Home directory.

For example, in the “sub” folder which is a sub folder of “folder1” there is still a file called “footer.php” as before. You can move the file directly to the Home directory by using a command like the following:

mv folder1/sub/footer.php ~

The worm sign (~) in the command represents the Home directory.

Then check and make sure the “footer.php” file has successfully moved to that directory by using the command ls as previously.

That’s all the tutorial for Learning Terminal this time about how to move a file using command line interface on Linux. We will meet in the next section with a more exciting discussion.

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *