How to copy files in linux terminal

Copy files in the Linux terminal

To copy a file on a computer with a graphical interface, you usually either drag and drop a file from one window to another window, sometimes using a modifier key. Alternately, you might prefer to right-click on a file icon, select Copy, and then Paste the file icon into another window.

To copy a file in a terminal, you use the cp command, which works exactly like the mv command, except that it duplicates the contents of a file rather than moving them from one location to another.

As with the mv command, you can rename a file while copying it.

$ cp example.txt ~/Documents/file.txt

Be careful when using cp , because it does exactly what you tell it to do. By default, it does not ask you whether you want to copy one file over another file with the same name. To protect yourself from this, use cp —interactive (or -i for short), which runs cp in interactive mode, meaning that it will ask you whether you’re sure you want to overwrite an existing file. You can either confirm or cancel and re-do the cp and rename the file to something different so that it doesn’t conflict with existing files.

Copy a folder

The cp command looks for files, not folders because folders don’t really exist (that is, they’re not really data, they’re just markers for us humans to logically divide our data into different imaginary containers.) To copy a folder, use cp —recursive (or -r for short) , which takes the files in the folder and recreates their imaginary container.

$ cp -r Documents MyStuff $ ls Documents Downloads Music MyStuff Templates Pictures Videos

You may notice that the Linux shell is eerily quiet when it works. This is actually by design. How many times have you set a computer on a task, like copying a thousand files from one drive to another, only to come back 4 hours later to find that it stopped copying after the first file, just to ask you some trivial esoteric question?

Sometimes, however, it’s nice to have some feedback from your OS. Usually, the -v switch adds «verbosity» to the command:

$ cp -r --verbose Documents Stuff 'Documents/' -> 'Stuff' 'Documents/example.txt' -> 'Stuff/example.txt' 'Documents/file.txt' -> 'Stuff/file.txt' 'Documents/example.png' -> 'Stuff/example.png' 'Documents/picture.png' -> 'Stuff/picture.png' 

Источник

The Linux cp Command – How to Copy Files in Linux

Dillion Megida

Dillion Megida

The Linux cp Command – How to Copy Files in Linux

There are a couple different ways to copy and paste content when you’re working on your computer.

If you spend more time in the user interface of your device, you’ll probably use your mouse to do this. You can copy files by right-clicking on the file and selecting «Copy», then going to a different directory and selecting «Paste».

Читайте также:  Linux посмотреть версии пакетов

For my terminal friends, you can also perform file copy-paste operations without leaving the terminal. In a Linux-based terminal, you do this using the cp command.

In this article, I’ll explain what the cp command is and show you how to copy and paste files and directories in Linux using the terminal.

What is the cp command?

You use the cp command for copying files from one location to another. This command can also copy directories (folders).

The syntax of this command is:

cp [. file/directory-sources] [destination] 

[file/directory-sources] specifies the sources of the files or directories you want to copy. And the [destination] argument specifies the location you want to copy the file to.

To understand the rest of this article, I will use this folder structure example. Let’s say a directory called DirectoryA has two directories in it: DirectoryA_1 and DirectoryA_2. These subdirectories have many files and sub directories in them.

I’ll also assume you’re currently in the DirectoryA location in the terminal, so if you aren’t, make sure you are:

How to copy files with the cp command

If you want to copy a file, say README.txt from DirectoryA_1 to DirectoryA_2, you will use the cp command like this:

cp ./DirectoryA_1/README.txt ./DirectoryA_2 # ./DirectoryA_1/README.txt is the source file # ./DirectoryA_2 is the destination 

If you want to copy more than a file from DirectoryA_1 to DirectoryA_2, you will use the cp command like this:

cp ./DirectoryA_1/README.txt ./DirectoryA_1/ANOTHER_FILE.txt ./DirectoryA_2 

As you can see, you will put all the source files first, and the last argument will be the destination.

How to copy directories with the cp command

By default, the cp command works with files. So if you attempt to copy a directory like this:

cp ./DirectoryA_1/Folder/ ./DirectoryA_2 

You will get an error stating:

./DirectoryA_1/Folder/ is a directory

To copy directories, you have to pass the -r flag. This flag informs the cp command to recursively copy a directory and its contents (which could be files or other sub directories). So for the previous command, you can add the flag before the directory sources like this:

cp -r ./DirectoryA_1/Folder/ ./DirectoryA_2 

This command will recursively copy the Folder directory in ./DirectoryA_1/ as well as all files and directories in the Folder directory.

How to copy files that match a glob pattern

A glob pattern is similar to Regex, which allows you to match multiple files with names that match a specific pattern. Learn more about the difference here: Regex vs Glob patterns.

For example, if you want to copy all files in DirectoryA_1 with the .txt extension, you can execute this command:

cp ./DirectoryA_1/*.txt ./DirectoryA_2 

./DirectoryA_1/*.txt matches files with the .txt extension in their names, and the cp command can copy all those files to the destination.

You can check out the glob documentation to learn more about globbing patterns and characters you can use.

Now you know how to copy files (and directories) right from the command line. Thanks for reading!

Dillion Megida

Dillion Megida

Читайте также:  Linux intel vga drivers

Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Источник

How to Copy Files in Linux

Copying the files is likely a part of daily routine for anyone who is using Linux. You often want to copy a file across various directories. The easiest way of copying files in Linux is using the command line. For those who have issues with the command line, you can use the graphical interface, but it’s recommended to stick to the command line for more flexibility. Nonetheless, this guide will discuss the various ways of copying files in Linux.

Copying Files in Linux

You can use the graphical interface or command line to copy the files in Linux.

Method 1: Graphical Way

For anyone who is not comfortable with the command line, you can simply right-click on the file that you want to copy and select the copy option. Then, navigate to where you want to copy the file, right-click, and select the paste command.

Alternatively, you can use the “copy to” which opens the file system for you to navigate to your target directory.

Method 2: Command-Line Way

Copying the files in Linux is best when using the command line. Besides, Linux offers the cp command that you can use to copy the files from one directory to another by specifying its path.

There are various options that you can use with the cp command. The following is a quick cheat sheet:

  1. -v: Added to make the cp command verbose.
  2. -i: Added to make the cp command interactive, especially when copying the files into a directory that contains files with the same filename.
  3. -p: Added to copy a file and its attributes, such as access permissions and modification dates.
  4. -b: Added to create a backup of the file being copied to the destination folder, but with a different extension.
  5. -r: It specifies recursiveness and is used when you want to copy all files in a directory.

Let’s see how the cp command is used to copy the files.

Copying Files in the Same Directory

Suppose you want to copy the files to another directory in the same location. In that case, you only need to specify the file name or pattern and the target directory. We will use the files in the following image for this example:

Читайте также:  Узнать заряд батареи linux

In the given image, if we needed to copy the bash1.sh to the folder dir1, the command would be:

Note that we added the -v option for verbose.

Also, if we need to copy more than one file, you must separate the file names with a space and list all the files that you want to copy.

If you need to copy the files with the same extension, you can use the wildcard to match a given pattern. For instance, you could use the following command to copy all the text files. All files that match the pattern get copied to the specified directory.

Suppose you wanted to copy the files to a subdirectory in the current directory; you must specify the path. For instance, if dir1 has a subdirectory named test and you want to copy a file to it, we could use the following command:

If you copy the files to a directory that contains the same filename, you will end up overwriting the existing files unless you add the -i option for interactivity.

Let’s try to repeat the command in the previous example to see the error it raises and how you can choose to overwrite the existing files or not.

In the previous image, if you want to overwrite the files having the same names in the target directory, you must type y to the terminal, then press the enter key. If you don’t want to overwrite, press n.

If you want to create a copy of a file in the same directory, you must specify the target file and the name of the replica file. For instance, let’s create a copy of our file1.txt called replica.txt, and let’s retain its attributes. In that case, the command would be as shown in the following:

To create a backup of the file that you are copying, add the -b flag and note the new backup file that is created with a different extension. Let’s create a backup of bash1.sh to our destination folder. We now have a new backup named bash1.sh-

If you want to copy a directory and all the files, add the -r option and specify the path. For instance, in the following image, wecopied the linuxhint folder and all its contents to dir1.

Copying Files in Different Directories

When you must copy the files located in different directories, use the following syntax:

For instance, the command is shown in the following if we needed to copy a file in the /Pictures into the linuxhint sub-directory in the dir1.

The trick is to specify the exact path of the source file and the destination.

Conclusion

We’ve seen the various options that you have when it comes to copying the files in Linux. The commands described only need a little practice, and soon you will master how to copy the files in Linux using the command line.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.

Источник

Оцените статью
Adblock
detector