All Basic Commands in Linux by Akshay
All Basic Commands in Linux by Akshay
All Basic Commands in Linux by Akshay
Introduction
UNIX is the first Operating system in the world, developed by Kem Thompson and Dennis Ritchie in 1969 at Bell
Lab by AT&T Company
IBM : AIX
SGI : IRIX
Sun : Solaris
Free software foundation organization, they start a project by name GNU. The main aim of this project is to
develop such an operating system that can run on any platform.
In 1991, a student Linuz Torvalds developed a kernel named Linux’s kernel plus GNU application called Linux
operating system.
Different companies that provide Linux in Market are Redhat, SuSe, Scientific, Centos, and Knoppix etc.
Features:
Linux is the fastest Operating system in the world. It runs 2 to 3 times fast than windows OS.
Linux is the much-secured OS because there is no any problem of virus.
Linux file format is text format and windows file format is binary format.
Linux is very reliable OS because kernel of Linux is very stable as compared to windows kernel not crashed easily.
Kernel of Linux is very small in size it can be stored in floppy.
Linux uses the x-Window system which is advanced network windowing system. Using this system, we can
display output of any workstation monitor attached in the network.
Advantages:
Virus Proof
Crash Proof
Economical
Multiuser, Multi-Tasking and Multiprocessing capacity
Login Modes:
Login to Text mode we have use (Ctrl+Alt+F1…..F3,F4,F5,F6) (Ctrl+Alt+F2) for Graphical Mode
2. Windows Vs Linux
Windows Linux
It is a proprietary software everything need to It is an open source software everything is
buy free
Less Secure More Secure
More costly Less Cost compare to windows
Chance to get a carpal tunnel syndrome There is no chance to get carpal tunnel
syndrome
Developed by Microsoft corporation Developed by Linus Torvalds
Options need to select You can develop anything as per your
requirement
Kernel is not editable Kernel is editable
Linux Architecture:
Kernel: It is a program. Kernel is a Core component of operating system, interacts directly with hardware and provides low level
services to upper layer components.
Shell: An interface to kernel, hiding complexity of kernel’s functions from users. Takes commands from user and executes kernel’s
functions.
User Interface: In information technology, the user interface (UI) is everything designed into an information device with which a
human being may interact. Two types CLI (Command line interface) and GUI (Graphical User Interface).
System Library - System libraries are special functions or programs using which application programs or system utilities accesses
Kernel's features. These libraries implements most of the functionalities of the operating system and do not requires kernel
module's code access rights.
System Utility - System Utility programs are responsible to do specialized, individual level tasks.
Before you turn over to the next chapter where you are going to meet a plethora of commands, remember a few things that apply
to all UNIX commands.
Date
Cal
Options Description
The "w" command displays information about all users logged into the current system.
who command is used to determine when the system has booted last time, a list of logged-in users, and the system’s current run level.
whoami command is used to print the user’s name from which they are currently logged-in
PWD command in Linux stands for 'print working directory.' It is used to display the current working directory on the screen
Exp: /home/nischal
ls -a: list all files including hidden files. These are files that start with “.”.
ls -A: list all files including hidden files except for “.” and “..” – these refer to the entries for the current directory, and for the parent directory.
ls -R: list all files recursively, descending down the directory tree from the given path.
ls -l: list the files in long format i.e. with an index number, owner name, group name, size, and permissions.
ls – o: list the files in long format but without the group name.
ls -g: list the files in long format but without the owner name.
ls -i: list the files along with their index number.
ls -s: list the files along with their size.
ls -t: sort the list by time of modification, with the newest at the top.
ls -S: sort the list by size, with the largest at the top.
ls -r: reverse the sorting order.
Cat in Linux stands for concatenation (to merge things together) and is one of the most useful and versatile Linux commands.
The cat command has three primary purposes involving text files:
Create
Read/Display
Update/Modify
Exp: cat >new_file_name>
Press “ENTER” and then use “CTRL + d" to exit the prompt.
Some of the common options to use with the cat command are:
Options Description
The mkdir command in Linux/Unix allows users to create or make new directories. mkdir stands for “make directory.
mkdir -p new{1..5}
mkdir -p /linux/redhat/centos
#to create parent
directories.
touch Create an empty file / Multiple empty files yet a time
CP
mv <old name> <new name> ………………………… Rename the file and directory.
mv command main options:
option description
rm Command:
less - read and display contents of text file one page per time.
more command is used to view the text files in the command prompt, displaying one screen at a time in case the file is large
Cd ../../..
HEAD
xTAIL
1) Displays topmost lines 1) Displays bottom-most lines
2) Syntax 2) Syntax
$ head –n filename $ tail –n filename
n > = 1 (default ‘n’ value is 10) n >= 1 (default ‘n’ value is 10)
The output displays ‘n’ top-most lines The output displays ‘n’ bottommost lines
3) Ex, 3) Ex,
$ head -15 test1.txt $ tail -15 test1.txt
Displays top 15 lines Displays bottom 15 lines
By Nischal Tech Support
tail command:
For example, to display the last 5 lines. To do so, invoke the command
head command:
For example, to display the first 5 lines. To do so, invoke the command
There are three redirectors to work with: >, >>, and <. The following
information describes each one:
$ ls /root 2>&1
ls: cannot open directory 'root/': Permission denied
Redirect both standard output and error output for a given command
to a file:
$ cat myfile
/usr/bin/ls
$ echo "Enable Sysadmin" >> myfile
$ cat myfile
/usr/bin/ls
Enable Sysadmin
Append the error output for a given command to an existing file:
| Pipe symbol.
You can run more than one command on a single line by using the command separator,
placing the semicolon between each command.
Command1; command2
wc command
Options:
-c #Character Count
-w #Word Count
1. Absolute Path
You use the absolute path when you specify the path to a file or folder,
starting with the root directory. This path begins with the root (/)
/home
/home/nischal
/home/nischal/test
2. Relative Path
Unlike the absolute path that starts from the root directory, the relative
paths start from the present working directory. Thus, the relative path
changes depending on your current directory
../../…
~/Downloads
File Types
-b - block device file Example: HDD and pen drive
-d - directory file
- - common file
c - Character device file Example: terminal
l - Linked file
Linking means reflecting to the original file, In case of copy command updating is not possible
after copying the file from the source to destination. In link updating is possible for both the files.
Soft Link
Hard Link
Overview
Everything in Linux can be reduced to a file. Partitions are associated with files such as
/dev/hda1. Hardware components are associated with files such as /dev/modem. The Filesystem
Hierarchy Standard (FHS) is the official way to organize files in Unix and Linux directories.
Several major directories are associated with all modern Unix/Linux operating systems.
These directories organize user files, drivers, kernels, logs, programs, utilities, and more into
different categories. The standardization of the FHS makes it easier for users of other Unix-based
operating systems to understand the basics of Linux. All of the other directories shown in Table are
subdirectories of the root directory, unless they are mounted separately.
Directory Description
The root directory, the top-level directory in the FHS. All other directories are subdirectories of
/ root, which is always mounted on some partition. All directories that are not mounted on a
separate partition are included in the root directory’s partition.
Essential command line utilities. Should not be mounted separately; otherwise, it could be
/bin
difficult to get to these utilities when using a rescue disk.
Includes Linux startup files, including the Linux kernel. Can be small; 16MB is usually adequate
/boot for a typical modular kernel. If you use multiple kernels, such as for testing a kernel upgrade,
increase the size of this partition accordingly.
/etc Most basic configuration files.
Hardware and software device drivers for everything from floppy drives to terminals. Do not
/dev
mount this directory on a separate partition.
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 26
By Nischal Tech Support
/mnt The mount point for removable media, including floppy drives, CD-ROMs, and Zip disks.
/tmp Temporary files. By default, Red Hat Linux deletes all files in this directory periodically.
Small programs accessible to all users. Includes many system administration commands and
/usr
utilities.
/var Variable data, including log files and printer spools.
Booting Process
MBR: Master boot record – which will have 446Bytes in size first sector of the HDD.
Stage 1: The duty of the stage 1 is to identify where the stage is located to
Stage 1.5: The duty of the stage 1.5 is to synchronize with the deferent file system. LBA – logical block
addressing. Once executed, core.img will load its configuration file and any other modules needed,
particularly file system drivers; at installation time, it is generated from diskboot.img and configured to
load the stage 2 by its file path.
Stage 2: it will list out all the available operating systems and it will boot with the default operating
system.
Init: init is the root daemon process after this process all the process will start under init
Run levels:
0 - Shutdown/Halt/Power off
4 - Unused
5 - GUI mode
6 - Reboot
After completion of run level it will enter into the User interface will ask you for the credential to login.
1 sylinder = 7.8 MB
3. Vim/vi
1. Gedit
2. Gvim
3. Sublime (have to install separately)
Vim is a highly configurable text editor built to enable efficient text editing. It is an improved
version of the VI editor distributed with most UNIX systems. It is a tool, the use of which you must be
learned. Program is written by Bram Moolenaar et al.
1. Command mode
2. Insert Mode
3. Ex-mode
Command mode: this mode is the default mode following things we can do
Step 1 type vim filename (edit the existing file named filename)
Step 2 move around the file using h/j/k/l key or any appropriate command
h Moves the cursor one character to the left
Step 4 hit Esc key (exit from insert mode if you insert or replace text)
o Open up a new line following the current line and add text there
O Open up a new line in front of the current line and add text there
^B Backward screenful
^U Up half screenful
Exit Commands
: 10,25 w temp
write lines 10 through 25 into file named temp. Of course, other line
numbers can be used. (Use :f to find out the line numbers you want.
x Delete character
dd Delete line
U Restore line
To create a page break, while in the insert mode, press the CTRL key
And l. ^L will appear in your text and will cause the printer to start
A new page.
pat2
Example :g/tIO/s//Ada.Text_IO/g
This will find and replace tIO by Ada.text_IO everywhere in the file.
User management
Type of Users:
Root user - Default user highly privileged UID is 0. This will create while installing the
operating system
System users - is nothing but services, at the time of installing particular package. UID starts
from 1 to 999.
Local users - after installing of the operating system admin user will create these users. UID
starts from 1000 to 60000.
After creating a user, user home directory will be created in default path /home.
One group is will be created with same user name (primary group)
Files from /etc/skel will be copied automatically to user home directory
/etc/passwd file is updated with user information
/etc/group file is update with primary group information
Important files
# useradd –d <home directory> <user name> - create a user with specified home path
# chage –l <user name> - to check user password expiry and account expiry information
Each line in /etc/passwd file represents an individual user account and contains following seven fields separated
by colons (:).
-e = Using this option we can make the account expiry in specific period.
-L = To lock the user account. This will lock the password so we can’t use the account.
-m = moving the contents of the home directory from existing home dir to new dir.
-U = To unlock the user accounts. This will remove the password lock and allow us to use the user
account.
Creating Groups:
# gpasswd –a nischal Administrators – Adds the user nischal to the group Administrators
# gpasswd –A nischal Administrators – give user nischal administrative rights to the group
# gpasswd –d nischal Administrators – remove user nischal from the group Administrators
# groupmod –n <new group name> <old group name> - change group name
# newgrp - <group name> - Login into the group if successful, re-initializes the user environment
Permissions
Note that “r” is for read, “w” is for write, and “x” is for execute.
permission
There are three options for permission groups available to you in Linux. These are
owners: these permissions will only apply to owners and will not affect other
groups.
groups: you can assign a group of users specific permissions, which will
only impact users within the group.
all users: these permissions will apply to all users, and as a result, they
present the greatest security risk and should be assigned with caution
Execute (x): A user or grup with execute permissions can execute a file or
view a directory.
Permissions:
There are two ways to use the commands. They are as follows:
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 38
By Nischal Tech Support
1. Numeric mode
0 --- No permission
1 --x Execute
2 -w- Write
3 -wx Write+Execute
4 r-- Read
5 r-x Read+Execute
6 rw- Read+Write
7 rwx Read+Write+Execute
2. Symbolic mode
In this mode, we can change permissions for all three owners. We can modify the
permissions of a specific owner. With the use of mathematical symbols, we can
modify the file permissions.
Operator Description
- Removes permissions
Write w 2
Execute x 1
Without any change in default umask permissions, all files created by user root will get 644 (666 - 022)
permissions and all directories will get 755 (777-022) permissions.
File Permissions
File 644
Directory 755
permissions
u -
user/owner g
- Group
o -
Others w
Write x -
Execute
+ -
Allow
- - deny
Process Management
Command Description
Example:
ps –af
ps –x - system processes
ps –ax - all
Service Management
service state is the systemd units’ operational status. The most common ones are:
Disabled – disabled services that users can enable directly using the start command.
Masked – stopped services that can only be started by removing the masked property.
Static – services that only run when another program or unit requires them.
Search commands will help you to search files/directories and file content.
The 'grep' command stands for "global regular expression print". grep command filters the content of a
file which makes our search easy.
Command GREP
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple
times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.
Grand regular expression, it is used to search the file name and content of the file
Command FIND
https://www.youtube.com/c/nischaltechsupport (Akshay K Singh)
pg. 45
By Nischal Tech Support
Command LOCATE
Locate command can be used to searching for the file based on the string name from / onwards.