Table of Contents
Understanding Files and Directories on Linux
On Linux, almost everything is represented as a file, and files are organized into a tree of directories (folders). This chapter gives you the practical basics you need at the command line; later chapters will go deeper into searching, permissions, and special filesystem locations.
The Filesystem as a Tree
The Linux filesystem is structured like an upside‑down tree:
- The top of the tree is the root directory:
/ - Directories can contain:
- Regular files
- Other directories (subdirectories)
- Other special file types (covered elsewhere)
A path is how you describe where a file or directory lives in this tree:
/home/alice/Documents/notes.txtis a path- Each
/separates directory levels - The last component is usually the file name (
notes.txt)
You’ll use paths constantly with command‑line tools.
Your Current Location: Working Directory
When you open a terminal, you are “inside” a directory. This is your current working directory.
pwd— print working directory:
pwd
/home/alicecd— change directory:
cd /home/alice/Documents
pwd
/home/alice/Documents
You can only refer to relative paths (e.g. Documents/report.txt) if you know what your current directory is; otherwise you must use absolute paths that start with / (covered more later).
Basic Path Components: `.` and `..`
Two special directory names exist everywhere:
.— the current directory..— the parent directory (one level up)
These are just names you can use in commands:
# Go to the parent directory
cd ..
# Refer to a file in the current directory explicitly
ls ./file.txt
# Go up one directory, then into another
cd ../DownloadsViewing Directory Contents with `ls`
The main tool to see what’s in a directory is ls.
Simple listing
ls
Desktop Documents Downloads Pictures
If no directory is given, ls lists the current directory.
You can list a specific path:
ls /etcLong listing
ls -l shows more detail:
ls -l
total 8
drwxr-xr-x 2 alice alice 4096 Dec 1 10:20 Documents
-rw-r--r-- 1 alice alice 123 Nov 30 09:15 todo.txtYou’ll learn what each column means in the permissions chapter, but for now:
- Entries starting with
dare directories - Entries starting with
-are regular files
Showing hidden files
On Linux, a hidden file is any file whose name starts with a dot (.), e.g. .bashrc.
To see them:
ls -a
. .. .bashrc Documents todo.txtCombine options:
ls -laNavigating Directories with `cd`
cd changes your current working directory.
Common usages:
- Go to your home directory (e.g.
/home/alice):
cd
# or
cd ~- Go to a specific absolute path:
cd /var/log- Go to a path relative to where you are:
cd Documents
cd ../Pictures- Go to the previous directory:
cd -This toggles between the current directory and the one you were in before.
Paths: Absolute vs Relative
You will use two kinds of paths with most commands:
- Absolute path
- Starts with
/ - Always describes the same location, no matter where you are
- Example:
/home/alice/Documents/report.txt - Relative path
- Does not start with
/ - Interpreted relative to your current working directory
- Example: if you are in
/home/alice, then: Documents/report.txtmeans/home/alice/Documents/report.txt../bob/notes.txtmeans/home/bob/notes.txt
You can always convert “Where am I + relative path” to an absolute path:
If pwd prints /home/alice and you run:
ls Documents/report.txtThe shell effectively interprets it as:
ls /home/alice/Documents/report.txtCreating Directories: `mkdir`
To create new directories:
- Single directory:
mkdir projects
ls
projects- Nested directories (parents created automatically):
mkdir -p projects/2025/january
Without -p, mkdir projects/2025/january would fail if projects or 2025 don’t exist.
You can create multiple directories at once:
mkdir dir1 dir2 dir3Creating and Copying Files and Directories
Creating empty files with `touch`
touch updates timestamps on files, and if the file does not exist, it creates an empty one:
touch notes.txt
ls
notes.txtYou can create several:
touch file1.txt file2.txt file3.txtCopying files with `cp`
Basic usage:
cp source_file destination_fileExamples:
- Copy a file in the same directory:
cp notes.txt notes_backup.txt- Copy a file to another directory:
cp notes.txt Documents/
To copy a directory and its contents, use -r (recursive):
cp -r projects/ projects_backup/
It’s common to use -v (verbose) to see what’s happening:
cp -rv projects/ projects_backup/Moving and Renaming with `mv`
mv moves files/directories and also renames them.
- Rename a file in the same directory:
mv notes.txt ideas.txt- Move a file to another directory:
mv ideas.txt Documents/- Move and rename at once:
mv Documents/ideas.txt Documents/old_ideas.txt- Move a directory:
mv projects/ /home/alice/old_projects/Removing Files and Directories with `rm` and `rmdir`
Removing is permanent at the command line; there is no recycle bin.
Removing files with `rm`
- Remove a single file:
rm notes.txt- Remove multiple files:
rm file1.txt file2.txt- Ask for confirmation before each file:
rm -i *.txtRemoving directories
Empty directories:
rmdir emptydirDirectories and their contents (recursive):
rm -r projects_backup/Be very careful with:
rm -r— removes directories recursivelyrm -rf— same as above but forces removal, skipping confirmation and errors
A common safe habit while learning:
- Use
rm -riinstead ofrm -rso you get prompted.
Paths with `~` (Home Directory Shortcut)
~ is a shortcut for your user’s home directory:
- On most systems:
/home/yourusername - On some systems:
/Users/yourusernameor/home/yourusername(concept is the same)
Examples:
cd ~ # go to your home
ls ~/Documents # list Documents in your home
cp file.txt ~/Downloads/
In shells like Bash, ~username refers to another user’s home (if you have permission):
ls ~bobInspecting Paths and File Locations
Sometimes you want to know where exactly something is or what kind of file a path refers to.
Showing the full path with `realpath` or `readlink -f`
If available on your system:
realpath Documents/report.txt
# or
readlink -f Documents/report.txtThese commands resolve relative paths to absolute ones.
File type and basic info with `file`
To see what type of data a file contains (text, binary, image, etc.):
file notes.txt
# notes.txt: ASCII text
file /bin/ls
# /bin/ls: ELF 64-bit LSB pie executable, ...Tab Completion for File and Directory Names
Most shells provide tab completion:
- Type the first few letters of a file or directory name
- Press
Tab - The shell auto‑completes the rest if unambiguous
Example:
- You have
DocumentsandDownloads - Type
cd Do+Tab: - The shell may expand to
cd Docand wait (because there are multiple matches) - Press
Tabtwice to see suggestions - Type more letters (
Docu) and pressTabagain
Tab completion saves time and prevents typos when working with long paths.
Basic Directory Organization Habits
Even as a beginner, a few simple habits make life easier:
- Keep personal files under your home directory (e.g.
~/Documents,~/Projects) - Avoid storing random work in system directories like
/etc,/usr,/bin - Use descriptive names and consistent structure:
~/projects/linux-course/notes.txt~/projects/linux-course/examples/
The tools in this chapter (ls, cd, mkdir, cp, mv, rm) are the foundation. Later chapters will build on these to search, manage permissions, and work with more advanced file operations.