Using copy in linux

Linux Copy File Command [ cp Command Examples ]

cp Command Syntax

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Terminal app/Shell prompt
Est. reading time 3 mintues

The syntax is as follows to copy files and directories using the cp command:
cp SOURCE DEST
cp SOURCE DIRECTORY
cp SOURCE1 SOURCE2 SOURCE3 SOURCEn DIRECTORY
cp [OPTION] SOURCE DEST
cp [OPTION] SOURCE DIRECTORY
Where,

  • In the first and second syntax you copy SOURCE file to DEST file or DIRECTORY.
  • In the third syntax you copy multiple SOURCE(s) (files) to DIRECTORY.

Note: You need to type the cp command at the dollar sign ($) prompt. This prompt means that the shell is ready to accept your typed commands. Do not type the dollar ($) sign. You need to open the Terminal app to use cp command on a Linux.

Linux Copy File Examples

To make a copy of a file called file.doc in the current directory as newfile.doc, enter:
$ cp file.doc newfile.doc
$ ls -l *.doc
Sample outputs:

You can copy multiple files simultaneously into another directory. In this example, copy the files named main.c, demo.h and lib.c into a directory named backup:
$ cp main.c demo.h libc. backup
If backup is located in /home/project, enter:
$ cp main.c demo.h libc. /home/project backup

Copy a file to another directory

To copy a file from your current directory into another directory called /tmp/, enter:
$ cp filename /tmp
$ ls /tmp/filename
$ cd /tmp
$ ls
$ rm filename

Verbose option

To see files as they are copied pass the -v option as follows to the cp command:

Preserve file attributes

To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:
$ cp -p file.txt /dir1/dir2/
$ cp -p filename /path/to/new/location/myfile
This option ( -p ) forces cp to preserve the following attributes of each source file in the copy as allowed by permissions:

  1. Modification time/date
  2. Access time
  3. File flags
  4. File mode
  5. User ID (UID)
  6. Group ID (GID)
  7. Access Control Lists (ACLs)
  8. Extended Attributes (EAs)

Copying all files

The star wildcard represents anything i.e. all files. To copy all the files in a directory to a new directory, enter:
$ cp * /home/tom/backup

The star wildcard represents anything whose name ends with the .doc extension. So, to copy all the document files (*.doc) in a directory to a new directory, enter:
$ cp *.doc /home/tom/backup

Recursive copy

To copy a directory, including all its files and subdirectories, to another directory, enter (copy directories recursively):
$ cp -R * /home/tom/backup

Linux copy file command with interactive option

You can get prompt before overwriting file. For example, if it is desired to make a copy of a file called foo and call it bar and if a file named bar already exists, the following would prompt the user prior to replacing any files with identical names:
cp -i foo bar

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Verbose output with cp command

If you pass the -v to the cp, it makes tells about what is going on. That is verbose output:
cp -v file1 file2
cp -avr dir2 /backups/

Conclusion

This page explained cp command that is used for copying files under Linux and Unix-like systems. For more info see man pages: ls(1).

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

cp Command in Linux: 7 Practical Examples

One of the commands that you must know in Linux is cp. It’s often called the copy command in Linux and it is actually short for copy and it does exactly as it name suggests: it copies.

cp is used for copying files from one location to other. cp can also be used to copy entire directories into a new location. You can use it to copy multiple files and directories as well.

7 Examples of using cp command in Linux

Let’s see how you can use cp command for various purposes:

1. How to copy a file

The simplest example would be to copy a file. To do that, you just have to specify the source file and the destination directory or file.

In the above example, if the target_fille doesn’t exist in the target_directory, it will create target_file.

However, if the new_file already exists, it will overwrite it without asking. Which means the content of the existing target file will be changed with the content of the source file.

I’ll show you how to deal with overwriting of files later in this tutorial.

Keep in mind: By default, cp commands overwrites if the target file already exists. This behavior can be changed with -n or -i option, explained later.

2. How to copy multiple files

If you want to copy multiple files at once to a new location, you can do that in the following manner:

This will copy all the specified files to the target directory. If the target directory has file(s) matching the name of the source file(s), it will be overwritten.

3. Multiple ways of dealing with overwriting while copying files

You probably won’t always want that your existing target files are overwritten and that’s totally logical.

To prevent overwriting existing files, you can use the -n option. This way, cp won’ overwrite existing files.

But maybe you want to overwrite some files. You can use the interactive option -i and it will ask you if you want to overwrite existing file(s).

You can enter y for overwriting the existing file or n for not overwriting it.

There is also an option for making automatic backups. If you use -b option with cp command, it will overwrite the existing files but before that, it will create a backup of the overwritten files.

The backup of the file ends with

You can also use the update option -u when dealing with overwriting. With the -u option, source files will only be copied to the new location if the source file is newer than the existing file or if it doesn’t exist in the target directory.

  • -i : Confirm before overwriting
  • -n : No overwriting
  • -b : Overwriting with backup
  • -u : Overwrite if the target file is old or doesn’t exist

4. How to copy a directory in Linux

You can also use the cp command to copy a directory in Linux including all its files and sub-directories. You have to use the -r option here which stands for recursive.

This will copy the entire source_dir into target_dir. Now the source_dir will be a subdirectory of the target_dir.

5. How to copy only the content of a directory, not the directory itself

In the previous example, you copied the entire directory into a new location.

But if you just want to copy the content of the source directory into the target directory, you should add /. at the end of the source directory. This will indicate that you only want to copy the content of the source directory.

Let’s see it with an example:

Now copy the content of the source directory:

If you check the contents of the target directory now, you’ll see that only the contents of the source directory have been copied.

6. How to copy multiple directories

You can also copy multiple directories at once with cp command in Linux.

Just use it the same way you did for a single directory.

It’s always the last argument in the command that is taken as the target directory.

If you want to copy only the content of multiple directories at once, you can do that as well:

In fact, you can mix directories, their content and files altogether.

Tip: You can use the verbose mode with option -v to see what files are being copied.

7. How to preserve the attributes while copying

When you copy a file to a new location, its attributes like the file permissions and the file timestamps are changed.

If you want to retain the attributes of the original file, you can copy the files with the option -p.

Let’s see it with an example.

If I try to copy this file normally, its attributes will be changed:

But if I use the option p, the copied file will retain the mode, ownership and the timestamp.

As you can see, you preserved the access mode and the timestamp pf the source file with the -p option.

But wait! Was it not supposed to preserve the ownership of the source files as well? But here the owner (root) of the source file has been changed to abhishek.

This is because only root has the permission to change the ownership of a file owned by root. If you use the -p option with a file not owned by root, it will preserve the ownership. Or, you can run the command with sudo to preserve the ownership of a file owned by root.

You can also specify the attributes you want to preserve. But then you’ll have to use the –preserve option.

As you can see in the above output, it preserved only the timestamp of the source file.

You can further explore the cp command by browsing its man page. The examples shown here are the most common ones that you’ll be using as Linux user, sysadmin or software developer.

If you liked this tutorial, please share this article on social media and various forums.

Источник

How to Copy Paste in Linux Terminal [For Absolute Beginners]

Last updated October 29, 2020 By Abhishek Prakash 27 Comments

I have been using Linux for a decade now and this is why sometimes I take things for granted.

Copy pasting in the Linux terminal is one of such things.

I thought everyone already knew this until one of the It’s FOSS readers asked me this question. I gave the following suggestion to the Ubuntu user:

Use Ctrl+Insert or Ctrl+Shift+C for copying and Shift+Insert or Ctrl+Shift+V for pasting text in the terminal in Ubuntu. Right click and selecting the copy/paste option from the context menu is also an option.

I thought of elaborating on this topic specially when there is no single universal way of copy and paste in the Linux terminal.

How to copy paste text and commands in the Linux terminal

There are several ways to do this.

Method 1: Using keyboard shortcuts for copy pasting in the terminal

On Ubuntu and many other Linux distributions, you can use Ctrl+Insert or Ctrl+shift+C for copying text and Shift+Insert or Ctrl+shift+V for pasting text in the terminal.

The copy pasting also works for the external sources. If you copy a command example from It’s FOSS website (using the generic Ctrl+C keys), you can paste this command into the terminal using the Ctrl+Shift+V into the terminal.

Similarly, you can use Ctrl+shift+C to copy text from the terminal and then use it to paste in a text editor or web browser using the regular Ctrl+V shortcut.

Basically, when you are interacting with the Linux terminal, you use the Ctrl+Shift+C/V for copy-pasting.

Method 2: Using right click context menu for copy pasting in the terminal

Another way of copying and pasting in the terminal is by using the right click context menu.

Select the text in the terminal, right click and select Copy. Similarly, to paste the selected text, right click and select Paste.

Method 3: Using mouse to copy paste in Linux terminal

Another way to copy paste in Linux terminal is by using only the mouse.

You can select the text you want to copy and then press the middle mouse button (scrolling wheel) to paste the copied text.

Please keep in mind that these methods may not work in all the Linux distributions for a specific reason that I explain in the next section.

There is no universal key shortcuts for copy paste in the Linux terminal. Here’s why!

The keybindings for copy-pasting are dependent on the terminal emulator (commonly known as terminal) you are using.

If you didn’t know that already terminal is just an application and you can install other terminals like Guake or Terminator.

Different terminal applications may have their own keybindings for copying and pasting like Alt+C/V or Ctrl+Alt+C/V.

Most Linux terminals use the Ctrl+Shift+C/V keys but if it doesn’t work for you, you may try other key combinations or configure the keys from the preferences of the terminal emulator.

Quick word about Putty

If you use Putty on Linux or Windows, it uses an entire different keybindings. In Putty, selecting a text automatically copies it and you can paste it using right click.

Why Linux terminals do not use the ‘universal’ Ctrl+C and Ctrl+V for

No Linux terminal will give you Ctrl+C for copying the text. This is because by default Ctrl+C keybinding is used for sending an interrupt signal to the command running in foreground. This usually stops the running command.

This behavior has been existing long before Ctrl+C and Ctrl+V started being used for copy-pasting text.

Since the Ctrl+C keys are ‘reserved’ for stopping a command, it cannot be used for copying.

Used Ctrl+S and hanged the terminal?

Most of us use Ctrl+S keys to save changes made to text, images etc. This key is almost universal for saving same as Ctrl+C is for copying.
However, if you enter Ctrl+S in Linux terminal, it will freeze the terminal. No need to close the terminal and start it again. You can use Ctrl+Q to unfreeze the terminal.
Ctrl+S and Ctrl+Q are shortcut keys for flow control.

I know this is elementary for the Sherlock Holmes of the Linux world but it could still be useful to the Watsons.

New or not, you may always use shortcuts in Linux terminal to make your life easier.

Like what you read? Please share it with others.

Источник

Читайте также:  Нет загрузки windows debian
Оцените статью