• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
WebSetNet

WebSetNet

Technology News

  • Technology News
    • Mobile
    • Games
  • Internet Marketing
  • System Admin
    • Windows
    • Linux
    • Mac & Apple
    • Website Scripts
      • Wordpress

Linux cp command tutorial for beginners (8 examples)

August 5, 2020 by Martin6

 

If you are new to Linux, it’s worth knowing that command line is a very powerful tool, capable of doing almost all those tasks that you can do through the graphical interface. However, more often than not, you’ll find yourself doing simple tasks on the command line. One such simple task is to copy files/directories.

The Linux cp command provides you the power to copy files and directories through the command line. In this tutorial, we will discuss the basic usage of this tool using easy to understand examples. But before we do that, it’s worth sharing that all examples/instructions mentioned in this article have been tested on Ubuntu 16.04LTS.

Linux cp command

Need to perform a copy operation on the command line, cp should be your go-to command. According to its man page, the tool can be used to copy both files and directories. Here’s the command’s generic syntax:

cp [OPTION]… SOURCE DEST

The above command copies SOURCE to DEST. The following Q&A type examples will give you a better idea of how the tool works.

Q1. How to perform basic copy operation?

The basic usage of cp is very easy – all you have to do is to specify the source and the destination. For example, the following command copies ‘file1’ present in the current working directory to the Desktop directory:

cp file1 ~/Desktop

Q2. How to make cp prompt before overwriting?

If the destination where you are copying the file already contains a file of same name, then the cp command silently overwrites the existing file. However, if you want, you can make the tool prompt before overwriting is done. This can be done by running cp in interactive mode, which is enabled using the -i option.

For example:

cp -i file1 ~/Desktop/

How to make cp prompt before overwriting

So as you can see in the screenshot above, the -i option makes cp ask the user whether or not to overwrite existing file of the same name in the destination directory.

Q3. How to force cp to not overwrite existing file?

Sometimes, you might not want cp to overwrite an existing file, and you also don’t want to enable the interactive option we just discussed in the previous section. Then for these kind of situations, you can use the -n command line option.

For example:

cp -n file1 ~/Desktop/

Please note that -n overrides the -i option, meaning even if you’ve used -i with -n, the command will not run in interactive mode.

Q4. How to copy directories using cp?

By default, the cp command only works for files. If you try copying a directory, you’ll likely get a “cp: omitting directory” error. So, whenever the requirement is to copy a directory, use the -r command line option.

For example:

cp -r dir ~/Desktop

Q5. How to create symbolic links using cp command?

If you want, you can also ask cp to create a symbolic link instead of actually copying a file. This can be done using the -s command line option.

For example:

cp -s ~/htf-daily/file1 ~/Desktop/

How to create symbolic links using cp command

Q6. How to make cp overwrite destination file only if source is newer?

Sometimes, the requirement is to overwrite existing file only when the source file is newer – think of this process as updating the file. This can be done using the -u command line option.

For example, suppose you want to copy ‘file1’ residing in current working directory to the Desktop directory, but the destination already has a file named ‘file1’. And you only want to copy if source is newer than destination. This can be accomplished using the following command:

cp -u file1 ~/Desktop

Q7. How to make cp not follow symbolic links in source?

By default, the cp command follows symbolic links in source. This means that, for example, if you are trying to copy a file which is a symbolic link to another one, then by default, the copy action is taken on the the file the symbolic link refers to. What I mean is, if ~/Desktop/file1 is a symbolic link to ~/htf-daily/file1, and you try copying ~/Desktop/file1 to ~/Downloads, then ~/htf-daily/file1 will get copied there.

However, if the requirement is to copy the symbolic link itself, the this can be made possible using the -P command line option, which asks cp to not follow symbolic links in source. So in our case, the command would be:

cp -P ~/Desktop/file1 ~/Downloads/

Q8. How to copy only file attributes?

Sometimes, the requirement could only be to copy attributes (like ownership and timestamps), and not the content of the file. This can be achieved using the –attributes-only command line option along with the –preserve option.

For example, file1 has following attributes:

-rw-rw-r-- 1 root himanshu 97 Jun 14 17:18 file1

And file2 has following attributes:

-rw-rw-r-- 1 root himanshu 179 May 25 15:09 file2

Note: You can use the ls command to fetch these attributes for a file. For more information on ls, head here.

And the requirement is to copy attributes of file2 and have them for file1 as well, then here’s how this can be done:

cp –attributes-only –preserve file2 file1

Here’s the aforementioned command in action:

How to copy only file attributes

Conclusion

As you’d agree, the basic usage of the cp command isn’t very difficult to understand. In this tutorial, we have tried to cover most of the useful command line options. Try them at your end, and after that, go to the tool’s man page to see what other features/options the command offers. In case you any doubt or query, drop a comment below.

Source

Related posts:

  1. Grab The Elder Scrolls Online and more in this week’s Xbox Deals with Gold
  2. Grab Psychonauts 2 and Star Wars in this week’s Xbox Deals with Gold
  3. Essential command tools to fix virtually any networking issue on Windows 10
  4. Fast Delete Complex Directories
  5. Amazon Alexa Easter eggs: Over 180 funny things to ask Alexa
  6. Amazon Alexa Easter eggs: Over 180 funny things to ask Alexa
  7. Amazon Alexa Easter eggs: Over 180 funny things to ask Alexa
  8. How to Change the Last Modified Date, Creation Date, and Last Accessed Date for Files and Folders
  9. How To Find Broken Symlinks On Linux Ubuntu And Delete Them
  10. Copy and move files faster, manage multiple tasks efficiently with Copy Handler

Filed Under: Uncategorized

Primary Sidebar

Trending

  • How to fix Windows Update Error 80244019
  • Windows 10 Update keeps failing with error 0x8007001f – 0x20006
  • How To Change Netflix Download Location In Windows 10
  • Troubleshoot Outlook “Not implemented” Unable to Send Email Error
  • How do I enable or disable Alt Gr key on Windows 10 keyboard
  • How To Install Android App APK on Samsung Tizen OS Device
  • 3 Ways To Open PST File Without Office Outlook In Windows 10
  • FIX: Windows Update error 0x800f0986
  • How to Retrieve Deleted Messages on Snapchat
  • Latest Samsung Galaxy Note 20 leak is a spec dump revealing key features
  • Install Android 7.0 Nougat ROM on Galaxy Core 2 SM-G355H
  • 192.168.1.1 Login, Admin Page, Username, Password | Wireless Router Settings
  • Websites to Watch Movies Online – 10+ Best Websites Without SignUp/Downloading
  • How to Backup SMS Messages on Your Android Smartphone
  • How to delete a blank page at the end of a Microsoft Word document
  • Fix: The Disc Image File Is Corrupted Error In Windows 10
  • Android 11 Custom ROM List – Unofficially Update Your Android Phone!
  • Samsung Galaxy Z Fold 3 could be scheduled for June 2021, with S Pen support

Footer

Tags

Amazon amazon prime amazon prime video Apple Application software epic games Galaxy Note 20 Galaxy S22 Plus Galaxy S22 Ultra Google Sheets headphones Huawei icloud Instagram instant gaming ip address iPhone iphone 12 iphone 13 iphone 13 pro max macOS Microsoft Microsoft Edge Mobile app office 365 outlook Pixel 6 Samsung Galaxy Samsung Galaxy Book 2 Pro 360 Samsung Galaxy Tab S8 Smartphone speedtest speed test teams tiktok Twitter vpn WhatsApp whatsapp web Windows 10 Windows 11 Changes Windows 11 Release Windows 11 Update Windows Subsystem For Android Windows 11 Xiaomi

Archives

  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org