After the Bash environment is ready in Chapter 1, the next step is learning to move around and work within the filesystem via the command line. This chapter covers the Linux directory structure, basic commands for changing locations and managing files (pwd, cd, ls, cp, mv, rm, mkdir, touch), methods for reading file contents directly from the terminal (cat, less, head, tail), to ways of finding help when you forget a command's syntax (man, --help, tldr). Mastering these commands is a essential prerequisite before moving into Chapter 3 regarding variables, because almost all Bash scripts ultimately manipulate files and directories through these basic commands.
2.1 Linux Directory Structure and Filesystem Hierarchy
Unlike Windows, which uses separate drive letters like C:\ and D:\, the Linux system has a single directory tree structure starting from the root directory, represented by a forward slash (/). All files, directories, and even hardware devices such as hard drives and flash drives, attach to a point within this directory tree through a process called mounting.
2.1.1 Filesystem Hierarchy Standard (FHS)
The directory layout in Linux is not random. Most distributions follow the Filesystem Hierarchy Standard (FHS), a specification regulating which directories must exist on a Unix-like system and what each is used for. This standard allows us to move from one Linux distribution to another, or from server to server, without relearning where configuration files or program binaries are stored.
In practice, knowing the FHS well is a key indicator of an experienced Sysadmin: when troubleshooting an unfamiliar server, a Sysadmin knows to look for log files in /var/log, rather than guessing or searching through the entire filesystem.
2.1.2 Essential Directories to Know
The following table summarizes the top-level directories most commonly used daily, both for regular users on desktop systems and Sysadmins on servers.
| Directory | Function | Example Contents |
|---|---|---|
/ | Root, the starting point of the entire filesystem | All other directories branch from here |
/bin, /usr/bin | Basic binaries/commands accessible to all users | ls, cp, bash |
/sbin, /usr/sbin | System administration binaries, usually requiring sudo | useradd, iptables |
/etc | System and application configuration files | /etc/passwd, /etc/ssh/sshd_config |
/home | Home directory for regular users | /home/budi |
/root | Home directory specifically for the root user | (empty / root-specific files) |
/var | Frequently changing data (variable data) | /var/log, /var/www |
/tmp | Temporary files, usually cleared automatically | Application cache files |
/opt | Third-party add-on software outside the package manager | Vendor-specific applications |
/dev | Device files representing hardware components | /dev/sda, /dev/null |
/proc | Virtual filesystem containing kernel and process information | /proc/cpuinfo |
/mnt, /media | Mount points for additional storage devices | Flash drive mount points, extra partitions |
An important note for modern distributions: many recent Linux distros (including Fedora, Arch Linux, and newer versions of Debian/Ubuntu) implement usr-merge, making /bin, /sbin, and /lib symlinks pointing to /usr/bin, /usr/sbin, and /usr/lib. This change is transparent in daily usage, but it is good to know so you are not confused when discovering that both paths lead to the same physical location.
2.1.3 Absolute Path vs Relative Path
An absolute path is the complete address of a file or directory referenced from the root (/), for example /home/budi/dokumen/laporan.txt. This type of path always points to the exact same location, regardless of the directory in which the command is executed.
A relative path is an address calculated relative to the current active directory (current working directory), for example dokumen/laporan.txt without a leading slash. Two special symbols are frequently used in relative paths: a single dot (.) refers to the current working directory itself, while a double dot (..) refers to the parent directory one level above.
In production environments, Bash scripts written to run via cron should always use absolute paths. The working directory when a script is run by cron often differs from the author's assumption, which is one of the most common pitfalls causing scripts to work on local machines but fail on servers (covered in detail in Chapter 18).
2.1.4 Exploring Structure with ls
The fastest way to understand the FHS is by exploring it directly. Run the following command to view the contents of the root directory.
ls /The output displays top-level directories such as bin, etc, home, var, and so on, matching the breakdown in section 2.1.2. Section 2.2 will discuss the ls command along with its flags in more detail.
2.2 Basic Commands: pwd, cd, ls, cp, mv, rm, mkdir, touch
These eight commands are the core tools used for interacting with the filesystem via terminal. All of them are part of GNU coreutils (except cd, which is a shell builtin rather than an external program), making them available out of the box on virtually any Linux system.
2.2.1 pwd and cd: Locating and Navigating Directories
pwd (print working directory) displays the absolute path of the current active directory.
pwdcd (change directory) is used to change the current directory. This command accepts either absolute paths or relative paths as arguments.
cd /var/log
cd dokumen/proyek
cd ..
cd ~The tilde symbol (~) is a shortcut for the logged-in user's home directory, so cd ~ is equivalent to cd /home/budi. Executing cd without any arguments will also return you to your home directory. Another practical trick: cd - switches back to the previous directory, useful for toggling between two working locations.
cd -2.2.2 ls: Listing Directory Contents
ls (list) lists the contents of a directory. The three most common options are -l for long listing format (showing permissions, ownership, file size, and modification date), -a to show hidden files (names starting with a dot), and -h to render file sizes in human-readable units (KB, MB, GB).
ls -lahThese flags can be combined into a single argument as shown above, a common habit among Sysadmins because it yields comprehensive information in a single command execution.
2.2.3 mkdir and touch: Creating Directories and Files
mkdir (make directory) creates new directories.
mkdir proyek-baruTo create nested directory structures in one go (such as proyek/src/utils where parent directories do not exist yet), use the -p (parents) flag. Without this flag, mkdir fails with an error if intermediate directories are missing.
mkdir -p proyek/src/utilstouch creates a new empty file if the specified file name does not exist, or updates the modification timestamp of an existing file without altering its contents.
touch catatan.txt2.2.4 cp and mv: Copying and Moving
cp (copy) copies files or directories. Its basic syntax is cp [source] [destination].
cp laporan.txt laporan-backup.txtCopying a directory along with all its contents requires the -r (recursive) flag, as cp refuses to copy directories by default.
cp -r proyek/ proyek-backup/mv (move) moves files or directories. It is also used to rename files, as renaming is technically moving a file to a new name in the same location.
mv catatan.txt catatan-lama.txt
mv laporan.txt arsip/Both cp and mv support the -i (interactive) flag, which prompts for confirmation before overwriting an existing destination file. Utilizing this flag is good practice to prevent accidental file overwrites.
cp -i laporan.txt arsip/laporan.txt2.2.5 rm: Removing Files and Directories
rm (remove) deletes files. Directories require the -r flag to be deleted alongside their contents, following the same pattern as cp.
rm catatan-lama.txt
rm -r proyek-backup/This command demands extra caution due to its destructive nature. Unlike deleting files via a graphical file manager where items are moved to a Trash or Recycle Bin for easy restoration, rm immediately removes files from the filesystem without a built-in recovery mechanism. Combining -rf flags (recursive and force, which suppresses confirmation prompts) is widely used in automation scripts, but it is also a leading cause of severe data loss incidents when paths are mistyped (e.g., accidental spaces placed between target paths and options).
A safe operational practice is running ls against the target path before executing rm, especially when using wildcards like the asterisk (*), ensuring that only intended files will be removed.
ls *.tmp
rm *.tmp2.2.6 Verification and General Troubleshooting
A recommended habit after executing commands that modify the filesystem is verifying the results using ls -l, rather than assuming success without confirmation.
mkdir -p proyek/src/utils
ls -l proyek/srcRecognizing common error messages when learning these commands is crucial. The error mkdir: cannot create directory 'proyek/src/utils': No such file or directory occurs when parent directories do not exist and the -p flag was omitted, as discussed in section 2.2.3.
The error rm: cannot remove 'proyek': Is a directory appears when attempting to delete a directory without the -r flag. A similar error message in cp reads cp: -r not specified; omitting directory 'proyek', indicating that the target is a directory rather than a standard file.
rm -r proyek
cp -r proyek proyek-backupThe error message bash: cd: proyek: No such file or directory typically indicates a mistyped directory name or that the current working directory is not where you expect it to be. Run pwd and ls first to verify your current location and directory contents before changing directories.
pwd
lsIf rm, cp, or mv returns Permission denied, the target file is likely owned by another user or requires root privileges. File permissions and ownership management (chmod, chown) are covered in detail in Chapter 17.
2.3 Viewing and Reading Files: cat, less, head, tail
After navigating and managing files, the next step is reading file content directly from the terminal without opening a text editor. These four commands serve distinct purposes depending on file size and requirements.
2.3.1 cat: Displaying Short File Contents
cat (concatenate) prints the entire contents of a file to the screen at once. It is suitable for short files such as small configuration files.
cat /etc/hostnameThe name cat derives from its original function: concatenating multiple files together into a single output, rather than just displaying a single file.
cat file1.txt file2.txtFor large files, running cat will flood the screen with text without pausing, making it impractical to read. Cases like this are better handled using less.
2.3.2 less: Interactively Reading Long Files
less opens a file in a pager mode, allowing interactive scrolling through content without loading the entire file into screen memory at once.
less /var/log/syslogEssential navigation shortcuts inside less include: the spacebar to move forward one page, the letter b to move back one page, a forward slash (/) followed by a keyword to search for specific text, n to jump to the next search match, and q to exit. Sysadmins regularly use less to read log files hundreds of megabytes in size without exhausting system memory, because less loads only the currently viewed portion of the file rather than the entire file at once like cat.
2.3.3 head and tail: Viewing the Beginning or End of a File
head outputs the first few lines of a file, defaulting to 10 lines.
head /var/log/syslogtail displays the last few lines, also defaulting to 10 lines. Both commands accept the -n flag followed by a number to specify the desired line count.
tail -n 20 /var/log/syslog2.3.4 Case Study: Monitoring Real-Time Logs with tail -f
One of the most practical applications of tail in system administration is the -f (follow) flag. This keeps tail running after displaying the final lines, printing new lines in real-time as they are appended to the file.
tail -f /var/log/syslogThis command is essential when debugging live issues (for example, monitoring web server logs while hitting an error page) because you do not need to reopen the file repeatedly to view new output. Press Ctrl+C to stop following the file. Deep-dive log monitoring topics, including journalctl, will be covered in Chapter 19.
2.3.5 Troubleshooting: cat on Binary Files
Running cat on binary files, such as image files or compiled executables, can corrupt terminal display formatting. Because cat dumps raw bytes straight to the screen, control characters can alter terminal color and rendering settings, leaving the terminal unresponsive or displaying distorted characters afterward.
If this occurs, execute the reset command to restore your terminal to its normal state.
resetTo avoid this issue altogether, form the habit of checking a file's format using the file command before attempting to open it with cat, especially when handling unfamiliar files.
file dokumen-tidak-dikenalIf the result indicates a binary file, use tools like xxd or hexdump -C to safely inspect its contents in hexadecimal format instead of using cat.
hexdump -C dokumen-tidak-dikenal | head2.4 Getting Help: man, --help, tldr
No one can memorize every single flag across hundreds of Linux commands. Learning to seek help directly from the terminal, rather than switching to a browser every time syntax is forgotten, is a skill that saves considerable time long-term.
2.4.1 man: Official Command Manuals
man (manual) opens the official manual page for a command, complete with description, flag lists, and usage examples.
man lsNavigation within man uses the same pager mechanics as less (since man invokes less internally), so spacebar navigation, / searching, and q exiting work identically. Manual pages are organized into sections; for instance, section 1 covers general user commands and section 8 covers system administration commands. Occasionally, a single command name exists across multiple sections; you can target a specific section by placing its number before the command name.
man 8 useraddIf you forget a command's exact name but remember its general purpose, man -k (equivalent to the apropos command) searches the entire manual page database by keyword.
man -k "list directory"2.4.2 --help: Quick On-screen Utility Help
Most GNU coreutils commands support the --help flag, printing a concise summary of available options directly to stdout without entering a pager view like man.
ls --helpThis method is faster than opening a manual page when you simply need a quick flag reference, though descriptions are noticeably brief. For Bash builtins like cd that are built into the shell itself rather than standalone binaries, use help instead of --help, as help is a Bash builtin designed specifically to print documentation for shell builtins.
help cd2.4.3 tldr: Concise Practical Examples
tldr is a community-driven tool providing simplified, example-based command guides. It cuts through formal documentation to show real-world usage instantly. Unlike man or help, tldr is not preinstalled and must be added separately, such as via the Node.js package manager.
npm install -g tldrSeveral Linux distributions also offer tldr via native package managers, such as sudo apt install tldr on Ubuntu/Debian systems. Once installed, simply pass the command name to tldr.
tldr tarThe resulting output lists common operational use cases along with short descriptions for each flag, allowing much faster comprehension than scanning through an extensive manual page.
2.4.4 Troubleshooting: man or tldr Command Not Found
In minimal environments, such as Docker containers or minimal server builds, the man utility is often omitted by default. Attempting to run it may produce bash: man: command not found, or if the binary exists without installed manual pages, No manual entry for ls. Resolution steps depend on your active distribution package manager.
# Ubuntu/Debian
sudo apt install man-db
# Fedora
sudo dnf install man-db
# Arch Linux
sudo pacman -S man-db man-pagesFor tldr, the error bash: tldr: command not found indicates that the utility is not present on the system and must be manually installed using npm or system package repositories, as discussed in section 2.4.3.
2.4.5 Selecting the Right Tool
These help sources complement one another rather than replace each other. Use --help when you need a quick reminder of flag syntax. Use tldr when encountering a command for the first time and needing concrete examples without reading lengthy documentation. Use man when you require complete, accurate specifications (such as writing scripts that must adhere strictly to standard behaviors) or when tldr and --help lack detailed answers. Forming a habit of checking official documentation via man ensures high script accuracy throughout this series.

