Linux File Operations

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 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.

The ln command creates 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.

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.

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.

Continue Learning

Explore Related Topics

Try the Tool

Related Resources