WCC logo

CIS120Linux Fundementals

cp, mv and rm Commands

The cp (copy), mv (move), and rm (remove) commands are essential tools in Linux for file and directory management. These commands allow users to copy, move, and delete files and directories. Understanding their options and usage can greatly enhance your efficiency when working with the Linux command line.

The cp Command

The cp command is used to copy files and directories. It can copy a file to another file, multiple files to a directory, or entire directories to another location.

cp [options] source destination

Commonly Used cp Options:

Option Description
-a Archive mode; preserves attributes and copies recursively
-i Prompt before overwriting files
-r Recursively copy directories
-u Copy only when the source file is newer than the destination file or when the destination file is missing
-v Verbose mode; show files as they are copied
-p Preserve file attributes (e.g., timestamps, ownership)
--backup Make a backup of each existing destination file

Examples:

To copy a file to another file:

This copies the contents of file1.txt to file2.txt. If file2.txt does not exist, it is created.

cp file1.txt file2.txt

To copy a file to a directory:

This copies file1.txt to the /home/user/documents/ directory.

cp file1.txt /home/user/documents/

To copy multiple files to a directory:

This copies file1.txt and file2.txt to the /home/user/documents/ directory.

cp file1.txt file2.txt /home/user/documents/

To copy a directory and its contents recursively:

This copies the documents directory and all its contents to the backup directory.

cp -r /home/user/documents /home/user/backup/

To copy files and preserve their attributes:

This copies file1.txt to the documents directory while preserving its attributes.

cp -p file1.txt /home/user/documents/

To copy files with verbose output:

This shows the files being copied in the terminal.

cp -v file1.txt /home/user/documents/

The mv Command

The mv command is used to move files and directories. It can also be used to rename files and directories. When a file or directory is moved to a new location, the original is deleted.

Basic usage of mv:

mv [options] source destination

Commonly Used mv Options:

Option Description
-i Prompt before overwriting files
-u Move only when the source file is newer than the destination file or when the destination file is missing
-v Verbose mode; show files as they are moved
-f Force move by overwriting destination files without prompt
--backup Make a backup of each existing destination file

Examples:

To move a file to another location:

This moves file1.txt to the documents directory.

mv file1.txt /home/user/documents/

To rename a file:

This renames file1.txt to file2.txt.

mv file1.txt file2.txt

To move a directory to another location:

This moves the documents directory to the backup directory.

mv /home/user/documents /home/user/backup/

To move files with verbose output:

This shows the files being moved in the terminal.

mv -v file1.txt /home/user/documents/

To move files and prompt before overwriting:

This prompts before overwriting file1.txt in the documents directory.

mv -i file1.txt /home/user/documents/

To force move files without prompting:

This moves file1.txt to the documents directory without prompting, even if it overwrites an existing file.

mv -f file1.txt /home/user/documents/

The rm Command

The rm command is used to remove files and directories. It is a powerful command that deletes files and directories permanently, so it must be used with caution.

Basic usage of rm:

rm [options] file

Commonly Used rm Options:

Option Description
-i Prompt before every removal
-f Force removal of files without prompting
-r Recursively remove directories and their contents
-v Verbose mode; show files as they are removed
--preserve-root Do not remove the root directory

Examples:

To remove a file:

This removes file1.txt.

rm file1.txt

To remove multiple files:

This removes file1.txt and file2.txt.

rm file1.txt file2.txt

To remove a directory and its contents recursively:

This removes the documents directory and all its contents.

rm -r /home/user/documents/

To remove files with verbose output:

This shows the files being removed in the terminal.

rm -v file1.txt

To force remove files without prompting:

This removes file1.txt without prompting, even if it is write-protected.

rm -f file1.txt

To prompt before every removal:

This prompts for confirmation before removing file1.txt.

rm -i file1.txt

Summary

The cp, mv, and rm commands are powerful tools for managing files and directories in Linux. The cp command allows for copying files and directories with various options to preserve attributes, prompt before overwriting, and provide verbose output. The mv command enables moving and renaming files and directories, with options to prompt before overwriting, force move, and provide verbose output. The rm command allows for removing files and directories, with options to force removal, prompt before deletion, and provide verbose output. By mastering these commands and their options, you can efficiently manage your files and directories in the Linux environment.