Copying, moving, and removing files

2.4 Creating, Moving, and Deleting Files (Weight: 2)

📘Linux Essentials (LPI 010-160)


Managing files is a fundamental task in Linux systems. System administrators, developers, and support staff often need to copy files, move files to different directories, or remove files that are no longer needed.

Linux provides several command-line tools for these tasks. The most important commands for the Linux Essentials (LPI 010-160) exam are:

  • cp – Copy files and directories
  • mv – Move or rename files and directories
  • rm – Remove files and directories

Understanding how these commands work and how their options affect behavior is important for passing the exam.


1. Copying Files and Directories

The cp (copy) command is used to create a duplicate of a file or directory.

Basic Syntax

cp [options] source destination
  • source – The file or directory to copy
  • destination – The location where the copy will be placed

Copying a Single File

Example:

cp config.txt backup_config.txt

Result:

  • A new file named backup_config.txt is created.
  • The contents of config.txt are copied into it.

This is commonly used in IT environments to create backups of configuration files before modifying them.


Copying a File to Another Directory

Example:

cp config.txt /home/user/backups/

Result:

  • The file config.txt is copied into the /home/user/backups/ directory.

The copied file keeps the same name unless you specify a different one.

Example:

cp config.txt /home/user/backups/config_backup.txt

Copying Multiple Files

You can copy several files at once.

Example:

cp file1.txt file2.txt file3.txt /home/user/archive/

Result:

  • All files are copied into the archive directory.

Copying Directories

To copy a directory and its contents, the recursive option -r must be used.

Example:

cp -r project/ project_backup/

Result:

  • The entire project directory, including all files and subdirectories, is copied.

Without -r, Linux will not copy directories.


Important cp Options

-r (recursive)

Copies directories and their contents.

cp -r website/ website_backup/

-i (interactive)

Prompts before overwriting an existing file.

Example:

cp -i config.txt /etc/config.txt

If the file already exists, the system asks before replacing it.


-u (update)

Copies files only if the source file is newer than the destination.

Example:

cp -u app.conf /backup/

This is useful in backup scripts.


-v (verbose)

Shows what the command is doing.

Example:

cp -v file.txt backup/

Output example:

'file.txt' -> 'backup/file.txt'

2. Moving and Renaming Files

The mv (move) command is used to:

  • Move files to another directory
  • Rename files
  • Move directories

Basic Syntax

mv [options] source destination

Moving a File to Another Directory

Example:

mv log.txt /var/log/archive/

Result:

  • The file log.txt is moved to /var/log/archive/.
  • It no longer exists in the original directory.

This is commonly done in IT environments when archiving old logs.


Renaming a File

Example:

mv oldname.txt newname.txt

Result:

  • The file name changes from oldname.txt to newname.txt.

No new file is created; the existing file is simply renamed.


Moving Multiple Files

Example:

mv *.log /var/log/archive/

Result:

  • All files ending in .log are moved to the archive directory.

The * is a wildcard that matches multiple files.


Moving Directories

Example:

mv project/ /home/user/projects/

Result:

  • The entire project directory is moved to a new location.

Important mv Options

-i (interactive)

Asks before overwriting files.

mv -i config.txt /etc/config.txt

-v (verbose)

Shows detailed output.

mv -v report.txt archive/

Output example:

'report.txt' -> 'archive/report.txt'

3. Removing Files and Directories

The rm (remove) command deletes files and directories.

⚠ Important:
Once removed using rm, files cannot be easily recovered.


Basic Syntax

rm [options] file

Removing a File

Example:

rm temp.txt

Result:

  • The file temp.txt is permanently deleted.

Removing Multiple Files

Example:

rm file1.txt file2.txt file3.txt

Result:

  • All listed files are removed.

Removing Files Using Wildcards

Example:

rm *.log

Result:

  • All .log files in the directory are deleted.

This is often used for clearing old logs.


Removing Directories

To remove directories and their contents, use recursive mode.

Example:

rm -r old_project/

Result:

  • The directory and all files inside it are deleted.

Important rm Options

-r (recursive)

Deletes directories and their contents.

rm -r backup/

-f (force)

Forces deletion without prompts.

rm -f file.txt

Used in scripts to remove files automatically.


-i (interactive)

Prompts before deleting files.

rm -i config.txt

The system asks for confirmation.


-v (verbose)

Displays detailed output.

rm -v file.txt

Output example:

removed 'file.txt'

4. Removing Empty Directories

The rmdir command removes only empty directories.

Example:

rmdir old_directory

If the directory contains files, the command will fail.


5. Common IT Environment Usage

These commands are used frequently in system administration tasks such as:

Backup Operations

Example:

cp database.conf database.conf.backup

Creates a backup before making configuration changes.


Archiving Log Files

Example:

mv *.log /var/log/archive/

Moves old log files to an archive directory.


Cleaning Temporary Files

Example:

rm *.tmp

Deletes temporary files created by applications.


Removing Old Project Directories

Example:

rm -r old_project/

Removes directories that are no longer needed.


6. Important Exam Points (LPI 010-160)

Students should remember the following key points:

Essential Commands

CommandPurpose
cpCopy files and directories
mvMove or rename files
rmRemove files or directories
rmdirRemove empty directories

Important Options

OptionCommandPurpose
-rcp, rmRecursive (copy/remove directories)
-icp, mv, rmInteractive confirmation
-vcp, mv, rmVerbose output
-frmForce removal

Key Concepts

Students must understand:

  • Difference between copying and moving
  • How to rename files using mv
  • How to remove directories using rm -r
  • The use of wildcards (*)
  • The importance of interactive mode (-i)

Summary

Linux provides powerful commands to manage files:

  • cp creates duplicates of files and directories.
  • mv moves files between directories or renames them.
  • rm deletes files and directories.
  • rmdir removes empty directories.
Buy Me a Coffee