AB
Master Linux fundamentals with this comprehensive guide covering shell basics, essential commands, filesystem hierarchy, and core Linux concepts.
What is a shell?
A shell in Linux is a command-line interpreter that provides a user interface for the Linux operating system. It acts as an intermediary between you and the system kernel, allowing you to execute commands, run programs, and manage system resources. Common shells include Bash (Bourne Again SHell), Zsh, and Fish.
The home directory in Linux serves as your personal workspace. It contains your personal files, configuration settings, and directories. Each user has their own home directory, typically located at /home/username
, where username
is your login name.
The home directory is usually represented by the tilde symbol (~
). When you see ~
in a path, it refers to your home directory.
In Linux, interactions with the shell involve using commands and arguments:
For example, in the command ls -l /home/username
:
ls
is the command (list directory contents)-l
is an option argument (use long listing format)/home/username
is an argument specifying which directory to listLinux commands fall into several categories:
Built-in Commands: These are part of the shell itself and don’t exist as separate files. Examples include cd
, echo
, and exit
.
External Commands: These are executable files stored in the filesystem. Examples include ls
, grep
, and find
.
Shell Scripts: These are files containing a series of commands that the shell can execute, used to automate tasks.
Alias Commands: Custom shortcuts created by users to represent longer commands or command sequences.
Functions: Similar to shell scripts but defined within the shell session for reuse.
To check whether a command is built-in or external, use the type
command:
$ type cd
cd is a shell builtin
$ type uptime
uptime is /usr/bin/uptime
Here are some fundamental commands you’ll use regularly in Linux:
pwd
- Print Working Directory
Shows the current directory path.
ls
- List Storage
Lists files and directories.
ls -l
: Long listing format with detailsls -a
: Show all files (including hidden)ls -lt
: Sort by modification timels -ltr
: Sort by modification time in reverse ordermkdir
- Make Directory
Creates new directories.
mkdir -p dir1/dir2/dir3
: Create nested directoriescd
- Change Directory
Changes your current location.
cd ~
: Go to home directorycd ..
: Go to parent directorycd -
: Go to previous directorymv
- Move
Moves or renames files and directories.
mv source destination
cp
- Copy
Copies files or directories.
cp source destination
cp -r source destination
: Copy directories recursivelyrm
- Remove
Deletes files or directories.
rm filename
: Remove a filerm -r directory
: Remove a directory and its contentsrm -f filename
: Force removal without confirmationrmdir
- Remove Directory
Removes empty directories.
cat
- Concatenate
Displays file contents.
cat filename
cat > filename
: Create a new file and add contenttouch
Creates empty files or updates timestamps.
touch filename
more
/ less
Display file contents one screen at a time.
more filename
: Basic pagerless filename
: Advanced pager with backward navigationhead
/ tail
Show beginning or end of files.
head -n 5 filename
: First 5 linestail -n 5 filename
: Last 5 linestail -f logfile
: Follow log updates in real-timegrep
- Global Regular Expression Print
Searches for patterns in files.
grep "pattern" filename
grep -i "pattern" filename
: Case-insensitive searchgrep -r "pattern" directory
: Recursive searchgrep -v "pattern" filename
: Show non-matching linesgrep -n "pattern" filename
: Show line numberschmod
- Change Mode
Changes file permissions.
chmod 755 filename
: Set permissions using octal notationchmod +x filename
: Make file executablechmod -w filename
: Remove write permissionchown
- Change Owner
Changes file owner.
chown username:groupname filename
chgrp
- Change Group
Changes file group.
chgrp groupname filename
When you need assistance with commands, these tools are invaluable:
man
- Manual
Displays the manual page for a command.
man ls
--help
option
Most commands provide basic help with this flag.
ls --help
whatis
Shows a one-line description of a command.
whatis grep
apropos
Searches for commands related to a keyword.
apropos file
There are two ways to specify file and directory locations in Linux:
An absolute path specifies the complete location from the root directory (/
).
/
/home/username/documents/file.txt
A relative path specifies a location relative to your current directory.
/
/home/username
, the relative path to file.txt
would be documents/file.txt
For more efficient navigation, you can use:
pushd
and popd
: Manage a directory stackpushd /var/log
: Save current directory and move to /var/log
popd
: Return to the previously saved directoryEnvironment variables store information about your system environment. Here are some important ones:
$HOME
: Your home directory$PATH
: List of directories for executable programs$PWD
: Current working directory$SHELL
: Your shell type$USER
: Your username$HOSTNAME
: Your computer’s name$OSTYPE
: Your operating system typeView all environment variables:
printenv
View a specific variable:
echo $HOME
Set a temporary environment variable:
export VARIABLE_NAME=value
Make environment changes permanent by adding them to configuration files:
echo "export VARIABLE_NAME=value" >> ~/.bashrc
source ~/.bashrc
The PATH variable is particularly important as it tells the shell where to look for executable files when you type a command. Directories in the PATH are separated by colons (:
).
View your PATH:
echo $PATH
Add a new directory to your PATH:
export PATH=$PATH:/new/directory/path
To find the location of a command’s executable, use:
which command_name
The shell prompt is the text displayed before you type commands. You can customize it by modifying the PS1
environment variable:
export PS1="\u@\h:\w\$ "
Common prompt elements:
\u
: Username\h
: Hostname\w
: Current working directory\d
: Date\t
: Time\$
: Shows #
for root, $
for regular usersMake prompt changes permanent:
echo 'PS1="\u@\h:\w\$ "' >> ~/.bashrc
The Linux kernel is the core component of the Linux operating system. It acts as an intermediary between hardware and software, managing:
The Linux kernel is both monolithic and modular:
Check your kernel version:
uname -r
uname -a
Linux memory is divided into two distinct areas:
User Space:
Kernel Space:
When a user application needs a system resource, it makes a system call that transitions from user space to kernel space, performs the operation, and returns to user space.
Linux provides several commands for hardware discovery and management:
dmesg
: Displays kernel ring buffer messages, useful for hardware details and debugginglspci
: Lists all PCI devices (network cards, graphics, etc.)lsblk
: Lists block devices (hard drives, SSDs, etc.)lscpu
: Displays CPU architecture informationlsmem
: Lists memory blocks and informationfree
: Shows memory usage statisticslshw
: Provides comprehensive hardware configuration detailsThe Linux boot process follows these steps:
BIOS/UEFI Initialization:
Bootloader (GRUB):
Kernel Initialization:
init
process or systemd
Init/Systemd:
Runlevel/Target Initialization:
Login Prompt:
The init
process is the first process started during boot (PID 1). Modern Linux distributions often use systemd
as the init system.
Runlevels define the state of the system:
In systemd
systems, runlevels are replaced by targets:
poweroff.target
(runlevel 0)rescue.target
(runlevel 1)multi-user.target
(runlevel 3)graphical.target
(runlevel 5)reboot.target
(runlevel 6)Check your current target:
systemctl get-default
Linux supports several types of files, each serving a specific purpose:
Regular Files (-
):
Directories (d
):
Symbolic Links (l
):
Special Files:
c
): Handle data as character streamsb
): Handle data in fixed-size blocksSockets (s
):
Named Pipes/FIFOs (p
):
Use the file
command to identify file types:
file filename
To see file types in a directory listing, use:
ls -l
The Linux filesystem is organized according to the Filesystem Hierarchy Standard (FHS), which defines the purpose and contents of each directory:
/
(Root): The top-level directory/bin
: Essential user command binaries/boot
: Boot loader files and kernel/dev
: Device files/etc
: System configuration files/home
: User home directories/lib
, /lib64
: Essential shared libraries/media
: Mount points for removable media/mnt
: Temporary mount points/opt
: Optional application software/proc
: Virtual filesystem for process information/root
: Home directory for the root user/run
: Runtime variable data/sbin
: Essential system binaries/srv
: Data for services provided by the system/sys
: Virtual filesystem for system information/tmp
: Temporary files/usr
: Secondary hierarchy for user data/var
: Variable data (logs, databases, etc.)This organization ensures consistency across Linux distributions and helps users and administrators locate specific files and directories.