Welcome to Part 5 of the Linux Bash course. In this final part, we complete our process control curriculum by sending system signals, master the versatile find search engine, and configure persistent environment variables, aliases, and prompt themes within startup profile scripts.
If you are setting up custom environment variables inside containers, you will find our guide on Installing Docker on Ubuntu highly relevant. For full details on shell behaviors, consult the official GNU Bash Manual.
Chapter 13: Killing and Controlling Processes
13.1 Understanding Linux Signals
Linux manages processes by sending them asynchronous system signals. Signals trigger predefined behaviors or force exits. The most common signals are:
SIGINT(Signal 2): Sent by pressingCtrl+C. Gently requests a process to abort, allowing it to clean up temp files.SIGHUP(Signal 1): Sent when the terminal owning the process closes. Often used to tell system services to reload config files without restarting.SIGTERM(Signal 15): The default termination signal. Politely requests termination. Processes can catch and intercept this.SIGKILL(Signal 9): Forceful termination. Handled directly by the kernel rather than the target process. It cannot be caught, blocked, or ignored.
13.2 Terminating Processes by PID: The kill Command
The kill command sends a signal to a process identified by its Process ID (PID).
Syntax:
kill [options] PID...
Example Command:
kill -9 1482
Expected Output:
[1]+ Killed sleep 100 &
Flag & Command Breakdown:
kill: The signal sending utility.-9: Sends the absolute force kill signal (SIGKILL). If omitted,killdefaults to-15(SIGTERM).1482: The target process ID.
13.3 Terminating Processes by Name: The killall Command
killall terminates all processes matching the specified name.
Syntax:
killall [options] process_name...
Example Command:
killall -v sleep
Expected Output:
Killed sleep(1512) with signal 15
Killed sleep(1513) with signal 15
Flag & Command Breakdown:
killall: Kills by process name.-v: Verbose output showing PIDs targeted.
13.4 Interactive Terminating: The pkill and pgrep Commands
- pgrep: Searches active processes and lists PIDs matching criteria (like username or partial name).
- pkill: Sends signals to processes matching criteria.
Syntax:
pgrep [options] pattern
pkill [options] pattern
Example Command:
pkill -u suresh -f "sleep"
Expected Output: (Kills all processes named “sleep” running under user context suresh)
Flag & Command Breakdown:
pkill: Sends termination signals based on search matches.-u suresh: Matches processes owned by usersuresh.-f: Matches the pattern against the full command line arguments, not just the process name.
13.5 Disowning Jobs from the Shell: The disown Command
When you close a terminal shell, all active background jobs receive a SIGHUP signal and exit. Use the disown command to remove a job from the shell’s active job table, preventing SIGHUP from reaching it.
Syntax:
disown [options] [%job_number...]
Example Command:
sleep 300 &
disown %1
(The sleep job continues running in the background even if you exit the shell)
13.6 Running Commands Immune to Hangups: The nohup Command
The nohup (No Hang Up) command executes another command with hangups ignored, and redirects standard output to a file named nohup.out.
Syntax:
nohup command [arguments] &
Example Command:
nohup backup_script.sh &
Expected Output:
nohup: ignoring input and appending output to 'nohup.out'
[1] 1540
13.7 Checking System Uptime and Load: The uptime Command
The uptime command reports how long the system has been running, user counts, and system load averages for the past 1, 5, and 15 minutes.
Syntax:
uptime
Example Command:
uptime
Expected Output:
13:30:22 up 5:12, 2 users, load average: 0.15, 0.08, 0.02
13.8 Adjusting Process Priority: The nice Command
Process priority in Linux is regulated by “niceness” values ranging from -20 (highest priority, least nice to other processes) to 19 (lowest priority, nicest to other processes). nice starts a process with a specific niceness.
Syntax:
nice -n [niceness_value] command [arguments]
Example Command:
nice -n 10 tar -czf backup.tar.gz documents/
(Starts compression tool with niceness 10, keeping it from hogging CPU resources)
13.9 Changing Priority of Running Processes: The renice Command
The renice command alters the scheduling priority of active processes.
Syntax:
renice [-n] priority -p PID
Example Command:
sudo renice -n -5 -p 1024
Expected Output:
1024 (process ID) old priority 0, new priority -5
13.10 Running Commands at a Scheduled Time: The at Command
The at command schedules tasks to run once at a specific future time.
Syntax:
at time
Example Command:
echo "tar -cf /tmp/backup.tar /home" | at 14:00
Expected Output:
job 3 at Fri Jun 12 14:00:00 2026
Chapter 14: Finding Files & Directories
14.1 Introduction to the find Command
The find command searches for files in a directory hierarchy in real-time. It evaluates criteria (name, size, type, modified dates) starting at a search path and walking down subdirectories.
14.2 Finding Files by Name
Find matching files using case-sensitive (-name) or case-insensitive (-iname) searches.
Syntax:
find search_path -name "pattern"
Example Command:
find /home/suresh -iname "*.md"
Expected Output:
/home/suresh/Documents/TechBlog/src/content/courses/linux-bash/part-1-foundations.md
/home/suresh/Documents/TechBlog/src/content/courses/linux-bash/part-2-core-utilities.md
14.3 Finding Files by Type: find -type
Limit search results to directories (d), regular files (f), or symbolic links (l).
Syntax:
find search_path -type file_type
Example Command:
find /home/suresh/Documents/TechBlog/src/content/courses -type d
Expected Output:
/home/suresh/Documents/TechBlog/src/content/courses
/home/suresh/Documents/TechBlog/src/content/courses/linux-bash
14.4 Finding Files by Size: find -size
Search files based on file sizes using suffixes like k (Kilobytes), M (Megabytes), G (Gigabytes). Prefix values with + for larger or - for smaller.
Syntax:
find search_path -size [+/-]value
Example Command:
find /var/log -size +5M
Expected Output:
/var/log/journal/system.journal
14.5 Finding Files by Modification Time
-mtime: Modified days ago.-mmin: Modified minutes ago.
Syntax:
find search_path -mtime [+/-]days
find search_path -mmin [+/-]minutes
Example Command:
find /home/suresh -mmin -60
(Locates files created or modified within the last hour)
14.6 Finding Files by Permissions and Owner
-user: Matches files owned by user.-perm: Matches files with specific permissions.
Syntax:
find search_path -user username
find search_path -perm octal_mode
Example Command:
find . -type f -perm 755
14.7 Combining Conditions: Logical Operators
You can combine search rules using:
-and/-a: True only if both conditions are met (default).-or/-o: True if either condition is met.-not/!: Negates a condition.
Syntax:
find search_path condition1 -and condition2
Example Command:
find . -type f -name "*.sh" -and -not -perm 755
14.8 Executing Commands on Found Files: find -exec
The -exec option runs a command on every file that matches search filters. The syntax requires:
{}: Substituted by the matching file path.\;or+: Terminates the command. Using\;runs the command once per file, while+bundles matching files into a single call.
Syntax:
find search_path filters -exec command {} \;
Example Command:
find . -name "*.txt" -exec chmod 644 {} +
14.9 Fast File Searching: The locate Command
find searches directories physically in real-time, which can be slow on massive filesystems. The locate command searches a prebuilt database index of files (/var/lib/mlocate/mlocate.db), completing searches almost instantly.
Syntax:
locate [options] pattern
Example Command:
locate "part-1-foundations.md"
Expected Output:
/home/suresh/Documents/TechBlog/src/content/courses/linux-bash/part-1-foundations.md
14.10 Real-time Search vs. Static DB
- locate: Instantaneous, but does not find recently created files until the database is updated.
- updatedb: A root system command that updates the
locateindex database. Typically run automatically by a daily cron job.
Syntax:
sudo updatedb
Chapter 15: Environment & Aliases
15.1 Shell Variables vs. Environment Variables
- Shell Variables: Local variables available only in the current shell session. Sub-shells or child processes spawned from this shell cannot access them.
- Environment Variables: Global variables available to the shell and all child processes it spawns.
15.2 Setting and Exporting Environment Variables
To promote a shell variable to an environment variable, use the export command.
Syntax:
export VARIABLE_NAME="value"
Example Command:
export EDITOR="nano"
15.3 Viewing Variables: env, printenv, and set
env&printenv: Display active environment variables.set: Displays all shell variables, environment variables, and shell functions.
Syntax:
printenv [variable_name]
Example Command:
printenv USER
Expected Output:
suresh
15.4 The Crucial $PATH Variable
The $PATH environment variable is a colon-separated list of directories. When you type a command, the shell searches these directories in order for an executable file matching the command name.
Syntax:
export PATH="/new/bin/path:$PATH"
Example Command:
echo $PATH
Expected Output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
15.5 Defining Custom Prompts via $PS1
The $PS1 variable controls the look of your primary shell prompt. You can customize colors, directories, and hostname configurations using backslash-escaped special characters (e.g. \u for username, \h for hostname, \w for directory path).
Syntax:
export PS1="[\u@\h \W]\$ "
15.6 Creating Command Shortcuts: The alias Command
The alias command creates shortcuts for long commands.
Syntax:
alias name='command'
Example Command:
alias ll='ls -la'
15.7 Removing Shortcuts: The unalias Command
Remove shortcuts using unalias. Pass -a to remove all aliases in the current session.
Syntax:
unalias name
15.8 Making Aliases and Variables Persistent
Aliases and variables set in the terminal disappear when you close the session. To make them permanent, append them to the user’s ~/.bashrc file.
Example:
echo "alias gs='git status'" >> ~/.bashrc
source ~/.bashrc
15.9 Script Execution vs. Sourcing
bash script.sh: Executes the script inside a new sub-shell. Environmental modifications vanish when the script exits.source script.sh(or. script.sh): Executes the script directly inside the active shell. Use this when a script modifies shell settings or environment variables you want to retain.
15.10 Customizing Shell Startup Behavior
When Bash starts, it checks if it is a Login or Non-Login shell, loading configurations accordingly:
- Login Shell: Reads
/etc/profilefirst, then searches for the first user file in order:~/.bash_profile,~/.bash_login, and~/.profile. - Interactive Non-Login Shell: Reads
~/.bashrcdirectly. - Non-Interactive Shell: Reads the file path defined in the environment variable
$BASH_ENV.