முக்கிய உள்ளடக்கத்திற்குச் செல்லவும்
eLearner.app
தொகுதி 2 · பாடம் 2 இன் 3பாடத்திட்டத்தில் 4/11~12 min
தொகுதி பாடங்கள் (2/3)

நகர்த்துதல் மற்றும் நகலெடுத்தல் (mv மற்றும் cp)

To reorganize and duplicate files in the terminal, use the cp (copy) and mv (move or rename) commands.


Copying Files and Folders: cp

The basic syntax for copying a file is:

Bash
cp [source] [destination]

It stands for Copy. For example, to copy a file named photo.png into a folder called images, you would type:

Bash
cp photo.png images/

Copying Folders Recursively with -r

If you try to copy a directory using the standard cp command, you will get an error. To copy a folder and all its contents recursively, you must use the -r (or -R, recursive) option:

Bash
cp -r project_folder project_backup

Moving or Renaming Files and Folders: mv

The mv command stands for Move. It is used both to move files and folders into different directories, and to rename them:

Bash
mv [source] [destination]

Moving a Resource

To move a file named document.pdf into a folder called archive:

Bash
mv document.pdf archive/

Renaming a Resource

To change a file's name without moving it to another directory, specify the new filename as the destination:

Bash
mv old_name.txt new_name.txt

Moving and Renaming at the Same Time

You can move and rename a file in a single step:

Bash
mv report.txt archive/monthly_report.txt

Usage Examples and Common Errors

If you try to copy a directory without specifying the recursive -r option, the cp command will fail with an error:

Bash
cp backup backup_copy
# Output:
# cp: -r not specified; omitting directory 'backup'

If you try to move or copy a source file that does not exist, you will receive an error message:

Bash
mv nonexistent_file.txt backup/
# Output:
# mv: cannot stat 'nonexistent_file.txt': No such file or directory

Try it yourself

Exercise 1: Copy a report

உடற்பயிற்சி#linux.m2.l2.e1
முயற்சிகள்: 0ஏற்றுகிறது…

Copy the file 'report.txt' into the 'backup' folder while keeping the original file.

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

Use cp with 'report.txt' as the source and 'backup' as the destination.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்

Exercise 2: Move and rename

உடற்பயிற்சி#linux.m2.l2.e2
முயற்சிகள்: 0ஏற்றுகிறது…

Move the file 'old_notes.txt' into the 'archive' folder, renaming it to 'notes_old.txt' at the same time.

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

Use mv, specifying the source file and the destination path including the new filename.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்

Exercise 3: Copy a directory

உடற்பயிற்சி#linux.m2.l2.e3
முயற்சிகள்: 0ஏற்றுகிறது…

Create a backup copy of the entire 'backup' folder and name it 'backup_dir' (remember to use the recursive option).

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

Use the cp command with the -r flag to copy the backup folder to backup_dir.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்