USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

File and Directory Operations

Lesson 2/32 | Study Time: 20 Min

File and directory operations form an essential part of working with Linux systems, especially for developers. Mastering these operations enables users to create, organize, move, copy, and delete files and directories efficiently via the command line.

This fundamental knowledge improves workflow, supports automation scripts, and aids system management. 

Creating Directories with mkdir

The mkdir command is used to create new directories. It can also create nested directories along a given path, ensuring parent directories are created as needed.


Usage


1. Basic syntax:

text
mkdir directory_name


2. To create nested directories (parent and child), use the -p option:

text
mkdir -p parent/child/grandchild

This creates the entire directory tree in one command.


3. The -v option provides verbose output to see the directories created step-by-step.


Example:

text
mkdir -p projects/2025/november

This command creates a nested directory structure suitable for organizing files by year and month.

Copying Files and Directories with cp

The cp command allows you to copy files or directories. By default, it copies files only; to copy directories recursively, use the -r or -R option.


Important options:


-r or -R: Recursively copy directories and their contents.

-i: Interactive mode prompts before overwriting existing files.

-u: Copy only when the source file is newer or the destination is missing.

-a: Archives files to preserve all attributes (permissions, timestamps).


Examples:


1. Copy a file:

text
cp file1.txt file2.txt


2. Copy a directory recursively:

text
cp -r dir1/ dir2/

This copies everything inside dir1 into dir2.

Moving and Renaming Files with mv

The mv command moves files or directories to a new location or renames them.


Key points:


Syntax:

text
mv [options] source destination


1. Similar to cp, -i prompts before overwriting.

2. Moving directories recursively is implicit; no need for -r.


Examples:


1. Rename a file:

text
mv oldfile.txt newfile.txt


2. Move a file to a different directory:

text
mv file.txt /path/to/destination/

Removing Files and Directories with rm and rmdir

The rm command deletes files and directories. Caution is advised, especially with recursive deletion.


Important options:


1. Remove a file:

text
rm filename


2. Remove a directory and its contents recursively:

text
rm -r directory_name


3. Use -i for interactive prompt before deletion.

4. Use -f to force deletion without prompt (use carefully).


The rmdir command can remove empty directories only.


Example:

text
rm -r old_project/

Deletes the directory and all files/folders within.

Symbolic Links with ln

Symbolic links (symlinks) are shortcuts pointing to files or directories.


1. Create a symbolic link with:

text
ln -s target link_name


2. Useful for quick access or linking libraries in development.

Best Practices and Tips

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.