MarzleyTech Learn

Home / Learn / Linux command line / Navigating and managing files: the Linux file system, paths, ls, cd, mkdir, cp, mv, rm, find and wildcards

Navigating and managing files: the Linux file system, paths, ls, cd, mkdir, cp, mv, rm, find and wildcards

Everything on a Linux system lives in one big tree of folders (called directories), starting at /. Before you can manage a web server, edit configs or write scripts, you need to move around this tree confidently and create, copy, move and delete files without a mouse. This unit covers the file system layout and every essential file command, with the mistakes to avoid.

The directory tree

/
├── bin, usr/bin   programs (ls, cp, python3...)
├── etc            system configuration files (nginx, ssh, users)
├── home           users' personal folders (/home/wanjiku)
├── root           the root user's home
├── var            changing data: logs (/var/log), websites (/var/www), databases
├── tmp            temporary files (cleared on reboot)
├── opt            optional/third-party software
├── dev            devices (disks, terminals)
├── proc, sys      live kernel and process information
├── mnt, media     mounted drives and USB sticks
└── boot           the kernel and boot loader

Unlike Windows, there are no drive letters (C:, D:). Extra disks and USB drives are mounted into the tree, e.g. /media/wanjiku/USB.

Paths

Path typeExampleMeaning
Absolute/home/wanjiku/projects/siteStarts from /; works from anywhere
Relativeprojects/siteStarts from where you are now
~~/projectsYour home folder (/home/wanjiku)
../script.shThe current folder
..../imagesThe parent folder (one level up)

Listing: ls

Terminal
ls                 # names in the current folder
ls -l              # long format: permissions, owner, size, date
ls -a              # include hidden files (names starting with .)
ls -lh             # human-readable sizes (K, M, G)
ls -lt             # newest first
ls -R              # include sub-folders
ls /var/log        # list another folder

Reading ls -l:

-rw-r--r-- 1 wanjiku wanjiku 2048 Sep  7 10:15 index.html
drwxr-xr-x 2 wanjiku wanjiku 4096 Sep  7 10:16 images
PartMeaning
- / d / lRegular file / directory / symbolic link
rw-r--r--Permissions (owner, group, others): next lessons
wanjiku wanjikuOwner and group
2048Size in bytes
Sep 7 10:15Last modified

Moving around: cd

Terminal
cd /var/log        # absolute path
cd projects        # relative path
cd ..              # up one level
cd ../..           # up two levels
cd ~  (or just cd) # home
cd -               # back to the previous folder

Use Tab to complete folder names. Names with spaces need quotes or a backslash: cd "My Documents" or cd My\ Documents. Better yet, avoid spaces in names; use my-documents.

Creating files and folders

Terminal
mkdir website                       # one folder
mkdir -p website/css website/js     # -p creates parents and doesn't complain if they exist
touch website/index.html            # create an empty file (or update its timestamp)
echo "Hello" > notes.txt            # create a file with content

Copying: cp

Terminal
cp index.html index-backup.html         # copy a file
cp index.html website/                  # copy into a folder
cp -r website website-backup            # copy a folder (-r = recursive)
cp -i a.txt b.txt                       # ask before overwriting
cp -v *.jpg images/                     # verbose: show each file copied

Moving and renaming: mv

Terminal
mv old-name.html new-name.html          # rename
mv report.pdf ~/Documents/              # move
mv *.png images/                        # move many files
mv -i a.txt folder/                     # ask before overwriting

There's no separate rename command for single files: renaming is moving to a new name.

Deleting: rm and rmdir

Terminal
rm file.txt              # delete a file
rm -i *.log              # ask for each file
rmdir empty-folder       # delete an empty folder
rm -r old-site           # delete a folder and everything inside

Wildcards (globbing)

PatternMatches
*Any characters: *.jpg = all JPG files
?Exactly one character: file?.txt = file1.txt, fileA.txt
[abc]One of these: [0-9]*.csv starts with a digit
{a,b}Each alternative (brace expansion): mkdir {css,js,img}
Terminal
ls *.html
cp report-2026-0?.pdf archive/
mkdir -p project/{src,docs,tests}
rm -i *.tmp

Finding files

Terminal
find ~ -name "*.pdf"                       # by name under home
find /var/www -type f -name "*.php"        # files only
find . -type d -name "node_modules"        # folders
find /var/log -mtime -1                    # modified in the last day
find . -size +100M                         # larger than 100 MB
find . -name "*.tmp" -delete               # find and delete (check without -delete first!)

locate is faster (it searches a database updated daily): sudo apt install plocate, then locate nginx.conf.

Hidden files, types and links

  • Files starting with . are hidden: .bashrc (shell settings), .ssh/ (keys), .git/, .env.
  • file photo.jpg tells you what a file really is, regardless of its extension.
  • A symbolic link is a shortcut: ln -s /var/www/site/current ~/site creates ~/site pointing to the real folder.

Disk usage quick checks

Terminal
du -sh website/          # size of a folder
du -sh * | sort -h       # sizes of everything here, smallest to largest
df -h                    # free space on each disk

Practice task

  1. In your home folder, create practice/{docs,images,backup} in one command.
  2. Create docs/notes.txt and docs/todo.txt.
  3. Copy all .txt files to backup/.
  4. Rename docs/todo.txt to docs/tasks.txt.
  5. Use find to list every .txt file under practice.
  6. Delete the backup folder (check with ls first).
Think about it: You're in /home/wanjiku/projects/site/css. Write two different commands to go to /home/wanjiku/projects/site/images.Show answer

Relative: cd ../images. Absolute: cd /home/wanjiku/projects/site/images (or cd ~/projects/site/images).

Summary

  • Linux has one directory tree from /: /etc configs, /home users, /var/log logs, /var/www websites.
  • Absolute paths start with /; relative paths start from the current folder; ~, ., .. are shortcuts.
  • ls -lah, cd, mkdir -p, touch, cp -r, mv, rm -r manage files; rm is permanent.
  • Wildcards (*, ?, [ ], { }) act on many files; preview with ls first.
  • find searches by name, type, size and age; du and df check space.

Check yourself

  1. Which folder holds most system configuration files?

    Show answer

    /etc

  2. Which symbol means the parent folder?

    Show answer

    ..

  3. Which option makes cp copy a whole folder?

    Show answer

    -r

  4. Which mkdir option creates parent folders as needed?

    Show answer

    -p

  5. Which wildcard matches exactly one character?

    Show answer

    ?

  6. Does rm move files to a Recycle Bin? (yes/no)

    Show answer

    no

Lesson 2 of 12 in Linux command line · Printable course notes