+3 votes
69 views
in Linux by (242k points)
reopened
Linux: Copy file - this is how it works

1 Answer

+4 votes
by (1.6m points)
 
Best answer

Copy the file or directory in Linux
Application examples

Do you want to copy files in Linux? Regardless of whether it is a normal file or an entire directory - we will show you what you have to do..

image image

In Linux you can easily copy files and directories without a nice user interface á la Ubuntu. All that is needed is a single command. You can also enter several files to be copied directly, but only one target directory can be selected at a time. See below for more explanations.

Copy the file or directory in Linux

  1. Open the terminal as usual with [Ctrl] + [Alt] + [T] .
  2. Now you can enter the cp command, the copy command. This is derived from the short form of "copy" (to German copy) and is always structured in the same way: Instead of [source (s)], enter the file names or directories that you want to copy, for example "file.txt". For the [target] enter the target directory into which the corresponding file is to be copied, for example "home / username / documents". For [Options] you have a whole list of possibilities how you can still modify the copy command. We have compiled the most important options for you in the following table:

    cp [Optionen] [Quelle(n)] [Ziel]





option meaning
-b
--backup
Caches files before overwriting them if they are two different files with the same name.
-i
--interactive
If a file would be overwritten by copying, you have to confirm this first.
-l
--link
With this option you choose that the file is not copied to the target directory. Instead, a link is set there to the storage location of this file, a so-called "hard link".
-n
--no-clobber
This option does not overwrite any existing files.
-p (a lowercase p)
--preserve = timestamps, ownership
The standard properties of the copied file are retained. Depending on your choice, this can be, for example, the time stamp for creation and processing (timestamps) or the creator of the file (ownership).
-P (a capital P)
--no-dereference
Symbolic links are copied as symbolic links instead of the system following the specified address.
-s
--symbolic-link
Instead of copying, a symbolic link (i.e. a reference) is created.
-u
--update
Here the file is only copied if the file is already in the target directory and it is older than the file to be copied.
-v
--verbose
Here the terminal shows exactly which actions are being carried out.
Option Bedeutung
-b
--backup
Speichert Dateien vor dem Überschreiben zwischen, wenn es sich um zwei unterschiedliche Dateien mit dem gleichen Namen handelt.
-i
--interactive
Wenn eine Datei durch das Kopieren überschrieben werden würde, müssen Sie dies erst bestätigen.
-l
--link
Mit dieser Option wählen Sie aus, dass die Datei nicht in das Zielverzeichnis kopiert wird. Stattdessen wird dort ein Link auf den Speicherort dieser Datei gesetzt, ein sogenannter "harter Link".
-n
--no-clobber
Mit dieser Option werden keine vorhandenen Dateien überschrieben.
-p (eines kleines p)
--preserve=timestamps,ownership
Die Standard-Eigenschaften der kopierten Datei werden beibehalten. Je nach Wahl kann dies beispielsweise der Zeitstempel für Erstellung und Bearbeitung (timestamps) oder auch der Ersteller der Datei (ownership) sein.
-P (ein großes P)
--no-dereference
Symbolische Links werden als symbolische Links kopiert, anstatt dass das System der angegebenen Adresse folgt.
-s
--symbolic-link
Anstatt zu kopieren, wird ein symbolischer Link (also ein Verweis) erstellt.
-u
--update
Hier wird die Datei nur kopiert, wenn sich die Datei schon im Zielverzeichnis befindet und diese älter ist als die zu kopierende Datei.
-v
--verbose
Hier zeigt das Terminal genau an, welche Aktionen durchgeführt werden.

Application examples

Because the mere description of the command is a bit cumbersome, we have put together a few examples for you. For this purpose, the command is displayed and then the meaning of this entry is explained in more detail:

  • cp -iv /home/username/Dokumente home/username/Backup

    Here all files are copied from the directory " home / username / documents " to the directory " home / username / Backup ". A check is made as to whether a file is being overwritten and, if this is the case, the user is asked to confirm before it is overwritten. The copying progress is also displayed.
  • cp -u datei.txt home/username/Dokumente

    Here the file " File.txt " is copied into the directory " home / username / documents ". However, this only happens if the " File.txt " file already in this directory is older than the file to be copied .
  • cp -n datei.txt datei2.txt dokument.docx home/username/Dokumente

    In this example, three files, namely the "Files File.txt ", " File2.txt " and " Dokument.docx " into the " home / username / Documents copied". This ensures that no files in the directory are overwritten.

...