Navigating the Linux Filesystem

The Linux Filesystem Hierarchy: Understanding /, /home, /etc, /var, /bin, and More
The Root (/)
Linux has no concept of drives and Windows/macOS file systems
everything on the system is in a tree
top of the tree is the root
standard sudirectories of root
NameMeaningDetails
/binBinariesthe essential tools needed to run the system
/boot Boot Loader contains the static files required to start (boot) the computer
houses the Linux kernel
treat as 'No Entry' zone
/dev devices everything is a file
hard drive is a file
mouse is a file
webcam is a file
to talk to hardware apps write data to the files
/etc Etcetera/Configuration the nerve center of the system
contains system-wide configuration files
/home User Directories every user on the system is given a directory inside /home
if username is "anna," the home directory is /home/anna
only place where permission to write files is by default
/lib Libraries shared code used by programs in /bin
similar to .dll files in Windows
/media and /mnt Mount Points where external drives appear
  • /media - usually where the system automatically attaches USB and external hard drives when they are plugged in
  • /mnt - traditionally used by administrators to manually attach (mount) filesystems temporarily
/optOptionalused for third-party software which doesn't fit the standard filesystem structure
/proc Processes virtual file system
created in memory by the kernel
contains information about running apps
/root The Root User's Home the home directory for the user named "root" (the Superuser)
regular users cannot open this directory
the administrator's private office
/run Runtime Data storage for information that describes the system since it was booted
data is deleted when the computer is restarted
/sbin System Binaries similar to /bin
tools meant for system administration (like repairing disks or configuring networks)
regular users can usually run them but they often require sudo to work
/srv Service Data if running a web server or an FTP server, the data intended to be served to the public is often stored here
/sys System virtual filesystem similar to /proc
interacting with the kernel's view of the hardware
/tmpTemporaryany file put here is deleted when the computer reboots
/usr User System Resources one of the largest directories
contains user-installed applications, documentation, and source code
  • /usr/bin - most programs live here
  • /usr/share - contains shared data such as icons and fonts
/var Variable Data contains files that are expected to grow and change size over time
  • /var/log - contains system logs
  • /var/spool - contains print and mail queues
  • /var/www - often used for web server files
Absolute vs. Relative Paths
The Absolute Path
full and complete path starting a the /root
unambiguous
always starts with a forward slash (/)

The Relative Path
never starts with a forward slash (/)
if the folder is /home/anna with a subfolder named Documents, get to Documents folder using
cd Documents
path is relative to current position in the tree

Essential Navigation Commands: ls, cd, pwd
Where Am I (pwd)
provides current folder as an absolute path

Look Around (ls)
lists contents of current directory

Change Directory (cd)
to move down from /home/anna to Documents subfolder
cd Documents
pwd command returns
/home/anna/Documents
to move to the root
cd /
pwd command returns
/
Understanding Directory Structure and Organization
Linux hierarchy is defined by the FHS (Filesystem Hierarchy Standard)
standard ensures consistency
regardless of distribution the configuration files are always in /etc and the logs are always in /var/log
structure allows for
  1. Security - separating user data from system data ensures a clumsy user cannot accidentally delete the operating system
  2. Multi-user Support - multiple users can share an app in /user/bin personal data is separate and private
  3. Network Storage - filesystem is a logical tree
    parts can be stored on different hard drives or computers
    appear as just another folder to the user

Special Directories: . (current), .. (parent), ~ (home)
The Tilde (~)
represents user's home folder
to get to home folder use command
cd ~
just entering cd does the same thing

The Dot (.)
represents the current directory
to run a script in the current folder use the command
./<script name>.sh
The Double Dot (..)
represents the current folder's parent folder

The Dash (-)
to return to the previous directory use the command
cd - 
Listing Files and Directories: Options for ls (-l, -a, -h)
Seeing the Details
-l flag stands for long listing
output will be similar to
drwxr-xr-x 2 anna anna 4096 Oct 27 10:00 Documents
  • drwxr-xr-x - permissions
    • first letter tells the type
      d is the folder/file
      l is a link
    • the rest determines who can read, write or execute the file
  • 2 - number of hard links (to be covered)
  • anna - user who owns the file
  • anna - group who owns the file
  • 4096 - file size in bytes
  • Oct 27 10:00 - date and time of last file modification
  • Documents - name of file/folder

Seeing Hidden Files (-a)
in Linux a file is hidden simply if its name starts with a dot (.)
a home directory is full of hidden configuration files which store settings for apps
to include hidden files and folders use the command
ls -a
files like .bashrc, .profile, or folders like .config will be included in the listing
deleting hidden files can reset the user's settings

Human Readable Sizes (-h)
by default file sizes a presented as bytes
58392012 bytes is not very useful
the -h flag stands for Human Readable
converts bytes to
  • Kilobytes (K)
  • Megabytes (M)
  • Gigabytes (G)

using the command
ls -lh
will show the file size as 4.0K or 56M

Sorting Ouput
by default the output is sort alphabetically
to sort by Time (newest first) use -t
to reverse sort order use -r
command combination is
ls -lrt
long format (-l), reversed (-r), by time (-t)

Reading File Contents: cat, less, more, head, tail
use in a terminal
cat
to view a file
dumps content to the screen
cat /etc/hostname
works well with small files

less
allows paging through file contents
less /etc/adduser.conf
  • navigation - use Up/Down arrows to scroll line by line
    use the spacebar to scroll down a page
    use 'b' to scroll up a page
  • search - enter command and press Enter
    /<word to search for>
    press 'n' to go to the next match
  • exit - press 'q' and Return

head
displays the first 10 lines of a file
head /etc/adduser.conf
to display a different number of lines use the -n flag
to show the first five lines
head -n 5 /etc/adduser.conf
tail
displays the last 10 lines of a file
useful for log files where newest entry is at the end of the file
tail /etc/adduser.conf
tail -f
-f flag stands for follow
tail -f displays additional lines as they are added to the file
tail -f /var/log/auth.log
Summary
Linux uses a single tree
absolute path starts with forward slash (/)
relative path does not start with forward slash (/)
cd .. moves up to parent folder
cd ~ moves to user's home folder
ls -l lists files with details
ls -a include hidden files in list
less best tool for reading large files
tail -f follows a file in real time

index