Linux Basics
Linux Basics
2 Creating file with minimal data echo “Hii ! , This is Data” >
<filename2>.<extension>
3 Adding extra data to existing file echo “Hii ! , This is Extra Data” >>
<filename2>.<extension>
10 Locate file (search with keyword i) update db : sudo updatedb && locate -e
without directory) bench-repo
ii) locate <keyword/filename/foldername>
12 Basic VI Editor Keystrokes : To Enter into Input mode / to add data/txt : type I
To Save file : Click ESC , and type wq! Enter (means write
quite)
5 Copy content from old file to new Cat <oldfile > > <newfile>
6 Add multiple files data into one cat <file1> <file2 >... > <newfilename>
1
9 Read with search less <filename> ,next /<enter text to
search>
8 To go backward directory cd ..
9 Create directory within another dir mkdir -p <dir1/dir2/dir3> (inside dir3 dir
will be created)
1 Listing directories ls
2 Long listing ll
Filetype + users Index owner user size date time File name
/
permission links
2
File Types ( first letter ) - : file d : directory l: link
File Permissions r → read w → write e → execute
rw- rw- r– ( - means no permission) first three for owner next : group last : other users
Linux Users
3 To find user id id
5 Switch to another user, you need to know password for user su <username>
2 Create user - w/o password - as normal user with sudo sudo useradd <username>
4 To add password to user as normal user - with - sudo sudo passwd <username>
16 Modify the user home directory with data usermod -d <dir> -m <username>
3
17 Modify the user id and groupid usermod -u <uid> -g <gid>
<username>
19 Modify normal user to root user(sudo group :wheel) usermod -aG wheel <username>
Filetype + users Index owner user size date time File name
/
permission links
All the directories in the Linux system come under the root(/) directory which is represented by a forward
slash (/).
4
Directory type Types of files stored
1 Binary directories Contains binary or compiled source code files, eg, /bin, /sbin, etc.
2 Configuration directories Contains configuration files of the system, eg, /etc, /boot.
3 Data directories Stores data files, eg, /home, /root,/media, /mnt, /tmp etc.
4 Memory directories files which don't take up actual harddisk space, eg, /dev, /proc, /sys.
5 Usr (Unix System Resources) Contains sharable, read only data, eg, /usr/bin, /usr/lib, etc.
6 var (variable directory) Contains larger size data, eg, /var/log, /var/cache, etc.
7 Non-standard directories Dir. which do not come under FHS, eg, lost+found,/run, /cdrom etc.
Linux - VI Editor
VI=VIsual Editor , it’s text editor in UNIX/Linux environment. It has two modes, (i) Command Mode :
based on commands you can edit text , eg: pressing dd → will delete the entire one line. (ii) Insert Mode :
whenever you enter vi (vi <filename> , by default its command mode, by pressing I you can shift into Insert
mode, here you can type , copy, paste text.
4 Move up line k
5 Move left h
6 Move Right l
7 Go to last line G
8 Last Position ``
15 undo u
17 Copy a line yy
5
19 Paste before current line P
26 Search /
Linux - Archiving
File archiving is used when one or more files need to be transmitted or stored as efficiently as possible.
Archiving: Combines multiple files into one, which eliminates the overhead in individual files and makes
the files easier to transmit.
Compression: Makes the files smaller by removing redundant information. Its two types : lossless, lossy
Lossless: No info is removed from the file. Compressing a file and decompressing= identical to the
original.
Lossy : Info might be removed from the file. uncompressing a file = slightly different from the
original.
When an archive is decompressed, and one or more files are extracted, this is called un-archiving.
1 zip Archive=combines files and keeps original files as it is
2 gzip Compress files along archive(only files) by replacing original files Lempel-Ziv data
compression
3 bzip2 Compress files+archive by replacing files, gen2 of gz(good compr.) Burrows-Wheeler block
sorting
zip: files+folders - combining - no compress
1 Zip file/folder zip <zip file name> <folder/file needs to zip> zip one.zip /home/user/dir
3 Remove file from zip Zip -d <zipfile.zip> <files/folders to zip> zip -d hii.zip hii2
4 Add file zipped file zip -u <zipfile.zip> <files/folders to zip> zip -u hii.zip hii2-add
5 Delete original after zip zip -m <zipfile.zip> <files/folders to zip> Zip -m one.zip
/home/user/one/
8 Verbose (info as response) Zip -v <zipfile.zip> <files/folders to zip> zip -v one.zip one.txt
gzip (GNU (GNU’S Not Unix) zip) : files - compression at low size
6
1 compress file gzip gzip one.zip
bzip2 (bunzip) : compress a file like gzip command but takes a little more time but compresses better.
1 compress file bzip2 <files> bzip all.zip
Tar(Tap Archive): combine+compress → files and folder → again you can add .gz and bz2 compression
c : create v : display output f : mentioned destination dir. to store z : compress with gz j : compress with bzip2
3 compress file/folder bzip2 tar -cvfj <filename.tar> <files/folders> tar -cvfj all.tar *
7
8 dig Query DNS related information.
Linux - man
The "man" is a short term for a manual page. In unix-like operating systems such as linux, man is an
interface to view the system's reference manual.
1 man <command> Eg: man passwd → to view manual related to man command
8
15 jobs To see running jobs , ex : starting sleep job : sleep 1000 & , & id run in
bg
Linux - processes
To view currently running processes on the system with the ps command. It mostly return PID(Process ID),
TTY(type of terminal ), TIME(process cpu use time),CMD(command used)
1 ps -ef List currently running process in full format
2 killall It kills all the processes with the specified name in the system.
3 pkill The pkill command uses the name of the process either by typing full name or partial
name.
4 xkill Command xkill is used to kill a process on X server without passing process name or
PID
9
2 kill -SIGNAL PID PID Kill -9 1002 1003 Command to kill multi process
Regular/Real time we mostly check real time activities and memory usage using top command,
top - 03:43:03 up 1 day, 2:00, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 94 total, 1 running, 56 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si,
0.3 st
KiB Mem : 1005836 total, 701520 free, 94628 used, 209688 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 767308 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
.
1 root 20 0 123484 5392 3852 S 0.0 0.5 0:03.75 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00
kworker/0:0H
5 root 20 0 0 0 0 I 0.0 0.0 0:00.39
kworker/u30:0
Linux - filters
Linux Filter commands accept input data from stdin (standard input) and produce output on stdout (standard
output). It transforms plain-text data into a meaningful way and can be used with pipes to perform higher
operations.
1 grep
global regular expression print → filters the content of a file which makes our search
easy.
E.g : cat /etc/passwd | grep root or ls -ltr | grep videos
2 sort sort <fileName> , eg : sort /etc/passwd
10
Linux - Distributions
Linux Developed by many developers , associations and companies , all of them shared and developed parts
of linux. And linux open source modifying source companies releasing their distribution.
1) RPM-based : Fedora, RedHat Linux Enterprise, CentOS, OpenSUSE, package: rpm → yum
2) Debian-based : Ubuntu, Kali linux, parrot os , package : dpkg → apt-get or apt
3) Arch Linux-based : Arch linux, Garuda Linux , package: pacman → pacman
Linux - History
In 1969, a team of developers of Bell Labs started a project to make a common software for all the
computers and named it 'Unix'. It was simple and elegant, used 'C' language instead of assembly language
and its code was recyclable.. As it was recyclable, a part of its code now commonly called 'kernel' was used
to develop the os.
In 1991, Linus Torvalds was a student at the university of Helsinki, Finland. Free version of Unix started
writing its own code. Later this project became the Linux kernel.released as GNU/Linux in 1992. It is an
open source so many companies made their modifications and released their distributions. You can see
distributions above.
Linux - package/software installation
11
3 Arch based : pacman -S <package-name>
OS : Arch linux, Garuda Linux
Linux - cronjob
To Build Job Daily or Time basic you can user cron job
Cron Jobs has 5 values :
Minutes Hour Day of Month Month Dow
Task - 1: If you want to run a job every day at 10:00PM, then write cron job
Answer : 0 22 * * *
Minutes Hour Day of Month Month Dow
0 22 * * *
Task - 2 : If you want to run a job every day at 11:00 PM From Monday to Friday only,
then write cron job
Answer : 0 23 * * 1-5
Minutes Hour Day of Month Month Dow
0 23 * * 1-5
30 08 10 06 * /home/maverick/full-backup
12
1. Elements of linux /system architecture : Kernel, system library,system utility, hardware, shell and user
2. kernel: it establishes communication between user application and hardware. It stays in b/w app and
hardware. Main Functions are Memory, Storage, Device, Process management. Kernel types:
Kernel Monolithic Micro Hybrid
work services operate in kernel space Less services@kernel, left in users Combo of monolithic and micro
3. Shell : it is a terminal environment ,here we can run our commands, it is an interface between user and
kernel.
Types : 1) bourne shell (sh), 2) bash(Bourne-Again Shell) - bash, 3) c shell(csh), 4) korn shell(ksh), 5) z
shell(zsh)
4.shell scripting : it is like programing language, here we write shell script(commands, conditions and
more) in file and execute, it will perform actions and process , eg: #1/bin/bash…….yum install
httpd
4.LILO (Linux Loader) : used to load Linux into memory and start the OS, simply its a boot loader.
functions such as locating kernel, identifying other supporting programs, loading memory, and starting the
kernel
5.Modes of vi editor : i) Command mode (press ESC), (ii) Insert mode(press I), (iii) Execute mode(press :)
6.Swap memory : Sharing the storage memory to RAM, when the RAM is used full, to handle
resources/processes
7. Daemons : background linux system process , that starts when os starts and ends at shutdown , no control
over it. Examples : httpd, sshd, nfsd
8. Linux user mode : (i). CLI(Command Line Interface), (ii) GUI(Graphical User Interface)
9. File permissions in linux : (i) r - Read , (ii) w - Write , (iii). e - Execution
10. Cron : it is program used to execute tasks at scheduled time, min time is minutes, used in servers , runs
24-7
11. Anacron : execute tasks in intervals/days – mintime days – not a demon, used in Desktops and laptops, eg:
updates
12. Process ID :
13