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

Linux Lab Exercise Summary

Linux
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Linux Lab Exercise Summary

Linux
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Linux lab exercise summary

40 Essential Linux Commands That Every User Should Know

Linux is a family of open-source Unix operating systems based on the Linux Kernel. They
include Ubuntu, Fedora, Debian, openSUSE, and Red Hat.When operating Linux, you need to
use a shell – a program that gives you access to the operating system’s services. Most Linux
distributions use a graphical user interface (GUI), making them beginner-friendly.

However, we recommend utilizing the command-line interface (CLI) because it’s quicker and
offers more control. Tasks that require multiple steps on the GUI can be done in a matter of
seconds by entering commands into the CLI.

So if you want to use Linux, learning the common utilities or commands will go a long way. This
article will discuss the 40 basic commands to help you use your Linux distro effectively.

● What Is a Linux Command?


● The 40 Most Commonly Used Linux Commands
○ 1. sudo command
○ 2. pwd command
○ 3. cd command
○ 4. ls command
○ 5. cat command
○ 6. cp command
○ 7. mv command
○ 8. mkdir command
○ 9. rmdir command
○ 10. rm command
○ 11. touch command
○ 12. locate command
○ 13. find command
○ 14. grep command
○ 15. df command
○ 16. du command
○ 17. head command
○ 18. tail command
○ 19. diff command
○ 20. tar command
○ 21. chmod command
○ 22. chown command
○ 23. jobs command
○ 24. kill command
○ 25. ping command
○ 26. wget command
○ 27. uname command
○ 28. top command
○ 29. history command
○ 30. man command
○ 31. echo command
○ 32. zip, unzip commands
○ 33. hostname command
○ 34. useradd, userdel commands
○ 35. apt-get command
○ 36. nano, vi, jed commands
○ 37. alias, unalias commands
○ 38. su command
○ 39. htop command
○ 40. ps command
○ Bonus Tips and Tricks

What Is a Linux Command?

A Linux command is a program or utility that runs on the CLI – a console that interacts with the
system via texts and processes. It’s similar to the Command Prompt application in Windows.

Linux commands are executed on Terminal by pressing Enter at the end of the line. You can run
commands to perform various tasks, from package installation to user management and file
manipulation.

Here’s what a Linux command’s general syntax looks like:

CommandName [option(s)] [parameter(s)]

A command may contain an option or a parameter. In some cases, it can still run without them.
These are the three most common parts of a command:

● CommandName is the rule that you want to perform.


● Option or flag modifies a command’s operation. To invoke it, use hyphens (–) or double
hyphens (—).
● Parameter or argument specifies any necessary information for the command.

Keep in mind that all Linux commands are case-sensitive.

The 40 Most Commonly Used Linux Commands


Before proceeding to the list of basic commands, you need to open Terminal first.

Although the steps may differ depending on your Linux distribution, the Terminal application is
usually found in the Utilities section.

Here is the list of basic Linux commands:

1. sudo command

Short for superuser do, sudo is one of the most popular basic Linux commands that lets you
perform tasks that require administrative or root permissions.

When using sudo, the system will prompt users to authenticate themselves with a password.
Then, the Linux system will log a timestamp as a tracker. By default, every root user can run
sudo commands for 15 minutes/session.

If you try to run sudo in the command line without authenticating yourself, the system will log
the activity as a security event.

Here’s the general syntax:

sudo (command)

You can also add an option, such as:

● -k or –reset-timestamp invalidates the timestamp file.


● -g or –group=group runs commands as a specified group name or ID.
● -h or –host=host runs commands on the host.

2. pwd command

Use the pwd command to find the path of your current working directory. Simply entering pwd
will return the full current path – a path of all the directories that starts with a forward slash (/).
For example, /home/username.

The pwd command uses the following syntax:

pwd [option]

It has two acceptable options:

● -L or –logical prints environment variable content, including symbolic links.


● -P or –physical prints the actual path of the current directory.
3. cd command

To navigate through the Linux files and directories, use the cd command. Depending on your
current working directory, it requires either the full path or the directory name.

Running this command without an option will take you to the home folder. Keep in mind that
only users with sudo privileges can execute it.

Let’s say you’re in /home/username/Documents and want to go to Photos, a subdirectory of


Documents. To do so, enter the following command:

cd Photos.

If you want to switch to a completely new directory, for example, /home/username/Movies, you
have to enter cd followed by the directory’s absolute path:

cd /home/username/Movies

Here are some shortcuts to help you navigate:

● cd ~[username] goes to another user’s home directory.


● cd .. moves one directory up.
● cd- moves to your previous directory.

4. ls command

The ls command lists files and directories within a system. Running it without a flag or
parameter will show the current working directory’s content.

To see other directories’ content, type ls followed by the desired path. For example, to view files
in the Documents folder, enter:

ls /home/username/Documents

Here are some options you can use with the ls command:

● ls -R lists all the files in the subdirectories.


● ls -a shows hidden files in addition to the visible ones.
● ls -lh shows the file sizes in easily readable formats, such as MB, GB, and TB.

5. cat command
Concatenate, or cat, is one of the most frequently used Linux commands. It lists, combines, and
writes file content to the standard output. To run the cat command, type cat followed by the file
name and its extension. For instance:

cat filename.txt.

Here are other ways to use the cat command:

● cat > filename.txt creates a new file.


● cat filename1.txt filename2.txt > filename3.txt merges filename1.txt and filename2.txt
and stores the output in filename3.txt.
● tac filename.txt displays content in reverse order.

6. cp command

Use the cp command to copy files or directories and their content. Take a look at the following
use cases.

To copy one file from the current directory to another, enter cp followed by the file name and the
destination directory. For example:

cp filename.txt /home/username/Documents

To copy files to a directory, enter the file names followed by the destination directory:

cp filename1.txt filename2.txt filename3.txt /home/username/Documents

To copy the content of a file to a new file in the same directory, enter cp followed by the source
file and the destination file:

cp filename1.txt filename2.txt

To copy an entire directory, pass the -R flag before typing the source directory, followed by the
destination directory:

cp -R /home/username/Documents /home/username/Documents_backup

7. mv command

The primary use of the mv command is to move and rename files and directories. Additionally, it
doesn’t produce an output upon execution.

Simply type mv followed by the filename and the destination directory. For example, you want
to move filename.txt to the /home/username/Documents directory:
mv filename.txt /home/username/Documents.

You can also use the mv command to rename a file:

mv old_filename.txt new_filename.txt

8. mkdir command

Use the mkdir command to create one or multiple directories at once and set permissions for
each of them. The user executing this command must have the privilege to make a new folder in
the parent directory, or they may receive a permission denied error.

Here’s the basic syntax:

mkdir [option] directory_name

For example, you want to create a directory called Music:

mkdir Music

To make a new directory called Songs inside Music, use this command:

mkdir Music/Songs

The mkdir command accepts many options, such as:

● -p or –parents create a directory between two existing folders. For example, mkdir -p
Music/2020/Songs will make the new “2020” directory.
● -m sets the file permissions. For instance, to create a directory with full read, write, and
execute permissions for all users, enter mkdir -m777 directory_name.
● -v prints a message for each created directory.

9. rmdir command

To permanently delete an empty directory, use the rmdir command. Remember that the user
running this command should have sudo privileges in the parent directory.

For example, you want to remove an empty subdirectory named personal1 and its main folder
mydir:

rmdir -p mydir/personal1

10. rm command
The rm command is used to delete files within a directory. Make sure that the user performing
this command has write permissions.

Remember the directory’s location as this will remove the file(s) and you can’t undo it.

Here’s the general syntax:

rm filename

To remove multiple files, enter the following command:

rm filename1 filename2 filename3

Here are some acceptable options you can add:

● -i prompts system confirmation before deleting a file.


● -f allows the system to remove without a confirmation.
● -r deletes files and directories recursively.

11. touch command

The touch command allows you to create an empty file or generate and modify a timestamp in
the Linux command line.

For example, enter the following command to create an HTML file named Web in the
Documents directory:

touch /home/username/Documents/Web.html

12. locate command

The locate command can find a file in the database system.

Moreover, adding the -i argument will turn off case sensitivity, so you can search for a file even
if you don’t remember its exact name.

To look for content that contains two or more words, use an asterisk (*). For example:

locate -i school*not

The command will search for files that contain the words school and note, whether they use
uppercase or lowercase letters.

13. find command


Use the find command to search for files within a specific directory and perform subsequent
operations. Here’s the general syntax:

find [option] [path] [expression]

For example, you want to look for a file called notes.txt within the home directory and its
subfolders:

find /home -name notes.txt

Here are other variations when using find:

● find -name filename.txt to find files in the current directory.


● find ./ -type d -name directoryname to look for directories.

14. grep command

Another basic Linux command on the list is grep or global regular expression print. It lets you
find a word by searching through all the texts in a specific file.

Once the grep command finds a match, it prints all lines that contain the specific pattern. This
command helps filter through large log files.

For example, you want to search for the word blue in the notepad.txt file:

grep blue notepad.txt

The command’s output will display lines that contain blue.

15. df command

Use the df command to report the system’s disk space usage, shown in percentage and kilobyte
(KB). Here’s the general syntax:

df [options] [file]

For example, enter the following command if you want to see the current directory’s system disk
space usage in a human-readable format:

df -h

These are some acceptable options to use:

● df -m displays information on the file system usage in MBs.


● df -k displays file system usage in KBs.
● df -T shows the file system type in a new column.

16. du command

If you want to check how much space a file or a directory takes up, use the du command. You
can run this command to identify which part of the system uses the storage excessively.

Remember, you must specify the directory path when using the du command. For example, to
check /home/user/Documents enter:

du /home/user/Documents

Adding a flag to the du command will modify the operation, such as:

● -s offers the total size of a specified folder.


● -m provides folder and file information in MB
● k displays information in KB.
● -h informs the last modification date of the displayed folders and files.

17. head command

The head command allows you to view the first ten lines of a text. Adding an option lets you
change the number of lines shown. The head command is also used to output piped data to the
CLI.

Here’s the general syntax:

head [option] [file]

For instance, you want to view the first ten lines of note.txt, located in the current directory:

head note.txt

Below are some options you can add:

● -n or –lines prints the first customized number of lines. For example, enter head -n 5
filename.txt to show the first five lines of filename.txt.
● -c or –bytes prints the first customized number of bytes of each file.
● -q or –quiet will not print headers specifying the file name.

18. tail command

The tail command displays the last ten lines of a file. It allows users to check whether a file has
new data or to read error messages.
Here’s the general format:

tail [option] [file]

For example, you want to show the last ten lines of the colors.txt file:

tail -n colors.txt

19. diff command

Short for difference, the diff command compares two contents of a file line by line. After
analyzing them, it will display the parts that do not match.

Programmers often use the diff command to alter a program instead of rewriting the entire source
code.

Here’s the general format:

diff [option] file1 file2

For example, you want to compare two text files – note.txt and note_update.txt:

diff note.txt note_update.txt

Here are some acceptable options to add:

● -c displays the difference between two files in a context form.


● -u displays the output without redundant information.
● -i makes the diff command case insensitive.

20. tar command

The tar command archives multiple files into a TAR file – a common Linux format similar to
ZIP, with optional compression.

Here’s the basic syntax:

tar [options] [archive_file] [file or directory to be archived]

For instance, you want to create a new TAR archive named newarchive.tar in the
/home/user/Documents directory:

tar -cvf newarchive.tar /home/user/Documents

The tar command accepts many options, such as:


● -x extracts a file.
● -t lists the content of a file.
● -u archives and adds to an existing archive file.

Check out the more practical examples to know more about the other functions.

21. chmod command

chmod is a common command that modifies a file or directory’s read, write, and execute
permissions. In Linux, each file is associated with three user classes – owner, group member, and
others.

Here’s the basic syntax:

chmod [option] [permission] [file_name]

For example, the owner is currently the only one with full permissions to change note.txt. To
allow group members and others to read, write, and execute the file, change it to the -rwxrwxrwx
permission type, whose numeric value is 777:

chmod 777 note.txt

This command supports many options, including:

● -c or –changes displays information when a change is made.


● -f or –silent suppresses the error messages.
● -v or –verbose displays a diagnostic for each processed file.

22. chown command

The chown command lets you change the ownership of a file, directory, or symbolic link to a
specified username.

Here’s the basic format:

chown [option] owner[:group] file(s)

For example, you want to make linuxuser2 the owner of filename.txt:

chown linuxuser2 filename.txt

23. jobs command


A job is a process that the shell starts. The jobs command will display all the running processes
along with their statuses. Remember that this command is only available in csh, bash, tcsh, and
ksh shells.

This is the basic syntax:

jobs [options] jobID

To check the status of jobs in the current shell, simply enter jobs to the CLI.

Here are some options you can use:

● -l lists process IDs along with their information.


● -n lists jobs whose statuses have changed since the last notification.
● -p lists process IDs only.

24. kill command

Use the kill command to terminate an unresponsive program manually. It will signal
misbehaving applications and instruct them to close their processes.

To kill a program, you must know its process identification number (PID). If you don’t know the
PID, run the following command:

ps ux

After knowing what signal to use and the program’s PID, enter the following syntax:

kill [signal_option] pid

There are 64 signals that you can use, but these two are among the most commonly used:

● SIGTERM requests a program to stop running and gives it some time to save all of its
progress. The system will use this by default if you don’t specify the signal when entering
the kill command.
● SIGKILL forces programs to stop, and you will lose unsaved progress.

For example, the program’s PID is 63773, and you want to force it to stop:

kill SIGKILL 63773

25. ping command


The ping command is one of the most used basic Linux commands for checking whether a
network or a server is reachable. In addition, it is used to troubleshoot various connectivity
issues.

Here’s the general format:

ping [option] [hostname_or_IP_address]

For example, you want to know whether you can connect to Google and measure its response
time:

ping google.com

26. wget command

The Linux command line lets you download files from the internet using the wget command. It
works in the background without hindering other running processes.

The wget command retrieves files using HTTP, HTTPS, and FTP protocols. It can perform
recursive downloads, which transfer website parts by following directory structures and links,
creating local versions of the web pages.

To use it, enter the following command:

wget [option] [url]

For example, enter the following command to download the latest version of WordPress:

wget https://wordpress.org/latest.zip

27. uname command

The uname or unix name command will print detailed information about your Linux system and
hardware. This includes the machine name, operating system, and kernel. To run this command,
simply enter uname into your CLI.

Here’s the basic syntax:

uname [option]

These are the acceptable options to use:

● -a prints all the system information.


● -s prints the kernel name.
● -n prints the system’s node hostname.

28. top command

The top command in Linux Terminal will display all the running processes and a dynamic real-
time view of the current system. It sums up the resource utilization, from CPU to memory usage.

The top command can also help you identify and terminate a process that may use too many
system resources.

To run the command, simply enter top into the CLI.

29. history command

With history, the system will list up to 500 previously executed commands, allowing you to
reuse them without re-entering. Keep in mind that only users with sudo privileges can execute
this command. How this utility runs also depends on which Linux shell you use.

To run it, enter the command below:

history [option]

This command supports many options, such as:

● -c clears the complete history list.


● -d offset deletes the history entry at the OFFSET position.
● -a appends history lines.

30. man command

The man command provides a user manual of any commands or utilities you can run in
Terminal, including the name, description, and options.

It consists of nine sections:

● Executable programs or shell commands


● System calls
● Library calls
● Games
● Special files
● File formats and conventions
● System administration commands
● Kernel routines
● Miscellaneous
To display the complete manual, enter:

man [command_name]

For example, you want to access the manual for the ls command:

man ls

Enter this command if you want to specify the displayed section:

man [option] [section_number] [command_name]

For instance, you want to see section 2 of the ls command manual:

man 2 ls

31. echo command

The echo command is a built-in utility that displays a line of text or string using the standard
output. Here’s the basic syntax:

echo [option] [string]

For example, you can display the text Hostinger Tutorials by entering:

echo “Hostinger Tutorials”

This command supports many options, such as:

● -n displays the output without the trailing newline.


● -e enables the interpretation of the following backslash escapes:
● \a plays sound alert.
● \b removes spaces in between a text.
● \c produces no further output.
● -E displays the default option and disables the interpretation of backslash escapes.

32. zip, unzip commands

Use the zip command to compress your files into a ZIP file, a universal format commonly used
on Linux. It can automatically choose the best compression ratio.

The zip command is also useful for archiving files and directories and reducing disk usage.

To use it, enter the following syntax:


zip [options] zipfile file1 file2….

For example, you have a file named note.txt that you want to compress into archive.zip in the
current directory:

zip archive.zip note.txt

On the other hand, the unzip command extracts the zipped files from an archive. Here’s the
general format:

unzip [option] file_name.zip

So, to unzip a file called archive.zip in the current directory, enter:

unzip archive.zip

33. hostname command

Run the hostname command to know the system’s hostname. You can execute it with or without
an option. Here’s the general syntax:

hostname [option]

There are many optional flags to use, including:

● -a or –alias displays the hostname’s alias.


● -A or –all-fqdns displays the machine’s Fully Qualified Domain Name (FQDN).
● -i or –ip-address displays the machine’s IP address.

For example, enter the following command to know your computer’s IP address:

hostname -i

34. useradd, userdel commands

Linux is a multi-user system, meaning more than one person can use it simultaneously. useradd
is used to create a new account, while the passwd command allows you to add a password. Only
those with root privileges or sudo can run the useradd command.

When you use the useradd command, it performs some major changes:

● Edits the /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow files for the newly
created accounts.
● Creates and populates a home directory for the user.
● Sets file permissions and ownerships to the home directory.

Here’s the basic syntax:

useradd [option] username

To set the password:

passwd the_password_combination

For example, to add a new person named John, enter the following command simultaneously:

useradd John

passwd 123456789

To delete a user account, use the userdel command:

userdel username

35. apt-get command

apt-get is a command line tool for handling Advanced Package Tool (APT) libraries in Linux. It
lets you retrieve information and bundles from authenticated sources to manage, update, remove,
and install software and its dependencies.

Running the apt-get command requires you to use sudo or root privileges.

Here’s the main syntax:

apt-get [options] (command)

These are the most common commands you can add to apt-get:

● update synchronizes the package files from their sources.


● upgrade installs the latest version of all installed packages.
● check updates the package cache and checks broken dependencies.

36. nano, vi, jed commands

Linux allows users to edit and manage files via a text editor, such as nano, vi, or jed. nano and vi
come with the operating system, while jed has to be installed.

The nano command denotes keywords and can work with most languages. To use it, enter the
following command:
nano [filename]

vi uses two operating modes to work – insert and command. insert is used to edit and create a
text file. On the other hand, the command performs operations, such as saving, opening, copying,
and pasting a file.

To use vi on a file, enter:

vi [filename]

jed has a drop-down menu interface that allows users to perform actions without entering
keyboard combinations or commands. Like vi, it has modes to load modules or plugins to write
specific texts.

To open the program, simply enter jed to the command line.

37. alias, unalias commands

alias allows you to create a shortcut with the same functionality as a command, file name, or text.
When executed, it instructs the shell to replace one string with another.

To use the alias command, enter this syntax:

alias Name=String

For example, you want to make k the alias for the kill command:

alias k=’kill’

On the other hand, the unalias command deletes an existing alias.

Here’s what the general syntax looks like:

unalias [alias_name]

38. su command

The switch user or su command allows you to run a program as a different user. It changes the
administrative account in the current log-in session. This command is especially beneficial for
accessing the system through SSH or using the GUI display manager when the root user is
unavailable.

Here’s the general syntax of the command:

su [options] [username [argument]]


When executed without any option or argument, the su command runs through root privileges. It
will prompt you to authenticate and use the sudo privileges temporarily.

Here are some acceptable options to use:

● -p or –preserve-environment keeps the same shell environment, consisting HOME,


SHELL, USER, and LOGNAME.
● -s or –shell lets you specify a different shell environment to run.
● -l or –login runs a login script to switch to a different username. Executing it requires you
to enter the user’s password.

39. htop command

The htop command is an interactive program that monitors system resources and server
processes in real time. It is available on most Linux distributions, and you can install it using the
default package manager.

Compared to the top command, htop has many improvements and additional features, such as
mouse operation and visual indicators.

To use it, run the following command:

htop [options]

You can also add options, such as:

● -d or –delay shows the delay between updates in tenths of seconds.


● -C or –no-color enables the monochrome mode.
● -h or –help displays the help message and exit.

40. ps command

The process status or ps command produces a snapshot of all running processes in your system.
The static results are taken from the virtual files in the /proc file system.

Executing the ps command without an option or argument will list the running processes in the
shell along with:

● The unique process ID (PID)


● The type of the terminal (TTY)
● The running time (TIME)
● The command that launches the process (CMD)

Here are some acceptable options you can use:


● -T displays all processes associated with the current shell session.
● -u username lists processes associated with a specific user.
● -A or -e shows all the running processes.

Bonus Tips and Tricks

Here are some tips and tricks you can use to manage the Linux system:

● Enter the clear command to clean the Terminal screen.


● Press the Tab button to autofill after entering a command with an argument.
● Use Ctrl + C to terminate a running command.
● Press Ctrl + Z to pause a working command.
● Use Ctrl + S to freeze your Terminal temporarily.
● Press Ctrl + Q to undo the Terminal freeze.
● Use Ctrl + A to move to the beginning of the line.
● Press Ctrl + E to bring you to the end of the line.
● When executing multiple commands in a single line, use (;) to separate them.
Alternatively, use && to only allow the next command to run if the previous one is
successful.

Conclusion

Linux commands let you perform basic and administrative tasks quickly and effectively from the
Terminal. With it, you’ll have more flexibility over your system and the ability to access features
that are not always available through a graphical user interface.

Learning basic Linux commands is essential to interact with your machine, primarily if you use a
Virtual Private Server (VPS). In most cases, each utility consists of three parts – a command
name, a flag or option, and a parameter or argument.

This article has discussed 40 common commands, such as apt-get to install a package, nano to
manipulate a file, htop to monitor current processes, and ls to view a directory.

We hope this article has helped you learn the basic Linux commands. If you have any questions
or suggestions, please leave them in the comments section below.

What Is the Basic Command of Linux?

One of the most commonly used Linux command is the cat command (short for concatenate).
This command helps you create, view and combine files, as well as redirect the output (the
contents of the whole file).

How Many Commands Does Linux Have?


There are 100+ commands that are shared by Linux and other similar operating systems.

Process management commands

Types of Processes in Linux

In Linux processes can be of two types:

● Foreground Processes
depend on the user for input
also referred to as interactive processes
● Background Processes
run independently of the user
referred to as non-interactive or automatic processes

Process States in Linux

A process in Linux can go through different states after it’s created and before it’s terminated.
These states are:

● Running
● Sleeping
○ Interruptible sleep
○ Uninterruptible sleep
● Stopped
● Zombie
● A process in running state means that it is running or it’s ready to run.
● The process is in a sleeping state when it is waiting for a resource to be available.
● A process in Interruptible sleep will wakeup to handle signals, whereas a process in
Uninterruptible sleep will not.
● A process enters a stopped state when it receives a stop signal.
● Zombie state is when a process is dead but the entry for the process is still present in the
table.

Different Commands for Process Management in Linux

There are two commands available in Linux to track running processes. These two commands
are Top and Ps.

1. The top Command for Mananging Linux Processes

To track the running processes on your machine you can use the top command.

$top
Top command displays a list of processes that are running in real-time along with their memory
and CPU usage. Let’s understand the output a little better:

● PID: Unique Process ID given to each process.


● User: Username of the process owner.
● PR: Priority given to a process while scheduling.
● NI: ‘nice’ value of a process.
● VIRT: Amount of virtual memory used by a process.
● RES: Amount of physical memory used by a process.
● SHR: Amount of memory shared with other processes.
● S: state of the process
○ ‘D’ = uninterruptible sleep
○ ‘R’ = running
○ ‘S’ = sleeping
○ ‘T’ = traced or stopped
○ ‘Z’ = zombie
● %CPU: Percentage of CPU used by the process.
● %MEM; Percentage of RAM used by the process.
● TIME+: Total CPU time consumed by the process.
● Command: Command used to activate the process.

You can use the up/down arrow keys to navigate up and down through the list. To quit press q.
To kill a process, highlight the process with the up/down arrow keys and press ‘k’.

Alternatively, you can also use the kill command, which we will see later.

2. ps command

ps command is short for ‘Process Status’. It displays the currently-running processes. However,
unlike the top command, the output generated is not in realtime.

$ps

The terminology is as follows :


process ID
PID

TTY terminal type

TIME total time the process has been running

CMD name of the command that launches the process

To get more information using ps command use:

$ ps -u

Copy

Here:

● %CPU represents the amount of computing power the process is taking.


● %MEM represents the amount of memory the process is taking up.
● STAT represents process state.
While ps command only displays the processes that are currently running, you can also use it to
list all the processes.

$ ps -A

This command lists even those processes that are currently not running.

3. Stop a process

To stop a process in Linux, use the 'kill’ command. kill command sends a signal to the process.

There are different types of signals that you can send. However, the most common one is ‘kill -9’
which is ‘SIGKILL’.

You can list all the signals using:

$ KILL -L
Kill L

The default signal is 15, which is SIGTERM. Which means if you just use the kill command
without any number, it sends the SIGTERM signal.

The syntax for killing a process is:

Kill [pid]

Alternatively you can also use :

Kill -9 [pid]

This command will send a ‘SIGKILL’ signal to the process. This should be used in case the
process ignores a normal kill request.

4. Change priority of a process

In Linux, you can prioritize between processes. The priority value for a process is called the
‘Niceness’ value. Niceness value can range from -20 to 19. 0 is the default value.

The fourth column in the output of top command is the column for niceness value.
To start a process and give it a nice value other than the default one, use:

Nice -n [value] [process name]

To change nice value of a process that is already running use:

renice [value] -p ‘pid’

Conclusion

This tutorial covered process management in Linux. Mainly the practical aspects of process
management were covered. In theory, process management is a vast topic and covering it in its
entirety is out of scope for this tutorial.

Command Description
bg To send a process to the background
fg To run a stopped process in the foreground
top Details on all Active Processes
ps Give the status of processes running for a user
ps PID Gives the status of a particular process
pidof Gives the Process ID (PID) of a process
kill PID Kills a process
nice Starts a process with a given priority
Changes priority of an already running
renice
process
df Gives free hard disk space on your system
free Gives free RAM on your system

Memory management commands


● Commands to Check Memory Use in Linux cat Command to Show Linux Memory Information
Entering cat /proc/meminfo in your terminal opens the /proc/meminfo file.
● free Command to Display the Amount of Physical and Swap Memory, Typing free in your
command terminal provides the following result:

The data represents the used/available memory and the swap memory figures in kilobytes.
total Total installed memory

used Memory currently in use by running processes


(used= total – free – buff/cache)

free Unused memory (free= total – used –


buff/cache)

shared Memory shared by multiple processes

buffers Memory reserved by the OS to allocate as


buffers when process need them

cached Recently used files stored in RAM

buff/cache Buffers + Cache

available Estimation of how much memory is available


for starting new applications, without
swapping.

Compared to the /proc/meminfo file, the free command provides less information. However, it is
easier to understand. The key figure being the available value as it displays how much memory is
still available for running new applications.

The free command has multiple options to format the output so that it better matches your
requirements. The table below lists the most useful variations of the free command.

Options Result
-b output in bytes
-k output in kibibytes
-m output in mebibytes
-g output in gibibytes
detailed low and high memory
-l
statistics
-o old format (no -/+buffers/cache line)
-t total for RAM + swap
-s update every [delay] seconds
-c update [count] times
● vmstat Command to Report Virtual Memory Statistics
●The vmstat command is a useful tool that reports virtual memory statistics. vmstat
provides general information about processes, memory, paging, block IO, traps, and CPU
activity.
The basic vmstat syntax is:

vmstat [options][delay [count]]


● Options – various switches to customize the output.
● Delay – defines the time elapsed between output updates.
● Count – the number of output updates after the specified delay interval. If count isn’t set,
the default value is infinite.

The list of available options:

Option: Description:
-a Displays active and inactive memory.
-f Displays the number of forks since boot.
-m Displays slab statistics.
-n Displays the header only once rather than periodically.
Displays a table of various event counters and memory
-s
statistics.
-d Displays disk statistics.
-D Detailed disk activity report.
-p Detailed partition statistics.
-t Adds a timestamp to the report.
-S [unit] Switches output units.
-V Displays version information.
-h Displays help.
man vmstat Displays a user manual in the terminal.

The detailed description listed below provides an explanation for each value in case you need
assistance in analyzing the results.

● Procs
● r: number of processes waiting for run time.
● b: number of processes in uninterruptible sleep.
● Memory
● swpd: amount of virtual memory used.
● free: amount of idle memory.
● buff: the amount of memory used as buffers.
● cache: amount of memory used as cache.
● Swap
● si: memory swapped in from disk (/s).
● so: memory swapped to disk (/s).
● IO
● bi: Blocks received from a block device (blocks/s).
● bo: Blocks sent to a block device (blocks/s).
● System
● in: number of interrupts per second, including the clock.
● cs: number of context switches per second.
● CPU – These are percentages of total CPU time.
● us: Time spent running non-kernel code. (user time, including nice time)
● sy: Time spent running kernel code. (system time)
● id: Time spent idle. Before Linux 2.5.41, this includes IO-wait time.
● wa: Time spent waiting for IO. Before Linux 2.5.41, included in idle.
● st: Time stolen from a virtual machine. Before Linux 2.6.11, unknown.

top Command to Check Memory Use


The top command is useful to check memory and CPU usage per process. It displays information
about:
● uptime
● average load
● tasks running
● number of users logged in
● number of CPUs/CPU utilization
● memory/swap system processes

The data is continuously updated, which allows you to follow the processes in real-time.

$htop

The following output appears:

1. The top segment provides summary information and contains graphic meters and text
counters.
2. The lower section structures the detailed data, per process. This allows you to perform
actions on individual processes with ease.
3. The shortcuts listed at the bottom of the screen will enable you to manipulate and
customize the processes quickly and without the need to type specific commands.

You might also like