Basic File Operations in Linux

LinuxBeginner
Practice Now

Introduction

Linux organizes data as a tree of directories and files. Commands operate on locations in that tree, so reliable file work begins with three questions: Where am I? What is here? Which path identifies the item I want?

This lab builds those ideas gradually in /home/labex/project/file-basics. You will navigate with absolute and relative paths, inspect visible and hidden entries, create files and directories, select names with wildcards, and safely copy, move, and remove practice files. The final step introduces terminal shortcuts that make these operations faster without changing their meaning.

Locate Your Working Directory

In this step, you will connect the terminal prompt to the directory where commands operate.

The shell always has a current working directory. Relative paths start from that directory, so checking your location prevents many beginner mistakes. Move to this lab's workspace using its complete path:

cd /home/labex/project/file-basics

A path beginning with / is an absolute path. It starts at the filesystem root and identifies the same location regardless of where you began.

Print the working directory:

pwd

You should see:

/home/labex/project/file-basics

Save that location as evidence. The > operator redirects the output into starting-location.txt; because the output goes to the file, nothing is printed this time:

pwd > starting-location.txt

Read the file to confirm what was saved:

cat starting-location.txt

The terminal screen and the file should show the same absolute path.

In this step, you will move through neighboring directories using relative paths and the special names . and ...

Return to the workspace if necessary:

cd /home/labex/project/file-basics

List its visible entries:

ls

You should see path-map and starting-location.txt. Enter path-map using a relative path—a path interpreted from the current directory:

cd path-map
pwd

Now enter north and read its reference file:

cd north
cat reference.txt

The output is north reference. The name .. means the parent directory, one level above the current location:

cd ..

Move from path-map into its south child:

cd south
cat reference.txt

The name . means the current directory. Save the current absolute location into the lab root by combining pwd with a relative destination that climbs two levels:

pwd > ../../relative-location.txt

Return directly to the lab root with its absolute path:

cd /home/labex/project/file-basics
cat relative-location.txt

The saved path should end with /path-map/south. You have used both relative movement and an absolute return path.

Inspect Directory Contents

In this step, you will use ls options to reveal hidden names and file details.

Run a plain listing in the lab root:

cd /home/labex/project/file-basics
ls

Linux normally hides names that begin with a dot. The -a option means “all” and includes those entries:

ls -a

You should now see .practice-note, along with . for the current directory and .. for its parent.

The -l option produces a long listing with permissions, owner, size, time, and name:

ls -l

Combine the options to inspect all entries in long form:

ls -la

Each output line begins with a type and permission field. A leading d identifies a directory; a leading - identifies a regular file. Permissions are taught in depth later.

Save the names, including hidden entries. ls already displays them alphabetically in this controlled workspace, so no additional sorting command is needed yet:

ls -A > all-names.txt

Unlike -a, -A omits the special . and .. entries while retaining other hidden names. Display the result:

cat all-names.txt

The list should include .practice-note, path-map, and the files created in earlier steps.

Create Directories and Files

In this step, you will create a small project tree and place text inside regular files.

mkdir creates a directory. Its -p option also creates missing parent directories, so one command can build a nested path:

cd /home/labex/project/file-basics
mkdir -p workspace/docs workspace/logs

This creates workspace plus its docs and logs children. Inspect the result:

find workspace -maxdepth 2 -type d

touch creates an empty file when the name does not exist. Create two files at once:

touch workspace/docs/guide.txt workspace/logs/app.log

Write content to the guide. A single > replaces the destination with the command's output:

echo "Linux file practice" > workspace/docs/guide.txt

Append a second line with >>, which preserves existing content:

echo "Paths make locations predictable" >> workspace/docs/guide.txt

Read the complete file:

cat workspace/docs/guide.txt

You should see both lines in their original order. The empty app.log also exists, ready for later wildcard practice.

Select Names with Wildcards

In this step, you will create groups of files and select them by filename patterns.

Move into the logs directory:

cd /home/labex/project/file-basics/workspace/logs

Brace expansion generates several literal names before the command runs. Create numbered logs:

touch app{1..3}.log worker{1..2}.log notes.txt

The shell expands {1..3} into 1 2 3, so app{1..3}.log becomes three arguments.

A wildcard is different: it matches names that already exist. * matches any sequence of characters:

ls *.log

The output includes every name ending in .log, but not notes.txt.

The ? wildcard matches exactly one character. List only the numbered app logs:

ls app?.log

The shell expands filename patterns in alphabetical order. printf prints each expanded name on its own line, and > saves those lines:

printf '%s\n' *.log > matched-logs.txt
cat matched-logs.txt

The saved list should contain app.log, app1.log, app2.log, app3.log, worker1.log, and worker2.log. Quoting *.log would prevent wildcard expansion, so the pattern is intentionally unquoted here.

Copy, Move, and Remove Files Safely

In this step, you will compare the effects of cp, mv, and rm before using them in the following challenge.

Return to the workspace directory:

cd /home/labex/project/file-basics/workspace

cp SOURCE DESTINATION creates another file while leaving the source in place. Make a backup copy of the guide:

cp docs/guide.txt docs/guide-backup.txt

Confirm that the two files have identical content. cmp compares two files byte for byte. It prints nothing when they match; an error message would mean they differ:

cmp docs/guide.txt docs/guide-backup.txt

mv SOURCE DESTINATION relocates or renames an item. Move the backup into the workspace root and give it a clearer name:

mv docs/guide-backup.txt archived-guide.txt

List both locations:

ls docs
ls

You should see guide.txt under docs and archived-guide.txt in the current directory.

rm permanently removes a named file; there is normally no terminal recycle bin. Remove only the disposable notes file:

rm logs/notes.txt

List the directory again to make the deletion observable:

ls logs

notes.txt should no longer appear. The original guide and its moved copy remain available, while the one explicitly named disposable file is gone.

Work Faster with Terminal Shortcuts

In this step, you will practice four terminal controls that reduce typing and help recover from a command that keeps running.

First enter the documents directory:

cd /home/labex/project/file-basics/workspace/docs

Run a command you will reuse:

cat guide.txt

Press the Up Arrow once. The previous cat guide.txt command should reappear at the prompt. Press Enter to run it again. Command recall avoids retyping and still lets you edit the recalled line before execution.

Next type cat g without pressing Enter, then press Tab. Because only guide.txt matches, the shell should complete the filename. Press Enter to run the completed command.

Now start a command that waits continuously:

tail -f /dev/null

tail -f follows a file and waits for new data. /dev/null never provides useful lines, so the command appears to sit silently. Press Ctrl+C to send an interrupt and return to the prompt. This is the standard first response when a foreground command should stop.

Press Ctrl+L to clear old output from the visible screen. Like clear, this does not delete files or command history.

Create a completion marker after returning to the prompt:

echo "Shortcuts practiced" > /home/labex/project/file-basics/shortcuts-complete.txt
cat /home/labex/project/file-basics/shortcuts-complete.txt

The marker confirms that you successfully regained the prompt after the interactive practice.

Summary

You learned how the working directory and paths control file operations. You navigated with absolute and relative paths, inspected hidden entries and metadata, created a directory tree and text files, selected names with wildcards, and compared copying, moving, and permanent removal. You also practiced command recall, Tab completion, Ctrl+C, and Ctrl+L. The next lab teaches how to find reliable help when an unfamiliar command or option appears.