Table of Contents
Getting the hang of the Linux filesystem can be a bit tricky at first, but it’s a super handy skill to have in your toolkit. I put together this guide mainly for myself—something I can refer back to whenever I need a refresher. But if it helps someone else along the way, that’s a bonus!
Linux Command Line
Navigating the Linux filesystem is a core skill for anyone using a Linux system. From the root directory (represented by /
), the Linux filesystem is organized in a way that resembles a tree structure, starting from the root and branching out into various files and directories. To effectively navigate this filesystem, let’s dive into some essential commands and tips that will help you master Linux navigation.
Understanding the Linux Filesystem
At the top, you’ve got the root directory (/). It’s home to several important subdirectories:
/
This is where it all starts, the root directory
/bin
Essential commands and binaries live here.
/etc
All your system configuration files are stored here.
/home
Personal directories for each user.
/var
Variable data like logs and spool files.
/usr
User-installed software and libraries.
/tmp
Temporary files created by the system and users.
Knowing these directories helps you find files and perform administrative tasks more efficiently.
Basic Navigation Commands
pwd: Print Working Directory
When moving around the filesystem, some special characters come in handy. The dot (.) represents the current directory, and the double dot (..) takes you to the parent directory. For example, if you’re in /home/user/documents
and you type cd ..
, you’ll move up to /home/user
.
To see where you currently are in the filesystem, use the pwd
(print working directory) command. It shows your current location. For instance:
pwd
/home/user/documents
cd: Change Directory
Moving around the filesystem is done with cd
. If you want to navigate to the /etc
directory, type:
cd /etc
To return to your home directory from anywhere in the filesystem, use:
cd
Or the shorthand version, which uses the tilde (~
) to represent your home directory:
cd ~
ls: List Files
To see what’s inside a directory, use ls
. This command lists files and subdirectories. For a more detailed view that includes file permissions, owner, size, and modification date, use:
ls -l
total 12
drwxr-xr-x 2 user user 4096 Jan 1 10:00 documents
-rw-r--r-- 1 user user 88 Jan 1 10:00 file.txt
To view hidden files (those starting with a dot), use the -a
option:
ls -a
. .. .bashrc documents file.txt
The .
and ..
entries represent the current and parent directories, respectively.
Managing Directories
Creating and managing directories is straightforward with commands like mkdir
(make directory) and rmdir
(remove directory). To create a new directory:
mkdir new_directory
To remove an empty directory:
rmdir new_directory
If the directory is not empty, use rm
with the -r
(recursive) option to remove it and all its contents:
rm -r new_directory
Path Navigation
When navigating between directories, you can use relative and absolute paths. A relative path is specified in relation to the current directory, while an absolute path starts from the root directory. For example, if you’re in /home/user
and want to go to documents
, you can use either:
Relative path:
cd documents
Absolute path:
cd /home/user/documents
Understanding these path concepts is crucial for efficient navigation and file management.
Suppose you are in your home directory and want to list the contents of the /etc
directory, move to /var/log
, and then return to your home directory. Here’s how you’d do it:
ls /etc
cd /var/log
pwd
cd
This sequence lists the contents of /etc
, changes the directory to /var/log
, prints the current directory to confirm your location, and then returns you to your home directory.
Special Features
Tab Completion
Tab completion is incredibly useful for speeding up command entry. Start typing a command or path, and pressing the Tab key will auto-complete it if there’s a single match, or show possible completions if there are multiple matches:
cd /etc/sys
Pressing Tab after “sys” will auto-complete to /etc/systemd/
if it is the only match or show all matches if there are multiple directories starting with “sys.”
tree Command
For a visual representation of the directory structure, the tree
command provides a hierarchical view. If tree
is not installed, you can install it using:
sudo apt install tree
Then, to view the directory structure from the current directory:
tree
This command provides a hierarchical view of directories and subdirectories, making it easier to understand the filesystem layout.
Searching The Filesystem
Finding files and directories is easier with commands like find
and locate
. The find
command is powerful for searching based on various criteria, such as name, size, or modification date:
sudo find / -name file.txt
The locate
command is faster because it uses a database of indexed files. Make sure to update the database periodically with updatedb
:
locate file.txt
Symbolic Links
Understanding symbolic links (symlinks) is also important. A symlink is a file that points to another file or directory. Creating a symlink uses the ln -s
command. For example, to create a symlink to /etc/passwd
in your home directory:
ln -s /etc/passwd mypasswd
Here, mypasswd
in your home directory points to /etc/passwd
. You can use ls -l
to verify:
ls -l mypasswd
lrwxrwxrwx 1 user user 12 Jan 1 12:00 mypasswd -> /etc/passwd
Network Config and Troubleshooting
ip and ifconfig
The ip
command is a powerful tool for network configuration. It can replace the older ifconfig
command, offering more features and flexibility. To view network interfaces and their details, use:
ip addr
To bring an interface up or down, use:
sudo ip link set eth0 up
sudo ip link set eth0 down
The older ifconfig
command is still widely used for simple tasks. To display network interfaces, you can use:
ifconfig
To configure an IP address on an interface:
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
mtr
The mtr
(My Traceroute) command combines the functionality of ping
and traceroute
, providing real-time network diagnostics. It helps identify where packet loss and latency occur. To use mtr
, simply enter:
mtr gitforgits.com
File and Text Processing
find
The find
command searches for files and directories based on various criteria. To search for a file named file.txt
starting from the root directory:
sudo find / -name file.txt
To find files modified in the last 7 days:
find /home/user -mtime -7
awk
The awk
command is a powerful text processing tool, often used for data extraction and reporting. It reads input line by line, splits each line into fields, and processes them based on specified patterns. For example, to print the second field of each line in a file:
awk '{print $2}' file.txt
If you have a CSV file and want to print the first and third columns, you can use:
awk -F, '{print $1, $3}' file.csv
This command uses a comma as the field separator and prints the desired columns.
Networking Monitoring and Diag
tcpdump
The tcpdump
command captures network packets and displays them in real-time, useful for network troubleshooting and security analysis. To capture packets on the eth0
interface, use:
sudo tcpdump -i eth0
To capture and save packets to a file for later analysis:
sudo tcpdump -i eth0 -w capture.pcap
To read the saved capture file, use:
sudo tcpdump -r capture.pcap
netstat
The netstat
command displays network connections, routing tables, interface statistics, and more. To list all active connections and listening ports, use:
netstat -tuln
To display network statistics, such as packets transmitted and received:
netstat -s
nslookup
The nslookup
command queries DNS to obtain domain name or IP address mapping. It’s useful for troubleshooting DNS issues. To find the IP address of a domain:
nslookup gitforgits.com
To perform a reverse lookup, converting an IP address to a domain name:
nslookup 192.168.1.1
System and Process Management
sudo
The sudo
command allows users to execute commands with superuser privileges, necessary for performing administrative tasks. To edit a system file with superuser permissions:
sudo nano /etc/hosts
To execute a command as another user, specify the -u
option:
sudo -u username command
tmux
The tmux
command is a terminal multiplexer that enables you to run multiple terminal sessions within a single window. It’s useful for managing multiple tasks without opening several terminal windows. To start a new session:
tmux
To detach from the session while keeping it running, use Ctrl+b
followed by d
. To reattach to the session:
tmux attach
lsns
The lsns
command lists information about system namespaces, which are used for isolation in Linux containers and virtual environments. To display all namespaces:
lsns
This command shows the PID, type, and other details of each namespace, useful for managing and troubleshooting containerized applications.
pkill
The pkill
command sends signals to processes based on their name or other attributes. It’s commonly used to terminate processes. To kill all processes named example
:
pkill example
To send a specific signal, such as SIGKILL (kill signal):
pkill -9 example
Email and Web Requests
The mail
command sends and receives emails from the command line, useful for scripting and automated notifications. To send an email:
echo "This is the body" | mail -s "Subject" user@gitforgits.com
To read received emails:
mail
This command lists the inbox messages, which you can navigate using commands within the mail interface.
curl
The curl
command transfers data to or from a server, supporting various protocols like HTTP, FTP, and more. To download a file from a URL:
curl -O http://gitforgits.com/file.txt
To send a GET request and display the response:
curl http://gitforgits.com
For a POST request with data:
curl -d "param1=value1¶m2=value2" -X POST http://gitforgits.com
watch
The watch
command runs a command at regular intervals, displaying the output and highlighting changes. It’s useful for monitoring system status or command output over time. To repeatedly execute df -h
and show disk usage:
watch df -h
To run a custom script every two seconds:
watch -n 2 ./myscript.sh
These commands form the backbone of Linux system management. By using them regularly, you’ll gain confidence and efficiency in handling Linux environments. As you incorporate them into your routine tasks, your command-line skills will greatly improve, enabling you to operate systems efficiently and effectively.