0% found this document useful (0 votes)
17 views

history_job control_alias

Uploaded by

singhm.techvala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

history_job control_alias

Uploaded by

singhm.techvala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Jobs and Job Control in Linux

Introduction
In the Linux operating system, jobs refer to processes that are running in the background or
foreground. Job control refers to the ability to manipulate these processes, including suspending,
resuming, and terminating them. This can be useful for managing multiple tasks or for debugging
problems with a process.

Job control is made possible by the shell, which is a command-line interface that allows users to
interact with the operating system. The most common shell in Linux is the Bourne Again
Shell (BASH), but other shells such as the Z Shell (ZSH) and the Korn Shell (KSH) are also
available.

In this article, we will explore the basics of job control in Linux and how to use it to manage
processes.

Understanding Processes and Jobs in Linux


In Linux, every program that is running is considered a process. A process can be a standalone
program or a part of a larger program.

Each process is assigned a unique identifier called a process ID (PID). The PID can be used to
refer to the process and perform actions on it, such as suspending or terminating it.

A job is a process that is either running in the foreground or the background. The foreground is
the active window in the terminal, and the background is any process that is running but not
actively being used in the terminal.

By default, when you run a command in the terminal, it runs in the foreground. You can tell that a
process is running in the foreground because it displays output and you cannot enter any more
commands until it finishes.

To run a process in the background, you can use the & symbol at the end of the command.

Example

$ sleep 30 &
[1] 12345

In this example, the sleep command causes the process to sleep for 30 seconds. The & symbol
causes the process to run in the background, and the output [1] 12345 indicates that it is job
number 1 with a PID of 12345.

Managing Jobs with the fg and bg Commands


The fg (foreground) and bg (background) commands allow you to move jobs between the
foreground and the background.

To bring a background job to the foreground, you can use the fg command followed by the job
number or the PID.

Example

$ fg %1
sleep 30

This brings the sleep command, which is job number 1, to the foreground and displays its output.

To send a foreground job to the background, you can use the bg command followed by the job
number or the PID.

Example

$ sleep 30
[1] 12345
^Z
[1]+ Stopped sleep 30
$ bg %1
[1]+ sleep 30 &

In this example, the sleep command is run in the foreground and then suspended with the
CTRL+Z keyboard shortcut. The bg command is then used to resume the job in the background.

Suspend or Resume Jobs in Linux


The suspend command allows you to temporarily stop a job, while the kill command allows you
to permanently terminate a job.
To suspend a job, you can use the suspend command followed by the job number or the PID.

Example

$ sleep 30 &
[1] 12345
$ suspend %1
[1]+ Suspended

This suspends the sleep command, which is job number 1. The job can then be resumed with
the fg command or left suspended in the background.

To terminate a job, you can use the kill command followed by the job number or the PID.

Example

$ sleep 30 &
[1] 12345
$ kill %1

This terminates the sleep command, which is job number 1.

View and Manage Jobs with the jobs and ps Commands


The jobs command allows you to view a list of all jobs running in the background or suspended
in the current shell. The ps command allows you to view a list of all processes running on the
system.

Example

$ sleep 30 &
[1] 12345
$ sleep 60 &
[2] 12346
$ jobs
[1] Running sleep 30 &
[2] Running sleep 60 &

You can use the jobs command to view the status of each job and its job number or PID.

The ps command allows you to view a list of all the processes that are currently running on the
system. You can use the -a flag to show all processes and the -x flag to show processes that are
not associated with a terminal.

Example
$ ps -ax
PID TTY STAT TIME COMMAND
1 ? Ss 0:00 /sbin/init
2? S 0:00 [kthreadd]
3? S 0:00 [ksoftirqd/0]
4? S 0:00 [kworker/0:0]
...

The ps command displays the PID, terminal (TTY), status, time, and command for each process.

How to Display Command History in Linux |


history Command
The command-line interface in Linux provides powerful tools for users, and
mastering command history is essential for efficient navigation and retrieval of
previously executed commands. The history command is a valuable utility that
allows users to view and search through their command history. In this
comprehensive guide, we will explore the various features of the history command,
enabling users to streamline their workflow and save time by efficiently accessing
and reusing commands.
Understanding the history Command:
The history command in Linux provides a chronological list of previously executed
commands, along with corresponding command numbers. This feature allows users
to recall, reuse, and modify commands without having to retype them. The
command history is stored in a file, typically ~/.bash_history for the Bash shell.
How to Display Command history in Linux
To view the command history, simply type:
history

Here, the number(termed as event number) preceded before each command


depends on the system. You may get different numbers while executing on your
own system.
Display a Limited Number of Commands History
To show the limited number of commands that executed previously as follows:
history 5

This command displays the last 5 commands from the command history.
Execute a Command by Event Number
Execute a command using its event number with the! Symbol. For instance:
!1997
This will rerun the command with event number 1997.

Print Command before Execution History


To print a command before executing it to avoid errors, use the :p option after the
event number. Example:
!1997 :p

This will display the command associated with event number 1997 without
executing it.

Search Command History with grep


Combine the history command with grep for efficient searching. Example:
Example:

history | grep chpasswd

This command filters and displays only the commands containing the term
“chpasswd.”

View the Most Recent Command History


To quickly view and rerun the most recent command, use:
!!

This will rerun the last executed command.

Execute a Command without Storing History


If a command needs to be executed without being stored in history, unset
the HISTFILE variable. Example:

This ensures that the command won’t be stored in the history file.

Execute a Command using a Part of the Command


History
Execute a command using a part of its string. Example
!command_starting_string

This will execute the latest command starting with “command_starting_string.”

Remove a Specific Command from History


Remove a specific command from history using the history -d option. Example:
history -d 1996

This command removes the command with event number 1996 from history.

Clear Entire Command History


Clear the entire command history using the history -c option. Example
history -c

Be cautious, as this action irreversibly removes all commands from the history
View the Last 10 Commands History
Use history | tail to view the last 10 commands from the history:
history | tail

This command efficiently displays the most recent commands in the terminal.
How to Create and Use Alias Command in
Linux
Last Updated : 03 Jul, 2024



Imagine you’re lost in a maze of complicated Linux commands.


You stumble upon a secret doorway marked “Alias,” and inside
you find shortcuts to all your favorite commands! That’s what
creating aliases is like. You get to make your own mini-commands
for the long ones you use all the time, making things simpler and
faster. This article guides you through building these shortcuts,
turning you into a master of the Linux command jungle! No need
for fancy tech words, we’ll keep it clear and fun, like chatting with
a friend.
What is an alias in Linux?
In Linux, an alias is a user-defined shorthand for a longer
command or sequence of commands. These aliases can be
created and customized according to user preferences, making
the command-line interface more user-friendly. The
alias command instructs the shell to replace one string with
another string while executing the commands.
When we often have to use a single big command multiple times,
in those cases, we create something called an alias for that
command. Alias is like a shortcut command, which will have the
same functionality as if we were writing the whole command.
How does alias Command in Linux Work?
The alias command in Linux allows users to create shortcuts for
long or complex commands, simplifying command-line usage. By
defining an alias, you can substitute a shorter or more intuitive
name for a lengthy command sequence. For example, you can
set alias ll='ls -la' to make ll a shortcut for ls -la, which
lists directory contents in detail. Aliases are typically defined in
shell configuration files like .bashrc or .zshrc, ensuring they persist
across sessions. This command enhances productivity by
reducing the amount of typing and minimizing the chances of
errors in frequently used commands.
Linux alias Syntax
 The Basic syntax of the alias command in Linux is as follows:
alias shortname='longer command'
Here,
 shortname – represents to that we can give any name we
want.
 longer command – represents to where we type our
command.

Options Available for Alias Command


The following are the some of the options available for alias
command:
Option Description

alias Displays all current aliases

alias
name='value' Creates a new alias with the specified name and command

unalias name Removes a specific alias

unalias -a Removes all aliases

alias --help Displays help information about the alias command

alias -p Prints all defined aliases in a reusable format

alias -t Temporarily creates an alias that lasts only for the current shell session
Option Description

(if supported by the shell)

How to Create and Use Alias Command in


Linux?
 To create an alias, open your terminal and use the following
syntax:
alias shortname='longer command'
 The following screenshot illustrates on successfully creation of
alias for shortname variable.

For example
 If we want to create an alias for the ‘cd Desktop’ command
(which is to move into Desktop directory using cd command),
you can use:
alias CD="cd Desktop"
 Here we have give shortname= CD and our longer command =
cd Desktop
 This alias allows you to type ‘CD’ instead of ‘cd Desktop’ for the
same result.
List All Aliases in Linux
To list all aliases in Linux, you can use the alias command without
any arguments. This will display all the currently defined aliases
in your shell session. Here’s how it works:
1. Open Terminal: Launch your terminal.
2. Run the Command: Type alias and press Enter.
alias
This will output a list of all aliases, showing their names and the
commands they represent. For example:
alias ll='ls -la'
alias gs='git status'
alias ..='cd ..'
Each line shows an alias name followed by the command it
represents, making it easy to see and manage your shortcuts.
How to Remove an Alias?
 The removing an existing alias is known as unaliasing. It
following is the syntax of it.
Syntax
unalias [alias name]
 This accurately reflects the process of removing an alias using
the ‘unalias’ command in Linux.

How to create an Aliases Persistent?


While creating an alias in the terminal is useful for the current
session, users may want to make aliases persistent across
sessions. To do this, add the alias command to your shell
configuration file (e.g., ‘.bashrc’ for Bash or ‘.zshrc’ for Zsh). This
ensures that your aliases are loaded each time you start a new
terminal session.
echo "alias lsa='ls -la'" >> ~/.bashrc

Using Alias Command Effectively


Once aliases are set up, incorporating them into your workflow
can significantly enhance productivity. Users can create aliases
for commonly used commands, complex sequences, or even
personalized shortcuts. For example, an alias like ‘update’ for the
system update command could be:
alias update='sudo apt update && sudo apt upgrade'

 With this alias, typing ‘update’ in the terminal will execute both
update and upgrade commands in sequence.

How to remove all aliases at once using


unalias?
Step 1: Open terminal for accessing command line interface.
Step 2: Execute the following command to unlias all the aliases:
unlias - a

Step 3: Verify Removal, to ensure all the aliases are been


removed with the following command
alias

 On displaying no output confirms that all aliases have been


successfully removed.

Commonly used Aliases Command Examples


with Options
The following are the some of the commonly used aliases
command:
1. Directory Navigation
alias ..='cd ..'
 This allows users to move up one directory level by typing ‘..’
instead of ‘cd ..’.
2. List Files with Details
alias ll='ls -l'
 Shortening the ‘ls -l’ command for a detailed file list.
3. Clearing the Screen
alias cls='clear'

 Typing ‘cls’ clears the terminal screen.


Best practices of using Alias Command
The following are the best practices of using alias command:
 Keep the aliases simple and easy to remember for frequent
commands.
 Document aliases for clarity and future reference.
 Avoid overriding system commands to prevent confusion or
errors.
 Ensure aliases are defined persistently in shell startup files.
 Use aliases sparingly to maintain a clean and manageable
command-line environment.

You might also like