Kahibaro
Discord Login Register

2.2 Files and Directories

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:

A path is how you describe where a file or directory lives in this tree:

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
  /home/alice
  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:

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

Viewing 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 /etc

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

You’ll learn what each column means in the permissions chapter, but for now:

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

Combine options:

ls -la

Navigating Directories with `cd`

cd changes your current working directory.

Common usages:

  cd
  # or
  cd ~
  cd /var/log
  cd Documents
  cd ../Pictures
  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:

You can always convert “Where am I + relative path” to an absolute path:

If pwd prints /home/alice and you run:

ls Documents/report.txt

The shell effectively interprets it as:

ls /home/alice/Documents/report.txt

Creating Directories: `mkdir`

To create new directories:

  mkdir projects
  ls
  projects
  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 dir3

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

You can create several:

touch file1.txt file2.txt file3.txt

Copying files with `cp`

Basic usage:

cp source_file destination_file

Examples:

  cp notes.txt notes_backup.txt
  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.

  mv notes.txt ideas.txt
  mv ideas.txt Documents/
  mv Documents/ideas.txt Documents/old_ideas.txt
  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`

  rm notes.txt
  rm file1.txt file2.txt
  rm -i *.txt

Removing directories

Empty directories:

rmdir emptydir

Directories and their contents (recursive):

rm -r projects_backup/

Be very careful with:

A common safe habit while learning:

Paths with `~` (Home Directory Shortcut)

~ is a shortcut for your user’s home directory:

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 ~bob

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

These 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:

Example:

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:

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.

Views: 123

Comments

Please login to add a comment.

Don't have an account? Register now!