Copying Files and Copying Directories on Linux

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

Bash jq command

JSON data are used for various purposes. But JSON data can’t be read easily from JSON file by using bash script like...

How to check the variable is set or empty in bash

A variable can be defined or undefined. When any variable is not declared or declared but no value is assigned then the...

Bash builtin examples

builtin candidate_builtin arg … in bash allows you to only call bash builtins. That is, even if an external command or...
Bài Viết

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

Huớng dẫn dùng proxy cho ios, iphone 2023

Cách gắn set proxy cho điện thoại android, oppo, giả lập android, Ldplayer Bằng Proxydroid

Mua Proxy Socks5 VN Chơi Game Gia Lập Tăng Cường Trải Nghiệm Chơi Game

Mua Proxy Mỹ, Us Nuôi Tài Khoản Etsy, eBay Tìm Hiểu Về Mua Proxy Mỹ tại

Mua Proxy Game – Giải pháp tuyệt vời cho việc chơi game trên mạng mà không bị giới hạn về vị trí địa lý