Server Command Line Interface: Mastering the Terminal
This chapter covers the most important aspect of Linux: the terminal. The terminal is the beating heart of Linux, enabling you to accomplish virtually anything. For the first eight years I used Linux, I relied exclusively on the terminal, never even considering a graphical interface. The terminal is reliable, quick, and efficient, making it an indispensable tool for Linux users.
Pro Tip: The terminal is always accessible. If you're stuck on a black screen or graphical interface, press Ctrl + Alt + F2 or Ctrl + Alt + F3 to switch to a terminal. To return to the graphical interface, press Ctrl + Alt + F7.
Navigating the Command Line Interface
Videos: How to Make Bash Terminal look and feel GREAT! https://links.thelinuxbook.com/prettybash
Types of Terminals
- TTY: A terminal connected to a physical device, such as a keyboard and monitor
- GUI Terminal: A terminal emulator that provides a graphical interface for command-line operations, such as GNOME Terminal, Konsole, or xterm
TTY is used for direct interaction with the system, while GUI terminals provide a more user-friendly experience with features like tabs, copy-paste, and customizable appearance. TTY is often used for system recovery or when the graphical interface is unavailable or malfunctioning.
To access a TTY terminal, you can use the following key combinations:
Ctrl + Alt + F1toCtrl + Alt + F6: Switch to different TTY terminals (TTY1 to TTY6)Ctrl + Alt + F7: Return to the graphical interface (usually TTY7)
GUI Terminals are typically launched from the applications menu or by pressing Ctrl + Alt + T in most desktop environments or hotkey like Super + Enter in many Tiled Window Managers (TWM).
Recommended GUI Terminals:
- GNOME Terminal: Default terminal for GNOME desktop environments, supports tabs and profiles
- Konsole: Default terminal for KDE Plasma, highly customizable with split views and profiles
- Alacritty: A fast, GPU-accelerated terminal emulator with a focus on performance and simplicity
- Kitty: A feature-rich terminal emulator with support for graphics
- Terminator: Allows multiple terminal windows in a single window, with customizable layouts
Directory Movement Commands and Examples:
cd(change directory):cd ..(move up a directory)cd name(move into the specified directory)cd /(move to the root directory)cd ~(move to the home directory, e.g.,/home/titus)zoxide- A better package for navigating the file system, using fuzzy finding and remembering past directories. _Note:zoxideis a third-party tool that can be installed via your package manager. Watch the video I did on Zoxide: https://links.thelinuxbook.com/zoxide
ls(list files):ls -a(show all files, including hidden ones)ls -l(long listing format showing permissions)
rm(remove files):rm -rf(remove files/directories recursively and forcefully)rmdir(remove empty directories)trash-cli- An alternative tormthat moves files to the trash instead of permanently deleting them.
cp(copy files):cp -r(copy files and subdirectories recursively)rsync- A more efficient tool for copying or syncing large amounts of files.
mv(move files):mv file1.txt file2.txt(rename or move files)mv /path/to/file /new/path/(move a file to a new location)
pwd(print working directory):- Displays the current directory path.
find(search for files):find /path/to/search -name "filename"(search for a file by name)find /path/to/search -type d -name "dirname"(search for a directory by name)fd- A faster alternative tofindwith a simpler syntax.
locate(find files by name):locate filename(find files by name using a pre-built index)updatedb(update the locate database, usually run automatically by the system)
tree(display directory structure):tree(display the directory structure in a tree-like format)tree -L 2(limit the depth of the tree display to 2 levels)
ln(create links):ln -s /path/to/original /path/to/link(create a symbolic link)ln /path/to/original /path/to/link(create a hard link)
chmod(change file permissions):chmod 755 file(set permissions to read, write, and execute for owner, and read and execute for group and others)chmod +x script.sh(make a script executable)
chown(change file ownership):chown user:group file(change ownership of a file to a specific user and group)chown -R user:group /path/to/directory(change ownership recursively for a directory)
stat(display file or filesystem status):stat file(show detailed information about a file, including size, permissions, and timestamps)stat /path/to/directory(show information about a directory)
du(disk usage):du -h(display disk usage in human-readable format)du -sh /path/to/directory(show total size of a directory)
df(disk space usage):df -h(display disk space usage in human-readable format)df -i(show inode usage instead of disk space)
Directory Layout and File System Overview
/- Root directory/bin- System executable files/boot- Bootloader files/dev- Device files (requires mounting to browse)/etc- System configuration files/home- User files and configuration files/media- Mounted external media/mnt- Temporary mount points/opt- Optional files/root- Root user files and configurations/tmp- Temporary files/usr- System resources/var- Logs, databases, and other variable data
Most interactions with the file system occur within the /home/user directory. This includes configuration files and executable programs. Direct editing of system-wide configuration files in /etc or adding programs to /bin should be done sparingly.