Before we start the guide, it’s time for a short note on what Linux understands by telling a file or folder. In Linux, each and every folder is known as “directory”. A directory can contain other directories and files of any size given that the file size enough to fit in the storage device.
When you want to copy a file/folder, you have to clarify it enough to the system so that it doesn’t mess things up. It’s also a wonderful thing that whenever you copy/move file from one drive or another, you’ll still be putting them into a folder!
For copyping, we’ll be using “cp” command. This is the basic “cp” structure –
If you want to copy a file to another directory, you have to run the following commands. Note that I’m using
“~/Downloads/testDir/” with 3 test files as the demo for this guide.
# Copy all the available files to
Here, “cp” is the associated command for copying file from one directory to another. It’s just a short term of “copy”. There are some other available options like –
- -i – Interactive copy mode. If the program finds out any confliction (file already exists etc.), it will ask your action on the situation.
- -r – Recursive. This option will copy all the included files & directories to the destination. It will also preserve the tree structure of the source directory.
- -v – Verbose mode. This is useful if you want to get feedback that the copy task is ongoing well. For each question, there are 2 available answer – y (Yes) and n (No).
It’s recommended that you use these parameters most of the time for the best feedback during copying process.
Copying an entire directory
Now, let’s think of a situation when you need to copy all your files and directories (folders) into the destination directory. Maybe you’re thinking to use the same trick as above, right?
Here is a test run of the command where I’m trying to copy all the files and directories under “~/Downloads/” into a created subdirectory “sub/”. After running this command –
The result is this –
Horrific, right? Everything is alright and “cp” should have copied everything into that directory. What’s the problem?
The answer we already discussed above. Remember the “cp” parameter “-r”? It tells to perform the task recursively – copy all the sub-directories and files from the source to destination.
Let’s fix it right away! Run the fixed command –
Now, everything looks just fine and working.
An interesting thing to note that the destination sub-directory will also be copied within itself.
As you can see, everything of the “Downloads” directory including the “sub” sub-directory is inside the “sub” directory.
Just like that, if you want to copy an entire directory to another directory, use the “-r” parameter. For example, I’ll be copying “~/Downloads/” to “/Desktop/testDir1/”.
Hopefully, your copying experience with Linux has improved enough. Enjoy!