Bytes of Awareness

Linux

Linux Files and Directories: A Hands-On Guide

Learn how Linux organizes files and directories and practice navigating and managing them from the terminal.


Understanding the Linux filesystem

Linux organizes information in a hierarchical filesystem. Directories can contain files as well as other directories.

The top of this hierarchy is called the root directory and is represented by /.

/

Absolute and relative paths

An absolute path starts from the root directory.

/home/user/projects

A relative path starts from your current directory.

projects

Find your current directory

Use pwd to see where you currently are.

pwd

List files and directories

The ls command shows the contents of your current directory.

ls

For a more detailed listing, use:

ls -la

Move between directories

Use cd to change your current directory.

cd projects

Move to the parent directory with:

cd ..

To return to your home directory:

cd ~

Create directories

Use mkdir to create a new directory.

mkdir practice

You can also create nested directories with the -p option.

mkdir -p practice/linux/commands

Create files

Use touch to create an empty file.

touch notes.txt

You can create several files at once:

touch one.txt two.txt three.txt

Copy files

Use cp to create a copy of a file.

cp notes.txt notes-backup.txt

Move and rename files

The mv command can move a file into another directory or rename it.

mv notes.txt practice/

To rename a file:

mv notes-backup.txt backup.txt

Remove files

Use rm to remove a file.

rm backup.txt

Be careful when using rm. The command does not provide the same kind of recycle-bin experience you may be used to from a graphical desktop.

Hands-on exercise

Let’s combine the commands you’ve learned.

Create a directory called linux-practice, enter it, create a file, and then make a copy of that file.

mkdir linux-practice
cd linux-practice
touch notes.txt
cp notes.txt notes-backup.txt
ls -la

You should now see both notes.txt and notes-backup.txt.

What’s next?

Once you are comfortable navigating the filesystem, the next step is understanding Linux permissions, ownership, and how users and groups control access to files.