Copying 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?

Читайте также:  Linux no console login

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».

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:

Читайте также:  Wireguard client linux gui

./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

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 via terminal?

1) By using -i for interactive you will be asked if you would like to replace the file:

cp -i /home/levan/kdenlive/untitelds.mpg /media/sda3/SkyDrive/ 

or you can use -b to create a backup of your file:

cp -b /home/levan/kdenlive/untitelds.mpg /media/sda3/SkyDrive 

2) Same as the above:

cp (-i or -b) /media/sda3/SkyDrive/untitelds.mpg /home/levan/kdenlive 

3) Use -R for recursive and -i for interactive:

Читайте также:  Linux done syntax error

4) This last one can be done via the mv command, move is like cutting:

mv -i ~/MyFile ~/OtherFolder/MyFile 

if you want to move a directory, use:

mv -Ri ~/MyDirectory ~/OtherDirectory/ 

@WarriorIng64 you can use four spaces identation for code blocks, or you can surround small pieces of code with `s.

@WarriorIng64 That is triggered by the bash tag in the question. highlighting will be enabled by default by the system. If you answer a question with bash tag, the hightlighting will follow bash style and so on.

@Anwar I was just partway through making a Meta post on this when I saw your comment. Of course, I credited you in the answer. 🙂

When ~/Dropbox/RECENT/ is your current directory:

And I want to copy input.txt with another name in my current directory.

Again with ~/Dropbox/RECENT/ as current directory:

Existing filenames can be auto-completed using TAB .

Long version of the same copy command (when you are not in ~/Dropbox/RECENT/ ):

cp /home/$USER/Dropbox/RECENT/input.txt /home/$USER/Dropbox/RECENT/SORT/ 

I put a / behind every directory. If SORT does NOT exist a cp will also create a file named SORT making you think something went wrong. Adding the / will have cp error out and not copy the file.

Copying a file something.txt to file folder : use cp something.txt folder/

Copying a file something.txt to the current directory as something2.txt : use cp something.txt something2.txt

ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l total 8 drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:53 Folder1 -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:52 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l Folder1/ total 4 -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:53 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l total 8 drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:54 folder -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:52 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l folder/ total 0 ubuntu@ubuntu-T100TA:~/TestFolder$ cp something.txt folder/ ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l folder/ total 4 -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:55 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ cp something.txt something2.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l total 12 drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:55 folder -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:55 something2.txt -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:52 something.txt 

Источник

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