Let us copy the notes file while leaving the source in place. Cp is short for copy. The order of the source and destination is the detail to watch first.
Could the destination already exist?
Copying onto an existing file can replace its content. We use only the practice area and include -i to request an overwrite question when appropriate. Read that question instead of approving automatically.
The notlar.txt file should contain the two lines from lesson eight. If starting here, create it with nano first; copying an empty file makes the experiment less informative.
cd "$HOME/netopsiyon-dersleri"
cp -i notlar.txt notlar-kopya.txt
ls -l notlar.txt notlar-kopya.txt
cp -i notlar.txt belgeler/
ls -l belgeler/notlar.txt
The first copy uses notlar.txt as source and notlar-kopya.txt as destination. Both should appear in the listing. Open them separately in nano to confirm that their initial text matches.
When the target is a directory
cp -i notlar.txt belgeler/ copies into the existing belgeler directory, retaining the filename. The resulting path is belgeler/notlar.txt. A trailing slash makes the intended directory target clearer; it does not create a missing directory.
If an unqualified destination does not exist, it may become a new filename instead. Do not assume the second path always means a directory. Ls -ld can inspect the target itself.
The copy is now independent
Edit notlar-kopya.txt and add a line. Reopen the source: that new line should not be there. A normal file copy does not keep two names attached to one live text buffer.
The destination's state changes the result
In cp notlar.txt kopya, a nonexistent kopya becomes a new filename. An existing kopya directory receives notlar.txt inside it. An existing regular file may have its contents replaced. Inspect the destination type rather than assuming one fixed interpretation.
| Destination | Expected result |
|---|---|
| A new filename | A new file beside the source. |
| An existing directory | The source filename inside it. |
| An existing regular file | Its content can be replaced after confirmation. |
Ls -ld helps inspect a target without automatically listing its contents. This is useful when a copy seems to have vanished into an unexpected nested directory.
Several sources need a directory destination
cp -iv -- notlar.txt bir.txt belgeler/
The final target must be an existing directory when supplying multiple sources. Here -v reports actions, -i requests confirmation for applicable overwrites, and -- ends option processing. A question is expected if an earlier lesson already copied the same name there.
Declining an overwrite leaves the source in place and preserves the old destination. Copy is not move. Read the localized question instead of assuming every system uses the same answer letter.
Preview a pattern before copying
ls -d -- *.txt
cp -iv -- *.txt belgeler/
The first line previews matching visible names. The second uses that pattern for real copying. Bash expands it before cp receives the paths. Leading-dot names are normally absent. Avoid broad patterns that accidentally select the destination directory itself.
Matching filenames and sizes are not proof of matching contents. Reopen both files to compare their text. Tools such as cmp can compare bytes in later exercises; two different sentences can have the same length.
A copy is not a link
The independent-edit experiment uses ordinary files, not symbolic links. Do not generalize it into a claim that every cp invocation preserves all filesystem relationships or constitutes a complete backup.
Add “Copy” to the copy's first line, then confirm the source lacks it. Read the source and destination order aloud. Reversing them can replace the wrong file rather than merely giving an untidy result.
Copy a directory tree
cp -ri belgeler belgeler-yedek
The -r option requests recursive copying. When belgeler-yedek does not yet exist, it becomes the destination directory containing the copied contents. If it already exists as a directory, the source directory may be placed inside it. Repeating the command need not create the same arrangement.
These are ordinary practice files. Preserving ownership, permissions, links and other metadata for system backups is a separate subject. Copying a few files is not a complete backup strategy.
Finish by identifying the source, the renamed copy and the copy inside belgeler as three separate paths. That is the source/destination distinction we need before moving files.
References: GNU / Linux