How to Copy and Rename Files Using Terminal

How to Copy and Rename Files Using Terminal

Previously we have learned how to delete a file and folder using command line interface or Terminal on Linux, before that we have also learned how to create a new file.

In this Terminal Learning section we will learn how to duplicate a file (copy) and rename a file using command line interface or Terminal on Linux.

First, we will learn first how to duplicate or copy a file using command line interface or Terminals.

How to Copy Files Using Terminal – cp

To duplicate a file using command line interfaceyou can do it by using the command cp.

It’s easy, you just need to select a file that you want to duplicate by typing it after the cp command, then give a name for the file that will be the result copy afterwards.

Say you have a file called “header.php” and you want to duplicate it and the resulting file copyit has the name “footer.php”, you can do this by using the command:

cp header.php footer.php

Then a duplicate file will be created from “header.php” named “footer.php” which has the same contents. To check whether the “footer.php” file has been created, you can use the command ls like the following picture:

How to Copy or Duplicate Files Using Terminal
How to Copy or Duplicate Files Using Terminal

In addition to duplicating files that are in the same folder. You can also duplicate files to different folders. For example, you have a folder called “folder1” in which there is a file called “index.php”.

Then you also have another folder named “folder2”. You can duplicate orcopy file “index.php” from “folder1” to “folder2” using the following command:

cp folder1/index.pxp folder2

To check whether the file has been successfully downloaded,copy to “folder2”, you can check it by using the command ls like the picture below:

How to Copy Files to Another Folder Using Terminal
How to Copy Files to Another Folder Using Terminal

If in “folder2” there is already an “index.php” file, it means that you have succeeded in duplicating or re-editingcopy files using command line interface.

How to Change File Names Using Terminal – mv

To rename a file using command line interface or Terminal on Linux, you can use the command mv.

For example, you have a file called “header.php”, then you want to change the name of the file to “page.php”, you can do this by using the command:

mv header.php page.php

Then the file that previously had the name “header.php” will change its name to “page.php”.

To be sure, you can do this by using the command lsas shown below:

How to Rename Files Using Terminal
How to Rename Files Using Terminal

Make sure in that folder you no longer see a file called “single.php” and have a new file named “page.php”. If there is, it means that you have successfully renamed a file using command line interface.

That’s the tutorial Learn Terminal in this section, in the next section we will learn how to move a file using command line interface or Terminal on Linux.

Tinggalkan Balasan

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