DevOpsLesson
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Linux Tutorial

Introduction to Linux
Linux Filesystem
Linux Navigation
Linux File Operations
Linux Text Processing
Linux Permissions
Linux Users and Groups
Linux Process Management
Linux systemd Basics
Linux Networking
Linux Disk and Storage
Linux Bash Scripting
Linux Package Management
Linux Environment Variables
Linux Cron Jobs
Linux SSH Keys
Linux Firewall Basics
Linux Log Management
Linux Performance Monitoring and Troubleshooting

Linux File Operations

PreviousPrev
Next

Learn how to create, copy, move, remove, link, and inspect files in Linux with safe, practical examples.

Working with Files Safely

Linux gives you powerful file commands, but power requires care. A single rm -r in the wrong location can remove important data. The good news is that the core file operations are consistent and easy to learn once you practice them.

This lesson covers the commands you will use constantly in development, troubleshooting, and server administration.

Creating Files and Directories

Use mkdir to create directories.

mkdir project
mkdir -p project/config/environments

The -p flag creates parent directories as needed and does not fail if parts already exist.

Use touch to create an empty file or update a file’s timestamp.

touch notes.txt
touch app.log

Tip: mkdir -p is very common in automation because it is safe for repeated runs.

Copying Files with cp

The cp command copies files or directories.

cp app.conf app.conf.bak
cp -r site/ site-backup/
cp -v *.log archive/

Useful flags:

  • -r copies directories recursively
  • -v shows what is being copied

A common admin pattern is to back up a config before editing it:

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

Moving and Renaming with mv

The mv command moves or renames files.

mv draft.txt final.txt
mv report.txt docs/

If the target is a directory, the file is moved into it. If the target is a new filename in the same directory, the file is renamed.

Removing with rm

The rm command deletes files. It does not move them to a recycle bin.

rm old.log
rm -r old-directory
rm -i important.txt

Useful flags:

  • -r removes directories recursively
  • -i asks for confirmation

Note: Many production mistakes start with removing the wrong path. Double-check with pwd and ls before destructive commands.

Reading File Content

cat

cat prints a file’s full contents.

cat /etc/hosts
cat app.log

It is best for small files.

head

head shows the first lines of a file.

head app.log
head -n 20 app.log

tail

tail shows the last lines of a file.

tail app.log
tail -n 50 app.log
tail -f /var/log/nginx/access.log

tail -f is especially useful for watching logs as new entries appear.

less

less lets you scroll through file contents interactively.

less /var/log/syslog

Use the arrow keys or page navigation, type /error to search, and press q to quit.

Links: Hard Links and Symbolic Links

The ln command creates links.

Hard Links

A hard link is another directory entry that points to the same underlying file data.

ln original.txt hardlink.txt

Both names refer to the same inode. Deleting one name does not remove the data as long as another hard link still exists.

Symbolic Links

A symbolic link or soft link points to another path.

ln -s /var/log/nginx/access.log access-current.log

Symbolic links are more flexible because they can point across filesystems and to directories.

Tip: In DevOps, symbolic links are often used for versioned deployments, like linking current to the active release directory.

A Practical Example

Suppose you are preparing a simple app directory:

mkdir -p myapp/{config,logs,bin}
touch myapp/config/app.conf
cp myapp/config/app.conf myapp/config/app.conf.bak
mv myapp/config/app.conf.bak myapp/config/app.conf.old
ln -s myapp/logs latest-logs

You created a structure, added a file, made a backup, renamed it, and created a symbolic link.

Choosing the Right Command

  • Use cp when you need a duplicate
  • Use mv when you want to rename or relocate
  • Use rm when you are sure you want permanent removal
  • Use cat, head, tail, or less depending on file size and what part you need
  • Use ln -s when you want a flexible pointer to another path

Beginners often overuse cat for huge logs. On large files, less or tail is usually better.

Test Your Understanding

Let's see how well you understood the concepts! These exercises will help reinforce what you just learned.

Exercise 1: Creating directory trees

Question: Which command from the lesson creates project/config/environments in one step without failing if parent directories are missing?

Exercise

Exercise 1: Creating directory trees

Choose the mkdir form that safely creates nested parent directories.

Exercise 2: Following a growing file

Scenario: You want to watch new Nginx access log lines appear during troubleshooting.

Question: Which command is the best match?

Exercise

Exercise 2: Following a growing file

Identify the command that continues showing new lines added to a log file.

Exercise 3: Using flexible links

Question: Why would ln -s be a good fit for a current release directory in a deployment?

Exercise

Exercise 3: Using flexible links

Choose the reason symbolic links are useful in versioned deployments.

PreviousPrev
Next

Continue Learning

Introduction to Linux

Understand what Linux is, how the kernel, distribution, and shell relate, and why Linux matters in DevOps.

12 min·Easy

Linux Filesystem

Learn how the Linux filesystem is organized, what key directories do, and how to work with absolute and relative paths.

15 min·Easy

Linux Navigation

Practice moving around the Linux command line with pwd, ls, cd, tree, file, which, whereis, man, and help.

12 min·Easy

Explore Related Topics

Do

Docker Tutorials

Run and manage containers on Linux

Gi

Git Tutorials

Master Git version control on the command line

Try the Tool

Cron Parser

Parse and validate cron expressions with a visual schedule preview.

Regex Tester

Test and debug regular expressions used in Bash scripts and configs.

On This Page

Working with Files SafelyCreating Files and DirectoriesCopying Files with `cp`Moving and Renaming with `mv`Removing with `rm`Reading File Content`cat``head``tail``less`Links: Hard Links and Symbolic LinksHard LinksSymbolic LinksA Practical ExampleChoosing the Right CommandTest Your UnderstandingExercise 1: Creating directory treesExercise 2: Following a growing fileExercise 3: Using flexible links