File and Directory Operations

LinuxBeginner
Practice Now

Introduction

Linux organizes files in one directory tree that begins at /, the root directory. Every file and directory has a path that describes its location in that tree. Learning to read and use paths makes nearly every later Linux task easier.

In this lab, you will build a small workspace while learning the operations used in everyday terminal work. You will navigate directories, create and edit files, copy and move data, find files, inspect metadata and inodes, experiment with hard and symbolic links, and compare files and directory trees with diff.

Explore the Linux Directory Tree

In this step, you will identify your current location, inspect important top-level directories, and create a dedicated workspace for the rest of the lab.

The pwd command means "print working directory." Run it first so that you know where the terminal started:

pwd

The result should be /home/labex/project. An absolute path begins with / and describes a location from the root of the filesystem.

List the top level of the filesystem:

ls /

You will see standard directories such as etc, home, tmp, usr, and var. Their common purposes are:

  • /etc stores system-wide configuration.
  • /home stores ordinary users' home directories.
  • /tmp stores temporary data.
  • /usr contains many installed programs and shared resources.
  • /var contains changing data such as logs and caches.

The -d option tells ls to describe the named directories instead of listing their contents. The -l option selects the long format:

ls -ld /etc /home /tmp /usr /var

The first character of each line is d, showing that the object is a directory. The remaining columns contain permissions, ownership, size, time, and name.

Create a workspace for this lab. The -p option lets mkdir create missing parent directories and avoids an error if the directory already exists:

mkdir -p /home/labex/project/file-ops-lab

Move into it and confirm your new location:

cd /home/labex/project/file-ops-lab
pwd

The final output should be /home/labex/project/file-ops-lab.

In this step, you will create a nested directory tree and move through it using absolute paths, relative paths, and navigation shortcuts.

Make sure you begin in the lab workspace:

cd /home/labex/project/file-ops-lab

Create three nested directories in one command:

mkdir -p navigation/alpha/beta

Here, navigation/alpha/beta is a relative path because it does not start with /. The shell resolves it from your current directory.

Use an absolute path to enter the deepest directory:

cd /home/labex/project/file-ops-lab/navigation/alpha/beta
pwd

The special path .. means the parent directory. Move from beta to alpha:

cd ..
pwd

The output should end in /navigation/alpha. The cd - command returns to the previous working directory:

cd -
pwd

You should be back in beta. Finally, ~ represents your home directory. Use it as the beginning of a path to return to the lab workspace:

cd ~/project/file-ops-lab
pwd

Absolute paths work from any location. Relative paths are shorter when you already know your current location.

Create Directories and Write Files

In this step, you will create directories and files, then learn the difference between overwriting and appending with shell redirection.

Start in the lab workspace and create two directories at once:

cd /home/labex/project/file-ops-lab
mkdir documents archive

The touch command creates an empty file when the named file does not exist:

touch documents/empty.txt

Inspect the empty file with the long listing format:

ls -l documents/empty.txt

Its size should be 0 bytes. Next, > redirects command output into a file. It creates the file or replaces its previous contents:

echo "Linux file practice" > documents/notes.txt

The >> operator appends output without removing the existing content:

echo "Paths identify each filesystem object" >> documents/notes.txt

Display the complete file with cat:

cat documents/notes.txt

You should see two lines in their original order. Use wc -l to count them; -l asks wc to count lines:

wc -l documents/notes.txt

The first field should be 2.

Copy, Move, Rename, and Remove Files

In this step, you will practice the four common ways that filesystem objects change: copying, moving, renaming, and removing.

Return to the lab workspace:

cd /home/labex/project/file-ops-lab

The cp command copies a source to a destination. Copy notes.txt into the archive with a new name:

cp documents/notes.txt archive/notes-backup.txt

Confirm that the source and copy both exist:

ls -l documents/notes.txt archive/notes-backup.txt

Create a temporary file, then use mv to move and rename it in one operation:

echo "draft" > documents/draft.txt
mv documents/draft.txt archive/final-draft.txt

The old path should no longer exist, while the new path does:

ls -l archive/final-draft.txt

To copy a directory and all of its contents, use the recursive -r option:

cp -r documents archive/documents-copy
find archive/documents-copy -maxdepth 1 -type f

The rm command permanently removes files; there is no terminal recycle bin. Create and remove a disposable file:

touch disposable.tmp
rm disposable.tmp

List the current directory to confirm that the path is gone:

ls

disposable.tmp should not appear. Always check a path carefully before using rm.

View, Update, and Find Files

In this step, you will create a multiline file, inspect different parts of it, update it with append redirection, and find it by name.

Return to the lab workspace. A here-document sends several lines to a command until the shell reaches a line containing only the chosen marker, here EOF:

cd /home/labex/project/file-ops-lab
cat > documents/journal.txt <<'EOF'
First entry: created a workspace
Second entry: practiced paths
Third entry: copied files
EOF

Quoting 'EOF' prevents the shell from expanding special characters inside the document. Display the file with line numbers:

nl -ba documents/journal.txt

The -ba option numbers all lines. Use head -n 2 for the first two lines and tail -n 1 for the last line:

head -n 2 documents/journal.txt
tail -n 1 documents/journal.txt

Append a fourth entry. You used >> earlier; it adds output to the end without replacing the existing three lines:

echo "Fourth entry: reviewed the results" >> documents/journal.txt

Check the result:

tail -n 2 documents/journal.txt

Now search the workspace for text files. In find, . means the current directory, -type f limits results to regular files, and -name matches a filename pattern. Quotes prevent the shell from expanding *.txt before find receives it:

find . -type f -name "*.txt"

The output should include ./documents/journal.txt along with the other text files you created.

Inspect File Types, Metadata, and Inodes

In this step, you will look beyond a filename and inspect the information Linux stores about a filesystem object.

Begin in the lab workspace and ask file to identify two different objects:

cd /home/labex/project/file-ops-lab
file documents/journal.txt /bin/ls

The journal is identified as text, while /bin/ls is identified as an executable program. Linux does not rely only on filename extensions to identify data.

The stat command displays metadata such as size, permissions, owner, timestamps, and inode number:

stat documents/journal.txt

An inode is the filesystem record that stores a file's metadata and points to its data. A filename is a directory entry that refers to an inode. Use a custom stat format to show stable fields:

stat -c 'name=%n type=%F size=%s inode=%i links=%h' documents/journal.txt

The %i field is the inode number, while %h is the number of hard links that refer to that inode.

The -i option of ls also shows inode numbers. Create a metadata practice file and inspect it:

echo "inode practice" > metadata-sample.txt
ls -li metadata-sample.txt

The number at the beginning of the line is the inode number. It may differ between environments, so focus on what the field represents rather than memorizing its value.

In this step, you will create both kinds of Linux links and observe how each behaves when the original filename is removed.

Create a directory and a source file for the experiment:

cd /home/labex/project/file-ops-lab
mkdir links
echo "shared inode data" > links/source.txt

The ln command without an option creates a hard link. Both names refer to the same inode and file data:

ln links/source.txt links/hard-link.txt

The -s option creates a symbolic link, sometimes called a symlink. A symbolic link stores a path to another file:

ln -s source.txt links/symbolic-link.txt

Compare inode numbers and link information:

ls -li links

source.txt and hard-link.txt should have the same inode number. The symbolic link has its own inode and displays -> source.txt.

Use readlink to display the path stored inside the symbolic link:

readlink links/symbolic-link.txt

Remove the original filename:

rm links/source.txt

The hard link still reaches the inode and its data:

cat links/hard-link.txt

The symbolic link now points to a name that no longer exists. Try to read it:

cat links/symbolic-link.txt

The No such file or directory error is expected and is useful evidence: the link object remains, but its stored target path is broken. This experiment shows why deleting one hard-link name does not delete the data while another hard link remains, whereas a symbolic link depends on its stored target path.

Compare Files and Directories with Diff

In this step, you will use diff to locate differences and then synchronize two small directory trees.

Create two version directories:

cd /home/labex/project/file-ops-lab
mkdir -p comparison/version-a comparison/version-b

Create configuration files that differ on one line:

printf 'mode=production\nport=8080\n' > comparison/version-a/app.conf
printf 'mode=production\nport=9090\n' > comparison/version-b/app.conf

The -u option asks diff for a unified view with context around each change:

diff -u comparison/version-a/app.conf comparison/version-b/app.conf

diff marks lines from the first file with - and lines from the second with +. Its nonzero exit status means the files differ; this is expected.

Make the second file match the first, then use -q for a quiet equality check:

cp comparison/version-a/app.conf comparison/version-b/app.conf
diff -q comparison/version-a/app.conf comparison/version-b/app.conf

No output means the files match. Now add a file to only one directory:

echo "release notes" > comparison/version-a/README.txt

The -r option recursively compares directory trees:

diff -r comparison/version-a comparison/version-b

The output reports that README.txt exists only in version-a. Copy it and compare again:

cp comparison/version-a/README.txt comparison/version-b/README.txt
diff -r comparison/version-a comparison/version-b

The final command should produce no output, proving that both directory trees now contain the same file contents.

Summary

You practiced the core file and directory operations used in Linux terminal work. You navigated with absolute and relative paths, created and edited files, copied and moved data, removed a disposable file safely, and searched by filename.

You also inspected file types and metadata, learned how filenames relate to inodes, compared hard links with symbolic links, and used diff to compare both individual files and complete directory trees. These concepts prepare you to reason about Linux files instead of treating commands as isolated recipes.