DEV Community

Aryan Vaishnani
Aryan Vaishnani

Posted on

File Management (touch, mkdir, cp, mv, rm, ln)

File management commands are used to:

  1. Create files
  2. Create directories
  3. Copy files
  4. Move files
  5. Delete files
  6. Create links

Most commonly used commands:

  1. touch
  2. mkdir
  3. cp
  4. mv
  5. rm
  6. ln

These commands are heavily used in:

  • Linux administration
  • DevOps
  • Cloud engineering
  • Automation
  • Application deployment

1. touch Command

Purpose

Creates empty files or updates file timestamps.

Syntax

touch filename

Example

touch app.log

Creates:

app.log

Create Multiple Files

touch file1.txt file2.txt file3.txt

Real-World Usage

Create Log File

touch /var/log/custom.log

Create Configuration Placeholder

touch nginx.conf

2. mkdir Command

Purpose

Creates directories.

Syntax

mkdir directory_name

Example

mkdir projects

Creates:

projects/

Create Multiple Directories

mkdir dev test prod

Create Nested Directories

mkdir -p app/backend/config

Creates:

app/
└── backend/
└── config/

Real-World Usage

Deployment Structure

mkdir -p /opt/app/logs

Kubernetes Manifest Folder

mkdir -p k8s/deployments

3. cp Command

Purpose

Copies files and directories.

Syntax

cp source destination

Example

cp file.txt backup.txt

Copies:

file.txt → backup.txt

Copy Directory

cp -r project backup_project

  • r = recursive

Important Options

Option Purpose
-r Copy directories
-v Verbose output
-p Preserve permissions
-i Ask before overwrite

Real-World Usage

Backup Configuration

cp /etc/nginx/nginx.conf nginx.conf.bak

Copy Deployment Files

cp -r build/ /var/www/html/

4. mv Command

Purpose

Moves or renames files/directories.

Syntax

mv source destination

Rename File

mv old.txt new.txt

Move File

mv app.log /var/log/

Move Directory

mv project /opt/

Real-World Usage

Rotate Logs

mv app.log app.log.old

Move Deployment Files

mv release/ /opt/application/

5. rm Command

Purpose

Deletes files and directories.

Syntax

rm filename

Example

rm file.txt

Deletes:

file.txt

Remove Directory

rm -r project

Important Options

Option Purpose
-r Recursive delete
-f Force delete
-i Ask before deleting

Real-World Usage

Remove Old Logs

rm -f old.log

Remove Temporary Files

rm -rf /tmp/cache

Dangerous Command

rm -rf /

This can destroy the entire Linux system.

Never run unknown rm -rf commands.

6. ln Command

Purpose

Creates links between files.

Two types:

  1. Hard link
  2. Symbolic (soft) link

Symbolic Link

Syntax

ln -s target link_name

Example

ln -s /var/log/app.log applog

Creates:

applog -> /var/log/app.log

Hard Link

ln file1 hardlink1

Difference Between Hard and Soft Links

Hard Link Soft Link
Direct inode reference Points to file path
Cannot cross filesystems Can cross filesystems
Works even if original deleted Breaks if original deleted

Real-World Usage

Application Symlink

ln -s /opt/app/current app

Common in deployments.

Shared Configuration

ln -s /etc/nginx/sites-enabled/app.conf app.conf

Top comments (0)