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

How to use Variables in Bash Programming

Variables work as temporary storage for any programming language. The coder needs to have a clear concept of using different...

How to Search for your Files on the Linux Command Line

For a Linux desktop, a user can easily install an app to search their files and folders in the file system, but another...

BASH Heredoc Tutorial

How to use Here Document in bash programming A block of code or text which can be redirected to the command script or interactive...
Bài Viết

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

Lý do tại sao bạn nên sử dụng proxy khi truy cập web đen

Các lỗi thường gặp khi sử dụng proxy và cách khắc phục chúng.

Tác động của việc sử dụng proxy đến tốc độ kết nối internet của bạn.

Các tiện ích và công cụ để quản lý proxy.

Các cách để kiểm tra tốc độ và độ ổn định của proxy.