Copying Files and Copying Directories on Linux

29/12/2020
Linux is a place that can do amazing things when performing almost any task. For enjoying the full power of Linux, it’s always a good idea to have the knowledge of some basic tricks and command, right? Today, let’s have a look at the file copying command on Linux.

File copying

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!

Copying tricks

For copyping, we’ll be using “cp” command. This is the basic “cp” structure –

cp [parameter] “source_file_directory” “target_file_directory”

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.

cd ~/Downloads/testDir

# Copy all the available files to “~/Desktop/testDir1” directory

cp * ~/Desktop/testDir1

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).
cp -v * ~/Desktop/testDir1/

It’s recommended that you use these parameters most of the time for the best feedback during copying process.

cp -irv ~/Desktop/testDir1/

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 –

cp * sub/

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 –

cp -vr * sub/

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/”.

cp -vr ~/Downloads/ ~/Desktop/testDir1/

Hopefully, your copying experience with Linux has improved enough. Enjoy!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to Debug a Bash Script like a Boss

A lot of things can happen in a bash script that unless you know how to debug like a boss, you might as well be up a creek...
29/12/2020

How Does PATH Work in Bash

When you’re typing a command in the Linux terminal, you’re generally calling a program to do a certain job, for example,...
29/12/2020

String concatenation in bash

The way of joining two or more strings together is called string concatenation. It is a common requirement of any programming...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024