0% found this document useful (0 votes)
9 views34 pages

Module i Sample Notes

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

Module i Sample Notes

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

lOMoARcPSD|49054302

Module I - sample notes

Linux administration (Mahatma Gandhi University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Adhiyee ([email protected])
lOMoARcPSD|49054302

Module I

Overview of Linux: Architecture of Linux, features, advantages, Booting


process, kernel, Shell Types, Shell variables, Linux file system, simple
commands- echo, date, cal, etc. Commands for files and directories- mkdir, cd,
pwd, ls, rmdir, mv, cp, rm, more, less. Creating and viewing file using cat. File
permissions chmod- filters—head, tail, cut, paste, sort, uniq, grep, pipe,tr,
teeCommunication &Scheduling commands- mail, wall, write, talk, at, cron,
crontab.

Linux operating system

 An operating system can be described as an interface among the


computer hardware and the user of any computer.
 It is a group of software that handles the resources of the computer
hardware and facilitates basic services for computer programs.
 An operating system is an essential component of system software within
a computer system.
 The primary aim of an operating system is to provide a platform where a
user can run any program conveniently or efficiently.

Architecture of Linux system

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

The Linux operating system's architecture mainly contains some of the


components:

 The Kernel

 System Library

 Hardware layer

 System

 Shell utility.

1. Kernel:

 The kernel is one of the core section of an operating system.

 It is responsible for each of the major actions of the Linux OS.

 This operating system contains distinct types of modules and


cooperates with underlying hardware directly.

 The kernel facilitates required abstraction for hiding details of


low-level hardware or application programs to the system.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

2. System Libraries

 These libraries can be specified as some special functions.

 These are applied for implementing the operating system's


functionality and don't need code access rights of the modules of
kernel.

3. System Utility Programs

It is responsible for doing specialized level and individual activities.

4. Hardware layer

Linux operating system contains a hardware layer that consists of several


peripheral devices like CPU, HDD, and RAM.

5. Shell

 It is an interface among the kernel and user. It can afford the


services of kernel.

 It can take commands through the user and runs the functions of
the kernel.

 The shell is available in distinct types of OSes.

 These operating systems are categorized into two different types,


which are the graphical shells and command-line shells.

 The graphical line shells facilitate the graphical user interface,


while the command line shells facilitate the command line
interface.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

Linux Operating System Features

Some of the primary features of Linux OS are as follows:

o Portable: Linux OS can perform different types of hardware and the


kernel of Linux supports the installation of any type of hardware
environment.
o Open source: Linux operating system source code is available freely and
for enhancing the capability of the Linux OS, several teams are
performing in collaboration.
o Multiprogramming: Linux OS can be defined as a multiprogramming
system. It means more than one application can be executed at the same
time.
o Multi-user: Linux OS can also be defined as a multi-user system. It
means more than one user can use the resources of the system such
as application programs, memory, or RAM at the same time.
o Hierarchical file system: Linux OS affords a typical file structure where
user files or system files are arranged.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

o Security: Linux OS facilitates user security systems with the help of


various features of authentication such as controlled access to specific
files, password protection, or data encryption.
o Shell: Linux operating system facilitates a unique interpreter program.
This type of program can be applied for executing commands of the
operating system. It can be applied to perform various types of tasks such
as call application programs and others.

Top 20 Advantages of Linux

1. pen Source

As it is open-source, its source code is easily available. Anyone having


programming knowledge can customize the operating system. One can
contribute, modify, distribute, and enhance the code for any purpose.

2. Security

The Linux security feature is the main reason that it is the most favorable option
for developers. It is not completely safe, but it is less vulnerable than others.
Each application needs to authorize by the admin user. The virus is not executed
until the administrator provides the access password. Linux systems do not
require any antivirus program.

3. Free

Certainly, the biggest advantage of the Linux system is that it is free to use. We
can easily download it, and there is no need to buy the license for it. It is
distributed under GNU GPL (General Public License). Comparatively, we have
to pay a huge amount for the license of the other operating systems.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

4. Lightweight

Linux is lightweight. The requirements for running Linux are much less than
other operating systems. In Linux, the memory footprint and disk space are also
lower. Generally, most of the Linux distributions required as little as 128MB of
RAM around the same amount for disk space.

5. Stability

Linux is more stable than other operating systems. Linux does not require to
reboot the system to maintain performance levels. It rarely hangs up or slow
down. It has big up-times.

6. Performance

Linux system provides high performance over different networks. It is capable


of handling a large number of users simultaneously.

7. Flexibility

Linux operating system is very flexible. It can be used for desktop applications,
embedded systems, and server applications too. It also provides various
restriction options for specific computers. We can install only necessary
components for a system.

8. Software Updates

In Linux, the software updates are in user control. We can select the required
updates. There a large number of system updates are available. These updates
are much faster than other operating systems. So, the system updates can be
installed easily without facing any issue.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

9. Distributions/ Distros

There are many Linux distributions available in the market. It provides various
options and flavors of Linux to the users. We can choose any distros according
to our needs. Some popular distros are Ubuntu, Fedora, Debian, Linux Mint,
Arch Linux, and many more.

For the beginners, Ubuntu and Linux Mint would be useful and, Debian and
Fedora would be good choices for proficient programmers.

10. Live CD/USB

Almost all Linux distributions have a Live CD/USB option. It allows us to try
or run the Linux operating system without installing it.

11. Graphical User Interface

Linux is a command-line based OS but, it provides an interactive user interface


like Windows.

12. Suitable for programmers

It supports almost all of the most used programming languages such as C/C+
+, Java, Python, Ruby, and more. Further, it offers a vast range of useful
applications for development.

The programmers prefer the Linux terminal over the Windows command line.
The package manager on Linux system helps programmers to understand how
things are done. Bash scripting is also a functional feature for the programmers.
It also provides support for SSH, which helps in managing the servers quickly.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

13. Community Support

Linux provides large community support. We can find support from various
sources. There are many forums available on the web to assist users. Further,
developers from the various opensource communities are ready to help us.

14. Privacy

Linux always takes care of user privacy as it never takes much private data from
the user. Comparatively, other operating systems ask for the user's private data.

15. Networking

Linux facilitates with powerful support for networking. The client-server


systems can be easily set to a Linux system. It provides various command-line
tools such as ssh, ip, mail, telnet, and more for connectivity with the other
systems and servers. Tasks such as network backup are much faster than others.

16. Compatibility

Linux is compatible with a large number of file formats as it supports almost all
file formats.

17. Installation

Linux installation process takes less time than other operating systems such as
Windows. Further, its installation process is much easy as it requires less user
input. It does not require much more system configuration even it can be easily
installed on old machines having less configuration.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

18. Multiple Desktop Support

Linux system provides multiple desktop environment support for its enhanced
use. The desktop environment option can be selected during installation. We
can select any desktop environment such as GNOME (GNU Network Object
Model Environment) or KDE (K Desktop Environment) as both have their
specific environment.

19. Multitasking

It is a multitasking operating system as it can run multiple tasks simultaneously


without affecting the system speed.

20. Heavily Documented for beginners

There are many command-line options that provide documentation on


commands, libraries, standards such as manual pages and info pages. Also,
there are plenty of documents available on the internet in different formats, such
as Linux tutorials, Linux documentation project, Serverfault, and more. To help
the beginners, several communities are available such as Ask Ubuntu, Reddit,
and StackOverflow.

Stages of Linux Boot Process:


1. The machine’s BIOS (Basic Input/Output System) or boot microcode
hundreds and runs a boot loader.
2. Boot loader finds the kernel image on the disk and loads it into memory, to
start the system.
3. The kernel initializes the devices and their drivers.
4. The kernel mounts the basis filesystem.
5. The kernel starts a program referred to as init with a method ID zero
6. init sets the remainder of the system processes in motion.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

7. For some purpose, init starts a method permitting you to log in, typically at
the top or close to the top of the boot sequence.

KERNAL
The Linux® kernel is the main component of a Linux operating
system (OS) and is the core interface between a computer’s
hardware and its processes. It communicates between the 2,
managing resources as efficiently as possible.
The kernel is so named because—like a seed inside a hard shell—it
exists within the OS and controls all the major functions of the
hardware, whether it’s a phone, laptop, server, or any other kind of
computer.
What the kernel does
The kernel has 4 jobs:

1. Memory management: Keep track of how much memory is used to store what, and where
2. Process management: Determine which processes can use the central processing unit
(CPU), when, and for how long
3. Device drivers: Act as mediator/interpreter between the hardware and processes
4. System calls and security: Receive requests for service from the processes
The kernel, if implemented properly, is invisible to the user, working in its own little
world known as kernel space, where it allocates memory and keeps track of where
everything is stored. What the user sees—like web browsers and files—are known
as the user space. These applications interact with the kernel through a system call
interface (SCI).

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

Think about it like this: The kernel is a busy personal assistant for a powerful
executive (the hardware). It’s the assistant’s job to relay messages and requests
(processes) from employees and the public (users) to the executive, to remember
what is stored where (memory), and to determine who has access to the executive at
any given time and for how long.
Experience Red Hat Enterprise Linux

Where the kernel fits within the OS


To put the kernel in context, you can think of a Linux machine as having 3 layers:

1. The hardware: The physical machine—the bottom or base of the system, made up of
memory (RAM) and the processor or central processing unit (CPU), as well as input/output
(I/O) devices such as storage, networking, and graphics. The CPU performs computations
and reads from, and writes to, memory.
2. The Linux kernel: The core of the OS. (See? It’s right in the middle.) It’s software residing in
memory that tells the CPU what to do.
3. User processes: These are the running programs that the kernel manages. User processes
are what collectively make up user space. User processes are also known as just processes.
The kernel also allows these processes and servers to communicate with each other (known
as inter-process communication, or IPC).
Code executed by the system runs on CPUs in 1 of 2 modes: kernel mode or user
mode. Code running in the kernel mode has unrestricted access to the hardware,
while user mode restricts access to the CPU and memory to the SCI. A similar
separation exists for memory (kernel space and user space). These 2 small details
form the base for some complicated operations like privilege separation
for security, building containers, and virtual machines.

The shell is a command-line interpreter for Linux and Unix systems. It


provides an interface between the user and the kernel and executes
commands. A sequence of commands can be written in a file for
execution in the shell. It is called shell scripting. It helps to automate tasks
in Linux.
Scripting language also has concepts of different types of variables like
procedural or object-oriented languages.
In shell scripting there are three main types of variables are present. They
are –
 Local Variables
 Global Variables or Environment Variables
 Shell Variables or System Variables
We will discuss them one by one in this article –

Local Variable
A local variable is a special type of variable which has its scope only
within a specific function or block of code. Local variables can override the
same variable name in the larger scope.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

Global Variables
A global variable is a variable with global scope. It is accessible
throughout the program. Global variables are declared outside any block
of code or function.

Shell Variables
These are special types of variables. They are created and maintained by
Linux Shell itself. These variables are required by the shell to function
properly They are defined in Capital letters and to see all of them, we can
use set / env / printenv command
Some useful shell variables are –

Variable Name Description Usage

Holds the version of this instance of echo


BASH_VERSION
bash. $BASH_VERSION

Provides a home directory of the


HOME echo $HOME
current user.

HOSTNAME Provides computer name echo $HOSTNAME

USERNAME Provides username echo $USERNAME

Linux File System


A Linux file system is a structured collection of files on a disk drive or a partition. A
partition is a segment of memory and contains some specific data. In our machine, there
can be various partitions of the memory. Generally, every partition contains a file
system.

The general-purpose computer system needs to store data systematically so that we can
easily access the files in less time. It stores the data on hard disks (HDD) or some
equivalent storage type. There may be below reasons for maintaining the file system:

o Primarily the computer saves data to the RAM storage; it may lose the data if it gets
turned off. However, there is non-volatile RAM (Flash RAM and SSD) that is
available to maintain the data after the power interruption.
o Data storage is preferred on hard drives as compared to standard RAM as RAM
costs more than disk space. The hard disks costs are dropping gradually
comparatively the RAM.
The Linux file system contains the following sections:

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

o The root directory (/)


o A specific data storage format (EXT3, EXT4, BTRFS, XFS and so on)
o A partition or logical volume having a particular file system.

What is the Linux File System?


Linux file system is generally a built-in layer of a Linux operating system used to handle
the data management of the storage. It helps to arrange the file on the disk storage. It
manages the file name, file size, creation date, and much more information about a file.

If we have an unsupported file format in our file system, we can download software to
deal with it.

Linux File System Structure


Linux file system has a hierarchal file structure as it contains a root directory and its
subdirectories. All other directories can be accessed from the root directory. A partition
usually has only one file system, but it may have more than one file system.

A file system is designed in a way so that it can manage and provide space for non-
volatile storage data. All file systems required a namespace that is a naming and
organizational methodology. The namespace defines the naming process, length of the
file name, or a subset of characters that can be used for the file name. It also defines the
logical structure of files on a memory segment, such as the use of directories for
organizing the specific files. Once a namespace is described, a Metadata description
must be defined for that particular file.

The data structure needs to support a hierarchical directory structure; this structure is
used to describe the available and used disk space for a particular block. It also has the
other details about the files such as file size, date & time of creation, update, and last
modified.

Also, it stores advanced information about the section of the disk, such as partitions and
volumes.

The advanced data and the structures that it represents contain the information about
the file system stored on the drive; it is distinct and independent of the file system
metadata.

Advertisement

Linux file system contains two-part file system software implementation architecture.
Consider the below image:

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

The file system requires an API (Application programming interface) to access the
function calls to interact with file system components like files and
directories. API facilitates tasks such as creating, deleting, and copying the files. It
facilitates an algorithm that defines the arrangement of files on a file system.

The first two parts of the given file system together called a Linux virtual file system. It
provides a single set of commands for the kernel and developers to access the file
system. This virtual file system requires the specific system driver to give an interface to
the file system.

Directory Structure
The directories help us to store the files and locate them when we need them. Also,
directories are called folders as they can be assumed of as folders where files reside in
the form of a physical desktop analogy. Directories can be organized in a tree-like
hierarchy in Linux and several other operating systems.

The directory structure of Linux is well-documented and defined in the Linux FHS
(Filesystem Hierarchy Standard). Referencing those directories if accessing them is
achieved via the sequentially deeper names of the directory linked by '/' forward slash
like /var/spool/mail and /var/log. These are known as paths.

The below table gives a very short standard, defined, and well-known top-level Linux
directory list and their purposes:

o / (root filesystem): It is the top-level filesystem directory. It must include every file
needed to boot the Linux system before another filesystem is mounted. Every other
filesystem is mounted on a well-defined and standard mount point because of the
root filesystem directories after the system is started.
o /boot: It includes the static kernel and bootloader configuration and executable files
needed to start a Linux computer.
o /bin: This directory includes user executable files.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

o /dev: It includes the device file for all hardware devices connected to the system.
These aren't device drivers; instead, they are files that indicate all devices on the
system and provide access to these devices.
o /etc: It includes the local system configuration files for the host system.
o /lib: It includes shared library files that are needed to start the system.
o /home: The home directory storage is available for user files. All users have a
subdirectory inside /home.
o /mnt: It is a temporary mount point for basic filesystems that can be used at the time
when the administrator is working or repairing a filesystem.
o /media: A place for mounting external removable media devices like USB thumb
drives that might be linked to the host.
o /opt: It contains optional files like vendor supplied application programs that must be
placed here.
o /root: It's the home directory for a root user. Keep in mind that it's not the '/' (root) file
system.
o /tmp: It is a temporary directory used by the OS and several programs for storing
temporary files. Also, users may temporarily store files here. Remember that files
may be removed without prior notice at any time in this directory.
o /sbin: These are system binary files. They are executables utilized for system
administration.
o /usr: They are read-only and shareable files, including executable libraries and
binaries, man files, and several documentation types.
o /var: Here, variable data files are saved. It can contain things such as MySQL, log
files, other database files, email inboxes, web server data files, and much more.

Linux File System Features


In Linux, the file system creates a tree structure. All the files are arranged as a tree and
its branches. The topmost directory called the root (/) directory. All other directories in
Linux can be accessed from the root directory.

Advertisement

Some key features of Linux file system are as following:

o Specifying paths: Linux does not use the backslash (\) to separate the components;
it uses forward slash (/) as an alternative. For example, as in Windows, the data may
be stored in C:\ My Documents\ Work, whereas, in Linux, it would be stored in /home/
My Document/ Work.
o Partition, Directories, and Drives: Linux does not use drive letters to organize the
drive as Windows does. In Linux, we cannot tell whether we are addressing a
partition, a network device, or an "ordinary" directory and a Drive.
o Case Sensitivity: Linux file system is case sensitive. It distinguishes between
lowercase and uppercase file names. Such as, there is a difference between test.txt
and Test.txt in Linux. This rule is also applied for directories and Linux commands.
o File Extensions: In Linux, a file may have the extension '.txt,' but it is not necessary
that a file should have a file extension. While working with Shell, it creates some
problems for the beginners to differentiate between files and directories. If we use the
graphical file manager, it symbolizes the files and folders.
o Hidden files: Linux distinguishes between standard files and hidden files, mostly the
configuration files are hidden in Linux OS. Usually, we don't need to access or read
the hidden files. The hidden files in Linux are represented by a dot (.) before the file

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

name (e.g., .ignore). To access the files, we need to change the view in the file
manager or need to use a specific command in the shell.

Types of Linux File System


When we install the Linux operating system, Linux offers many file systems such as Ext,
Ext2, Ext3, Ext4, JFS, ReiserFS, XFS, btrfs, and swap.

cal command in Linux with Examples


Last Updated : 02 Sep, 2024



The ‘cal’ command in Linux is a versatile tool that displays


calendars directly in the terminal. If a user wants a quick view of
the calendar in the Linux terminal, ‘cal’ is the command for you.
Here’s a look at the usage and features of ‘cal‘ command in
Linux.
What is the ‘cal’ command?

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

cal command is a calendar command in Linux which is used to


see the calendar of a specific month or a whole year. By default,
entering cal in the terminal shows the calendar of the current
month, with today’s date highlighted. This provides a quick
overview of the month at hand.
Syntax:
cal [ [ month ] year]
The rectangular bracket means it is optional, so if used without an
option, it will display a calendar of the current month and year.
Displaying Different Time Frames
‘cal’: Shows current month calendar on the terminal with the
current date highlighted.

‘cal -y’: Shows the calendar of the complete current year with
the current date highlighted.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

‘cal [month] [year]’: Shows calendar of selected month and


year. For example, ‘cal 08 2000′ displays the calendar for August
2000.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

‘cal [year]’: Shows the whole calendar of the year. For example,
‘cal 2018‘ displays the calendar for the year 2018.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

‘cal [year]| more’: When year is not be visible in the same


screen use ‘more‘ with ‘cal’ use spacebar to scroll down.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

‘cal -3’: Shows calendar of previous, current and next month

‘cal -j’: Shows the calendar of the current month in the Julian
calendar format not in the default Gregorian calendar format.
In Julian calendar format, the date does not reset to 1 after every
month’s end i.e. after 31st Jan, Feb will start as 32nd Feb, not as
1st Feb. But in the Gregorian calendar format, the date is reset to
1 after every month’s end i.e after 31st Jan, Feb will start as of 1st
Feb.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

echo command in Linux with Examples


Last Updated : 12 Mar, 2024



The echo command in Linux is a built-in command that allows


users to display lines of text or strings that are passed as
arguments. It is commonly used in shell scripts and batch files to
output status text to the screen or a file.
Syntax of `echo` command in Linux
echo [option] [string]
Here,
[options] = The various options available for modifying the
behavior of the `echo` command
[string] = It is the string that we want to display.
Basic Usage: Displaying Text/String:
The most straightforward usage of the echo command is to
display a text or string on the terminal. To do this, you simply
provide the desired text or string as an argument to the echo
command.
Syntax:
echo [string]
Example:
If we want to display “Geeks for Geeks”. We use the following
command.
echo "Geeks for Geeks"

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

ntroduction
The "date" command in Linux is a simple but powerful tool used to display
the current date and time, as well as set the system date and time. This
command is extremely useful for troubleshooting and system administration
tasks, and is a vital tool in understanding any Linux user. In this article, we'll
discuss the basic usage of the date command, as well as some of its more
advanced options and features. We'll also cover some examples of how the
date command can be used in real-world scenarios.

Basic usage of date command


The date command is one of the most basic commands in Linux. To display
the current date and time, simply type "date" at the command prompt and
press enter. The output will display the current date and time in the format
"Day Month Date Time TimeZone Year". For instance −

$ date
Tue Jan 25 14:20:34 EST 2022
Formatting the Output
One of the most useful features of the date command is the ability to specify
a particular format for date and time output. This can be done using the "+"
option followed by a format specifier. For example, to display the date in the
"YYYY-MM-DD" format, use the command −
$ date +%F
2022-01-25

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

There are many format specifiers that can be used with the date command,
each of which will display the date and time in a different format. Some of
the more commonly used format specifiers include −
 %Y − displays the year with century as a decimal number
 %m − displays the month as a decimal number (01-12)
 %d − displays the day of the month as a decimal number (01-31)
 %H − displays the hour (24-hour clock) as a decimal number (00-23)
 %M − displays the minute as a decimal number (00-59)
 %S − displays the second as a decimal number (00-59)
Setting the Date and Time
The date command can also be used to set the system date and time. To do
this, you need to log in as the root user. Use the following command to set
the date and time.
$ sudo date -s "25 JAN 2022 14:20:34"
You can also set the date and time with the "-u" option to set it to
Coordinated Universal Time (UTC) instead of local time.
$ sudo date -us "25 JAN 2022 14:20:34"
Linux Directory Commands
1. pwd Command

The pwd command is used to display the location of the current working directory.

Syntax:

1. pwd
Output:

2. mkdir Command

Advertisement

The mkdir command is used to create a new directory under any directory.

Syntax:

1. mkdir <directory name>


Output:

3. rmdir Command

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

The rmdir command is used to delete a directory.

Syntax:

1. rmdir <directory name>


Output:

4. ls Command

The ls command is used to display a list of content of a directory.

Syntax:

Advertisement

1. ls
Output:

5. cd Command

Advertisement

The cd command is used to change the current directory.

Syntax:

1. cd <directory name>
Output:

Linux File commands


6. touch Command

The touch command is used to create empty files. We can create multiple empty files by
executing it once.

Syntax:

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

1. touch <file name>


2. touch <file1> <file2> ....
Output:

7. cat Command

The cat command is a multi-purpose utility in the Linux system. It can be used to create
a file, display content of the file, copy the content of one file to another file, and more.

Syntax:

1. cat [OPTION]... [FILE]..


To create a file, execute it as follows:

1. cat > <file name>


2. // Enter file content
Press "CTRL+ D" keys to save the file. To display the content of the file, execute it as
follows:

1. cat <file name>


Output:

Advertisement

8. rm Command

The rm command is used to remove a file.

Syntax:

rm <file name>

Output:

9. cp Command

The cp command is used to copy a file or directory.

Syntax:

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

To copy in the same directory:

1. cp <existing file name> <new file name>


To copy in a different directory:

Output:

10. mv Command

The mv command is used to move a file or a directory form one location to another
location.

Syntax:

1. mv <file name> <directory path>


Output:

11. rename Command

The rename command is used to rename files. It is useful for renaming a large group of
files.

Syntax:

1. rename 's/old-name/new-name/' files


For example, to convert all the text files into pdf files, execute the below command:

1. rename 's/\.txt$/\.pdf/' *.txt


Output:

Basic Syntax of `cat` Command

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

The basic syntax of the ‘cat’ command is as follows:


cat [OPTION] [FILE]
Here,
[OPTION] : represents various command-line options.
[FILE] : the name of the file(s) to be processed. Let’s explore some of the
common uses of ‘cat’ along with examples.
Practical Examples of Cat Command in Linux
1. How to View the Content of a Single File in Linux
The most basic use of ‘cat’ is to display the contents of a file on the
terminal. This can be achieved by simply providing the filename as an
argument:
Syntax:
cat file_name
Example: If our file_name = jayesh.txt
cat jayesh.txt

cat jayesh.txt

Note: `ls` command is used to display all files and directories in the
current location.
2. How to View the Content of Multiple Files in Linux
Syntax:
cat file_name1 file_name2
Example: If we have two files , file1 and file2.
cat file1 file2

cat file1 file2

3. How to View Contents of a File preceding with Line Numbers in


Linux
Adding the -n option to cat introduces line numbers, making it convenient
to identify and reference specific lines within the file.
Syntax:

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

cat -n file_name
Example: If our file_name is file2.
cat -n file2

cat -n file2

Here, the cat command, used with the redirection ( >), allows you to create
a new file named “jayesh1” and input content directly into it. The
subsequent ls command lists all files in the current location.
4. How to Create a file and add content in Linux Using `cat` Command
If you want to create a new file or overwrite an existing file with new
content, you can use ‘cat’ with the output redirection (` >`):
Syntax:
cat > newfile_name
Example: If we want to create a newfile_name = jayesh1.
cat > jayesh1
ls
This will allow you to type text directly into the terminal, and when you
press Ctrl + D, the entered text will be saved to new_file.txt.
`ls` command is used to display all files and directories in the current
location.

creating file using cat command in linux

5. How to Copy the Contents of One File to Another File in Linux


As the name suggests, ‘cat’ can concatenate multiple files into a single
file.This example illustrates how to copy the entire content of “file1” into
“file2” using the cat command along with redirection ( >).
Syntax:
cat file1.txt file2.txt > merged_file.txt
This command combines the content of file1.txt and file2.txt into a
new file named merged_file.txt.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

6. Cat command can suppress repeated empty lines in output


The -s option comes in handy when dealing with files containing repeated
empty lines. It suppresses these repetitions, providing a cleaner output.
Syntax:
cat -s file_name
Output
Will suppress repeated empty lines in output
7. How to Append the Contents of One File to the End of Another File
If you want to add the content of one file to another, ‘cat’ can be used
along with the append ( >>) operator:
Syntax:
cat file_name1 >> file_name2
Example:
cat file1 >> file2
This will append the content of ` file1` to the end of `file2`

8. How to Display Content in Reverse Order Using `tac` Command in


Linux
The ‘tac’ command is the reverse of ‘cat’ and is used to display the
content of a file in reverse order. The syntax is simple:
Syntax:
tac file_name
Example:
This command will print the content of ‘file2’ in reverse order, displaying
the last line first, followed by the second-to-last line, and so on.
tac file2

tac file2

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

9. How to Highlight the End of Line in Linux


The ‘-E’ option in the ‘cat’ command is used to highlight the end of each
line.
Syntax:
cat -E "filename"
Output:

Displaying $ in the end of line

This will display the content of ‘jayesh1’ with a ‘$’ character at the end of
each line, indicating the line’s terminate.
10. `-A` Command Line Option in `cat` Command in Linux
The ‘-A’ option allows you to combine the effects of ‘-v’, ‘-E’, and ‘-T’
options. Instead of writing ‘-vET’ in the command, you can use ‘-A’:
Syntax:
cat -A "filename"
Explanation:
 The -v option creates non-printing characters visible (except for tabs
and line breaks).
 The -E option emphasizes the end of each line with a $.
 The -T option shows tabs as ^I.
This will display the content of ‘filename’ with non-printing characters
visible, line endings highlighted, and tabs displayed as ‘^I’.
11. How to Open Dashed Files in Linux Using `cat` Command
To open a file with a dash at the beginning of its name, use the ‘–‘ option:
Syntax:
cat -- "-dashfile"
Example:
cat -- "-jayesh2"

displaying content inside a file starting with `-`

This will display the content of a file named “-jayesh2”


12. Cat command if the file has a lot of content and can’t fit in the
terminal.
Syntax:
cat "filename" | more

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

Output:
Will show that much content, which could fit in terminal and
will ask to show more.
13. Merge Contents of Multiple Files Using `cat` Command
To merge the contents of multiple files into a single file, use the redirection
(‘>’)
Syntax:
cat "filename1" "filename2" "filename3" > "merged_filename"
Example:
cat "file1" "file2" "file3" > "merged123"
This will concatenate the contents of “file1” “file2” “file3” into “merged123”.

merging content of multiple files into single file

14. Display Content of All Text Files in a Folder Using `Cat` Command
To display the content of all text files in a folder, use the wildcard (‘*.txt’):
Syntax:
cat *.txt

Displaying all file with extension “.txt”

Will show the content of all text files present in the folder.
15. Cat Command to Append to an Existing File:
To append text to an existing file, use the ‘>>’ operator along with ‘cat’:
Syntax:
cat >> geeks.txt
The newly added text.
This will append the text “The newly added text.” to the end of the
‘geeks.txt’ file.

Downloaded by Adhiyee ([email protected])


lOMoARcPSD|49054302

Downloaded by Adhiyee ([email protected])

You might also like