Have you ever wondered how cinched it can be to copy and move Files & Directories in Ubuntu? In this era of technology, people don’t want to write commands rather they prefer GUI for that. But, there are certain times when GUI fails to load. At that instant, if you know how to play with the command-line Interface, then it is fabulous.
Through this tutorial, you will learn to Copy and Move Files & Directories in Ubuntu by performing the steps as explained below.
Steps to Copy and Move Files & Directories in Ubuntu
To copy a file within the same Directory
Step 1: In order to copy a content of one file to another within a same directory, simply write “sudo cp filename1 filename2”, sudo is used to have root permissions.
For an example, If want to copy a filename project.txt to another one i.e. project2.txt. So, in this case, the command will be “sudo cp project.txt project2.txt”.
Step 2: To list the changes use “ls” command and then press enter.
Step 3: You may have noticed that there is no output shown using the above command. To know which file/directory is copied, use the command “vi” in which“v” shows the output and “i” is for the interactive view.
To copy a file from another directory to a directory in which you’re currently in, use “cp -vi ~/Desktop/hello.txt ~/hello2.txt”. Here, I have copied “hello.txt” file stored under the “Desktop” directory, to a home directory, that’s why used “~” sign before giving the file a new name.
Step 4: As an output, the process is shown in the figure below.
Step 5: Use “ls” again to check whether the file is copied or not.
It’s done, check out in the figure below.
Step 6: Suppose, you had accidentally written a command, to copy the files and then you realized, no it’s not what you want to do. Therefore, to be on safer side, always use “-i”, which stands for interactive, that will ask you whether to overwrite the contents of the files or not.
Like, I wrote “cp -i project2.txt hello2.txt” and pressed enter.
Step 7: See in the figure, the bash shell asking to overwrite the contents of hello2.txt. Type “y” or “n”.
I typed “n” for no, as in the previous command hello2.txt is already copied & I don’t want to overwrite it.
To move a file:
Step 8: The “mv” command is used to move a file from a source to destination, whichever you specify. Meanwhile, used for renaming the file, if you have only mentioned a filename and, not a particular path. The shell will rename a file, in this case.
Let’s try it. Type “mv hello2.txt newhello.txt”. Hello2.txt file is already available in my home directory, that’s why tweaked with it. You can perform the same with the file you want.
To look at the changes, type“ls” command again. Now, you’ll see that “hello2.txt” is no more available,because it is now renamed as “newhello.txt”.
Step 9: To change the directory name, for an example: if you need to change the Music directory with another name then type“mv Music/ MusicExamples/” where “/” is explicitly used to denote that it is a directory.
Step 10: Always use “ls”command after performing a step for a clear view. You can see that the name changes to Music Examples.
To copy the sub-directories as well:
Step 11: Actually “cp” command doesn’t copy the sub-directories of a directory, if exits. For copying the sub-directories, we use recursive flag, use “-r” with “cp” command that will copy (recursively) all the sub-directories to the desired place.For that type: “cp -r MusicExamples/ Music/” and press enter.
The same can be applied within the files as well.
Step 12: To ensure both of the Directories, have the same content, in a single step. Execute the command “ls MusicExamples/ Music”, this command will show you the contents of both.
In this scenario, no file is shown as in my system there are no music files, within the directory.
To copy a Directory:
Step 13: I have created a sample directory named “friends” under the list directory which is actually located in my home directory. I am going to show you how to copy a directory from one place to another with the same content.
For that type: “cp -r ~/lists/friends ~/Public”, this command will copy all the content of a friends directory without leaving a bit of data to the “Public” directory. To deal with the Directories in Linux, you must use “-r”, otherwise the shell will throw an error called omitting directory.
Step 14: To check it out, type: “ls Public” command that will list the files and directories under the “Public”.
These are all the tweaks, you can perform in your system as well, to have a hands-on experience with it. It’s better to learn things practically, as if you would mistakenly insert/left a single white space may cause an error in the output.