Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Sunday, August 28, 2022

Create bootable USB for Ubuntu from Windows using balenaEtcher



Linux Operating system is one of the best choice for Laptop and Personal Computers, Linux is Open Source, in most cases you need not to worry about licenses and heavy charges, which are needed to pay for paid operating systems, you can download Linux for Free, you can choose from various Linux downloads, if you want a free Linux Tutorial ( Linux Commands ) you can click here

Linux Tutorial

Ubuntu is one of best Linux operating system among various Linux distributions, Due to its immense popularity among masses, you are more likely to get support, although i do believe each Linux distribution is unique and special , you if want to know about popular Linux distros, you can click here,

Linux distributions

In this post i will explain how to Create bootable USB Ubuntu from Windows for free,

before we move ahead, i want to answer some commonly asked questions about Ubuntu and Linux :

Q: What is Linux is used for?
A: Linux is not a single operating system, but it is family of various Unix like operating systems, Linux is dominant in super computer and server market, it can be used as web server, mail server etc, it can also be used on personal computers, where is getting popular now a days.

Q: Is Ubuntu better than Windows?
A: Both Windows have and Linux ( Ubuntu ) have their own USP, Both are good in their own way, however Ubuntu is largely free and open source, which makes it affordable for everyone.

Q: Is Ubuntu 20.04 stable now?
A: Yes it is stable.

Q: Is Ubuntu 20.04 better?
A : Yes As per my opinion its better than previous Ubuntu versions like Ubuntu 16.04 or Ubuntu 18.04

Q: What is the best Linux operating system? Or What is the best Linux to run from USB?
A: Again as i said earlier, each Linux distribution is special and have a specific purpose, some like Ubuntu are more popular than others, it purely depends upon your requirements, for example Kali Linux is very popular in the area of Cyber Security

You can Read Our Cyber Security tips here :

Cyber Security tips

Q: Can Ubuntu be run from a USB stick? Or Can I run an operating system from a flash drive? Or Can you boot Linux from a flash drive?
A :Yes, its possible.

Q: How to use USB stick Ubuntu?
A: Please read our complete post.

Q: Can I try Ubuntu without installing?
A: Yes its possible and we recommend you to do it before completely switching to Linux / Ubuntu.

Q: What size USB do you need for Ubuntu? Or Is 8GB USB enough for Ubuntu?
A: We suggest that you should have minimum 8GB USB, though it possible to use a 4GB USB too.

So lets begin , first you need to download ubuntu iso image, As of now some of official links for downloading it are as below :

ubuntu for desktop

ubuntu alternative-downloads

for desktops Latest Version is Ubuntu 22.04, you can download past releases as well as from alternate download links, i have chosen Ubuntu 20.04 for this tutorial, if you want you can download, try and also optionally install Ubuntu 22.04 LTS as well, before you proceed you need to make sure that USB boot is priority over Hard disk and other kind of boot in your laptop or PC BIOS options

One you download iso image, you will also need to download Some software which will write image file ( iso file ) to storage media ( USB or flash drive ), in this tutorial, i will use free and open-source "balenaEtcher" , its a exe file no installation required, there are several other alternatives for it such as "Rufus" and "Ventoy". you can download balenaEtcher from following link :





Alright since we have now both Ubuntu 20.04 iso image and utility to create Live USB system, we need to follow next two steps :

1. Select Ubuntu 20.04 ( or Ubuntu 22.04 LTS, which ever ubuntu version you have downloaded ) image ( iso ) file in balenaEtcher`s dialogue box .



2. After this you need to specify storage name, where you will be creating Live USB system ( your USB flash drive name ) in balenaEtcher`s dialogue box.



you are now ready to create (flash) Live USB system, normally it will take few minutes, once it is completed there is also a optional validation option, i would recommend, go for it,



your bootable USB for Ubuntu is now ready,
all you need to restart you laptop or PC while keeping USB flash drive plugged-in
You will get following screen while you will reboot your laptop or PC,
you need to press enter ( Choose default option ), if it does not proceed automatically in few seconds.




Once you have booted from flash drive, you will get two options on screen, one of them is "Try Ubuntu" and Other one "Install Ubuntu" , Since we want to use USB flash drive to play with Ubuntu first, please select Try Ubuntu, it will take you inside Ubuntu OS.




Congratulation you have completed all steps, you are now ready to use Ubuntu form live USB flash drive, you even get a shortcut on screen to install Ubuntu 20.04 directly to your laptop, We suggest you to try Ubuntu for USB stick for sometime and get used to it before installing, Thanks for Reading.

Disclaimer : this content is written as general information, and does not claim to be complete or accurate, readers should consult their respective Personal Computer/Server/laptop/Flash Drive/Operating system vendors; before performing any of these steps, we are not responsible for any damage caused; due to above information.

return to Whats Cool page

Go to Tech World Update page

Go to green world Page

return to Home Page

Sunday, July 24, 2022

Basics of Permissions on Unix and Linux Systems and chmod Command | Part -1

Permissions play an important role in unix / linux ecosystem, they are integral part of data security, every file and directory must have clearly defined ownership, As a beginner you should undertand that each file and directory have three types of permissions

A) Read
B) Write
C) Execute

There are some advanced concepts as well such as sticky bit,suid,sgid and umask which are meant for advanced users, we will discuss these in our future posts.

These permissions are represented in following two types of notations :

1) Symbolic notation
a) read : r
b) write : w
c) execute : x

2) Numeric notation
a) read : 4
b) write : 2
c) execute : 1

lets understand these concepts with examples:
i have created two directories Permissionsdemo1 and Permissionsdemo2 inside home path of user "appuser1" with same user, this user is part of group "app", defaults permissions of these directories are "rwxr-xr-x" ( rwx -> owner | r-x -> group | r-x others ), which means owner of these directories have all three read(4), write(2) and execute(1) permissions, the group ( or group members) to which owner belongs to have read(4) and execute (1) permissions, other too have read(4) and execute (1) permissions













Next i have created a file inside Permissionsdemo1 directory with "appuser1" user, with name "Script1.sh", its a executable file ( Script ), default permission of this file is "-rw-r--r--" ( -rw -> owner | r-- -> group | r-- others ), which means owner of this file have read(4) and write(2) permissions,the group ( or group members) to which owner belongs have read(4) permissions and other too have read(4) permissions















Here comes the interesting part, Unix ( or Linux ) Provides you way to change the default permissions, You can do this with the help of "chmod" command. Again this commands as two forms -

A) Symbolic
B) Numerical

In this post i will be focusing on Numerical form , will explain Symbolic form in next part, i personally find Numerical form easier to learn and practice , Please pay close attention to following example where i have provided permsions "640" to file "Script1.sh" . This means owner of this file have this file have read(4) and write(2) permissions,the group ( or group members) to which owner belongs to have read(4) permissions and other Dont have any permissions (0). Notice that only owner of the file or superuser ( example : root ) can run this command . After this change , one other user "neeraj" ( which is not part of group "app" ) is not able to read the file. howver appuser2 which is part of "app" group is still able to read the file, since group has read permissions,















However we notice that neither "appuser1" ( owner ) or "appuser2" ( owners group member ) are are able to execute this file













To Solve this problem, we will again use "chmod" command.
i provided permsions "750" to file "Script1.sh" . This means owner of this file have this file have read(4) and write(2) and execute(1) permissions,the group ( or group members) to which owner belongs have read(4) permissions and execute(1) and others Dont have any permissions (0).










In next expample i have tried to write one line to "Script1.sh" using "appuser2" which is not owner of file but is part of owners group, since the group does not have write permissions, my attempt failed and i got error "Permission denied". group currently has 5 ( Read : 4 + execute : 1 ) Permissions.









To Provide write permissions to group , i have used "chmod 770" , this will provide full permissions ( Read : 4 ,Write : 2 and Execute : 1 ) to owner as well as group but no (0) permission to others, after this i am able to write to file "Script1.sh", i am also able to execute the modified file with "appuser2"











Since now "appuser2" has "full" ( really ? ) permissions to file "Script1.sh" ( As it is part of "app" group and group has read,write and execute permissions). Should it be able to delete ( remove ) the file "Script1.sh" ? The answer is "not yet" and reason is little complex
you can see in following example "appuser2" is not able to delete ( remove ) the file "Script1.sh"







The reason is while deleting a file (Script1.sh) "appuser2" ( owners group user ), also trying to modifiy the directory "PermissionDemo1" , however the group does not have permission to modify the directory. with the help of chmod command we have now modified persmissions of "PermissionDemo1" directory, We have used chmod "771" , this gives read ( 4 ) , write ( 2 ) and execute ( 1 ) permissions to both owner and group and only execute (1) permisions to others, note that this is for directory. after that "appuser2" is able to delete the file.











By now you are aware of basic concepts related to linux persmissions and also about "chmod" command, in next post i will be covering some advacned copncepts




if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com



Get latest Tech world updates

Monday, July 18, 2022

Most Popular Linux distribution as of 2022

Here is list of most poular linux distribution as of 2022 , this list is not in any particualr order, This list include list of distributions aimed for both desktop and servers,



1. Debian Linux

2. Fedora Linux

3. Ubuntu / Ubuntu Server ( derived from Debian )

4. Red Hat Enterprise Linux ( RHEL ) ( Derived from Fedora Linux )

5. openSUSE/SUSE Linux Enterprise



Some honourable mentions

1. MX Linux

2. Slackware

3. Arch Linux

4. CentOS

5. Linux Mint

6. Manjaro Linux

7. oracle linux



Note - this list is based on our own opinion and research and we do not claim that it is accurate and complete
if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com

Sunday, June 12, 2022

ls command with examples

ls command with examples

ls command with examples



if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com

ls command is one of the most widely used command on unix,linux platform system, as name suggests ls is used for listing files and directories, but apart from listing it gives a lot of information, lets explore this command.





1. use 'ls' without anything ls you will get a list of files and directories


neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro.txt Python.txt

2. Using 'ls' with variety of wildcards makes it very useful, in below example we have listed
a) files/directories names starting with 'J' ( J followed by * )
b) files/directories names ending with 'txt' ( * followed by J )
c) files/directories names ending with 'txt' and 'o' coming anywhere before that ( *o*txt )


neeraj@ubuntu:~/unixtechworld_Workshop$ ls J*
Java.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ ls *txt
Java.txt Linux_Distro.txt Python.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ ls *o*txt
Linux_Distro.txt Python.txt
neeraj@ubuntu:~/unixtechworld_Workshop$

3. ls has a very useful option ( or argument ) called "-l" , l is for long listing, look carefully at below example, each column of output gives you following information
(I) permssions and file type ( plain file / directory etc )
(II) number of hard links
(III) owner
(IV) group
(V) file size
(VI) , (VII) and (VIII) Date, timeamp of file
(IX) filename


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -l
total 16
drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

4. You can '-t' with '-l' to sort file list with time stamp ( modification time )


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -lt
total 16
drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

5. Adding '-r' along with '-l' can help you sort files in reverse order ( old files first )


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -ltr
total 16
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs

6. using '-h' option along with '-l' will give you file size in human readable format ( notice size 4.0 k )


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -lh
total 20K
drwxrwxr-x 2 neeraj neeraj 4.0K May 29 06:08 Hello_World_Programs
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 29 May 29 05:37 MyFirstFile.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

7. using '-d' can help you list directories

neeraj@ubuntu:~/unixtechworld_Workshop$ ls -d Hello_World_Programs
Hello_World_Programs

8. using '-R' can help you list directories recursively


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -R Hello_World_Programs
Hello_World_Programs:
JavaScript.txt Kotlin.txt

9. you can use '-a' ( all )option to list hidden files, notice '.HideMe' file


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -la
total 28
drwxrwxr-x 4 neeraj neeraj 4096 May 27 12:31 .
drwxr-xr-x 16 neeraj neeraj 4096 Mar 6 12:05 ..
drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs
drwxrwxr-x 2 neeraj neeraj 4096 May 27 12:31 .HideMe
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

10. you can use '-A' ( almost all )option to list hidden files except current and parent directory ( . and .. )


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -lA
total 20 drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs
drwxrwxr-x 2 neeraj neeraj 4096 May 27 12:31 .HideMe
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

11. you can use '-S' option to sort output based on file size

neeraj@ubuntu:~/unixtechworld_Workshop$ ls -lS
total 16
drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

12. You can use '-f' option to print file list without any sorting

neeraj@ubuntu:~/unixtechworld_Workshop$ ls -f
. Linux_Distro.txt .. .HideMe Hello_World_Programs Java.txt Python.txt

13. here is an example of ls with wild card mask, we have lsited any filename which is 8 character long

neeraj@ubuntu:~/unixtechworld_Workshop$ ls ????????
Java.txt

14. In below example we have listed any filename which starts by any character followed by any one of three characters 'x','y','z' and after that any number of characters

neeraj@ubuntu:~/unixtechworld_Workshop$ ls ?[x-z]*
Python.txt

15. you can also list the content of current directory by using 'ls .' , since . ( single dot represent current directory )

neeraj@ubuntu:~/unixtechworld_Workshop$ ls .
Hello_World_Programs Java.txt Linux_Distro.txt Python.txt

16. you can list the content of parent directory by using 'ls ..' , since .. ( double dot represent current directory )

neeraj@ubuntu:~/unixtechworld_Workshop$ ls ..
Desktop Documents Downloads Music Pictures Public Templates unixtechworld_Workshop Videos WorkShop

17. you can list the content of child directory by using 'ls */*', this will include all child directories which are in same hierarchy

neeraj@ubuntu:~/unixtechworld_Workshop$ ls */*
Hello_World_Programs/JavaScript.txt Hello_World_Programs/Kotlin.txt

18. if you want to list conent of specific child directory ( for example 'Hello_World_Programs' ) you can do it like below

neeraj@ubuntu:~/unixtechworld_Workshop$ ls Hello_World_Programs/*
Hello_World_Programs/JavaScript.txt Hello_World_Programs/Kotlin.txt





if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com

Sunday, June 5, 2022

awk with examples

awk with examples

awk with examples

'awk' is one of the most versatile command or utility of unix OS, infact its a full-blown domain specific langauge in itself, it has powerfull text processing capabilities, here are some examples which shows how it can be used for text processing


We will be using below Sample File for our examples



neeraj@ubuntu:~/WorkShop$ cat Country.txt
COUNTRY,CAPITAL,AREA IN SQ KM
India,Delhi,3287263
Japan,Tokoyo,377975
USA,Washington DC,9833520
UK,London,242495
Australia,Canberra,7692024
China,Beijing,9596961
Ukraine,Kyiv,603628
Russia,Moscow,17098246
Brazil,Brasília,8515767
South Africa,Cape Town,1221037
France,Paris,643801
Taiwan,Taipei,36197
Canada,Ottawa,9984670


1. Printing Entire content (all columns ) of a comma(,) separated file, within awk "$0" represents all columns


neeraj@ubuntu:~/WorkShop$ awk -F"," '{print $0}' Country.txt
COUNTRY,CAPITAL,AREA IN SQ KM
India,Delhi,3287263
Japan,Tokoyo,377975
USA,Washington DC,9833520
UK,London,242495
Australia,Canberra,7692024
China,Beijing,9596961
Ukraine,Kyiv,603628
Russia,Moscow,17098246
Brazil,Brasília,8515767
South Africa,Cape Town,1221037
France,Paris,643801
Taiwan,Taipei,36197
Canada,Ottawa,9984670


2. Printing specific column of a comma(,) separated file, within awk "$n" represents column n, for example here we have printed 2nd column, Please note the anything can be a input separator ( even space ), we have to give it under Double Quotes after "-F"


neeraj@ubuntu:~/WorkShop$ awk -F"," '{print $2}' Country.txt
CAPITAL
Delhi
Tokoyo
Washington DC
London
Canberra
Beijing
Kyiv
Moscow
Brasília
Cape Town
Paris
Taipei
Ottawa


3. You can use "tolower" inbuilt function to print entire content or a specific column in lower Characters


neeraj@ubuntu:~/WorkShop$ awk -F"," '{print tolower($2)}' Country.txt
capital
delhi
tokoyo
washington dc
london
canberra
beijing
kyiv
moscow
brasília
cape town
paris
taipei
ottawa


4. Similarly we have "toupper" function as well, which can print entire content or a specific column in upper Characters


neeraj@ubuntu:~/WorkShop$ awk -F"," '{print toupper($2)}' Country.txt
CAPITAL
DELHI
TOKOYO
WASHINGTON DC
LONDON
CANBERRA
BEIJING
KYIV
MOSCOW
BRASíLIA
CAPE TOWN
PARIS
TAIPEI
OTTAWA


5. here is how you can use "$0" and "toupper" together to print entire conent in upper case


neeraj@ubuntu:~/WorkShop$ awk -F"," '{print toupper($0)}' Country.txt
COUNTRY,CAPITAL,AREA IN SQ KM
INDIA,DELHI,3287263
JAPAN,TOKOYO,377975
USA,WASHINGTON DC,9833520
UK,LONDON,242495
AUSTRALIA,CANBERRA,7692024
CHINA,BEIJING,9596961
UKRAINE,KYIV,603628
RUSSIA,MOSCOW,17098246
BRAZIL,BRASíLIA,8515767
SOUTH AFRICA,CAPE TOWN,1221037
FRANCE,PARIS,643801
TAIWAN,TAIPEI,36197
CANADA,OTTAWA,9984670


6. You can use "NR" built in variable to print line numbers ( row numbers )

neeraj@ubuntu:~/WorkShop$ awk -F"," '{print NR,$0}' Country.txt
1 COUNTRY,CAPITAL,AREA IN SQ KM
2 India,Delhi,3287263
3 Japan,Tokoyo,377975
4 USA,Washington DC,9833520
5 UK,London,242495
6 Australia,Canberra,7692024
7 China,Beijing,9596961
8 Ukraine,Kyiv,603628
9 Russia,Moscow,17098246
10 Brazil,Brasília,8515767
11 South Africa,Cape Town,1221037
12 France,Paris,643801
13 Taiwan,Taipei,36197
14 Canada,Ottawa,9984670


7. You can use "NF" built in variable to print "Field numbers" ( or the number of coulmns ), which are present in each line (based on field separator )

neeraj@ubuntu:~/WorkShop$ awk -F"," '{print NF}' Country.txt
3
3
3
3
3
3
3
3
3
3
3
3
3
3


8. "NF" can also be used to specify a column, for example here we have printed last column with help of NF ( Since total number of filed or coloumn are three in this file, NF is equal to 3, means we printed 3rd column)

neeraj@ubuntu:~/WorkShop$ awk -F"," '{print $NF}' Country.txt
AREA IN SQ KM
3287263
377975
9833520
242495
7692024
9596961
603628
17098246
8515767
1221037
643801
36197
9984670


9. if you want to print line having specific value, you can do it using following way, this example shows how you can print the line having 3rd column value greater than "3287263"

neeraj@ubuntu:~/WorkShop$ awk -F"," '$3>3287263{print $0}' Country.txt
COUNTRY,CAPITAL,AREA IN SQ KM
USA,Washington DC,9833520
Australia,Canberra,7692024
China,Beijing,9596961
Russia,Moscow,17098246
Brazil,Brasília,8515767
Canada,Ottawa,9984670


10. This example shows how to print rows based on equality condition, Note "==" operator is used

neeraj@ubuntu:~/WorkShop$ awk -F"," '$3==3287263{print $0}' Country.txt
India,Delhi,3287263


11. Again Variable "$NF" hold value at NF ( 3 Column ) position

neeraj@ubuntu:~/WorkShop$ awk -F"," '$NF>3287263{print $0}' Country.txt
COUNTRY,CAPITAL,AREA IN SQ KM
USA,Washington DC,9833520
Australia,Canberra,7692024
China,Beijing,9596961
Russia,Moscow,17098246
Brazil,Brasília,8515767
Canada,Ottawa,9984670


12. One more example with "NF" used with equality operator for comparision of value "$NF" position

neeraj@ubuntu:~/WorkShop$ awk -F"," '$NF==3287263{print $0}' Country.txt
India,Delhi,3287263


13. awk By default have " " ( space ) as its input field separator, Notice that in this example we did not specifiy input field separator hence " " space is input field sperator, hence from the line "South Africa,Cape Town,1221037" it has only printed string "South"

neeraj@ubuntu:~/WorkShop$ awk '{print $1}' Country.txt
COUNTRY,CAPITAL,AREA
India,Delhi,3287263
Japan,Tokoyo,377975
USA,Washington
UK,London,242495
Australia,Canberra,7692024
China,Beijing,9596961
Ukraine,Kyiv,603628
Russia,Moscow,17098246
Brazil,Brasília,8515767
South
France,Paris,643801
Taiwan,Taipei,36197
Canada,Ottawa,9984670


14. By using "+=$N" you can calculate sum of a specific column, Please refer below example , it also make uses of "BEGIN" ,"END" and "FS" keywords , this example starts with "BEGIN" where field separator has been specified, "FS" specify "Field seperator" jist like "-F",after that sum is calculated by "+=$N " at the "END" block prints the Sum

neeraj@ubuntu:~/WorkShop$ awk 'BEGIN{FS=","}; {sum+=$3} {print $0} END{print sum;}' Country.txt
COUNTRY,CAPITAL,AREA IN SQ KM
India,Delhi,3287263
Japan,Tokoyo,377975
USA,Washington DC,9833520
UK,London,242495
Australia,Canberra,7692024
China,Beijing,9596961
Ukraine,Kyiv,603628
Russia,Moscow,17098246
Brazil,Brasília,8515767
South Africa,Cape Town,1221037
France,Paris,643801
Taiwan,Taipei,36197
Canada,Ottawa,9984670
69133584


15. You can print substring with built in function "substr", you need to provide column number , start position within column number and length

neeraj@ubuntu:~/WorkShop$ awk -F"," '{print $1,substr($1,1,2)}' Country.txt
COUNTRY CO
India In
Japan Ja
USA US
UK UK
Australia Au
China Ch
Ukraine Uk
Russia Ru
Brazil Br
South Africa So
France Fr
Taiwan Ta
Canada Ca


16. You can print two functions in combination too, here we have used "substr" result as a input of "toupper" function

neeraj@ubuntu:~/WorkShop$ awk -F"," '{print $1,toupper(substr($1,1,2))}' Country.txt
COUNTRY CO
India IN
Japan JA
USA US
UK UK
Australia AU
China CH
Ukraine UK
Russia RU
Brazil BR
South Africa SO
France FR
Taiwan TA
Canada CA


17. you can use external or SHELL variable inside awk by using "-v" option, this comes handy when you need to compare any column with externally defined value at runtime, in this example we have printed the row where Second Column String is "Delhi".

neeraj@ubuntu:~/WorkShop$ MyVar=Delhi
neeraj@ubuntu:~/WorkShop$ awk -F"," -v x=$MyVar '$2 == x {print $0}' Country.txt
India,Delhi,3287263
neeraj@ubuntu:~/WorkShop$ MyVar=Paris
neeraj@ubuntu:~/WorkShop$ awk -F"," -v x=$MyVar '$2 == x {print $0}' Country.txt
France,Paris,643801


18. You can use "awk" as grep, in this example we have searched for string "London"

neeraj@ubuntu:~/WorkShop$ awk '/London/' Country.txt
UK,London,242495


19. You can even use wild card mask while searching lines. in this example "." ( dot ) represent a single character, we have searched for six character string which surrounded by "," ( at both start and End )

neeraj@ubuntu:~/WorkShop$ awk '/,......,/' Country.txt
Japan,Tokoyo,377975
UK,London,242495
Russia,Moscow,17098246
Taiwan,Taipei,36197
Canada,Ottawa,9984670


20. Here is another example of using wild card within "awk" and using it like "grep", this example prints lines starting with "U"

neeraj@ubuntu:~/WorkShop$ awk '/^U/' Country.txt
USA,Washington DC,9833520
UK,London,242495
Ukraine,Kyiv,603628


21. Following example Searches the pattern starting with "U" followed by any single character "." and then comma (,)

neeraj@ubuntu:~/WorkShop$ awk '/U.,/' Country.txt
UK,London,242495


22. "awk" will print the output in space separated format, if you use comma between fields

neeraj@ubuntu:~/WorkShop$ awk -F"," '{print $1,$2,$3}' Country.txt
COUNTRY CAPITAL AREA IN SQ KM
India Delhi 3287263
Japan Tokoyo 377975
USA Washington DC 9833520
UK London 242495
Australia Canberra 7692024
China Beijing 9596961
Ukraine Kyiv 603628
Russia Moscow 17098246
Brazil Brasília 8515767
South Africa Cape Town 1221037
France Paris 643801
Taiwan Taipei 36197
Canada Ottawa 9984670


23. "awk" will print the output without any separator, if you dont use comma between fields.


neeraj@ubuntu:~/WorkShop$ awk -F"," '{print $1 $2 $3}' Country.txt
COUNTRYCAPITALAREA IN SQ KM
IndiaDelhi3287263
JapanTokoyo377975
USAWashington DC9833520
UKLondon242495
AustraliaCanberra7692024
ChinaBeijing9596961
UkraineKyiv603628
RussiaMoscow17098246
BrazilBrasília8515767
South AfricaCape Town1221037
FranceParis643801
TaiwanTaipei36197
CanadaOttawa9984670


24. "awk" has built in variable "OFS" For separating output fields, it can be used with "BEGIN" block, here is the example, output is separated by , (comma )


neeraj@ubuntu:~/WorkShop$ echo "A B C" | awk 'BEGIN{OFS=","} {print $1,$2,$3}'
A,B,C


25. "OFS" can also be used with -v option, refer below example


neeraj@ubuntu:~/WorkShop$ echo "A B C" | awk -v OFS="," '{print $1,$2,$3}'
A,B,C


26. if you wish to use "OFS" along with "$0" , you need reset the field value using "$1=$1", here is the example

neeraj@ubuntu:~/WorkShop$ echo "A B C" | awk -v OFS="," '{$1=$1;print $0}'
A,B,C


27. "OFS" will not work here as we are not resetting field value

neeraj@ubuntu:~/WorkShop$ echo "A B C" | awk 'BEGIN{OFS=","} {print $0}'
A B C


28. lets apply "OFS" to seperate output fields by "|" ( pipe ) symbol, for our test file "Country.txt".

neeraj@ubuntu:~/WorkShop$ awk 'BEGIN{FS=","} { $1=$1; OFS="|"} {print $0}' Country.txt
COUNTRY CAPITAL AREA IN SQ KM
India|Delhi|3287263
Japan|Tokoyo|377975
USA|Washington DC|9833520
UK|London|242495
Australia|Canberra|7692024
China|Beijing|9596961
Ukraine|Kyiv|603628
Russia|Moscow|17098246
Brazil|Brasília|8515767
South Africa|Cape Town|1221037
France|Paris|643801
Taiwan|Taipei|36197
Canada|Ottawa|9984670


29. "awk" also has inbuilt variable called "FILENAME" , which can be used to print input filename

neeraj@ubuntu:~/WorkShop$ awk -F"," '{print FILENAME,$1}' Country.txt
Country.txt COUNTRY
Country.txt India
Country.txt Japan
Country.txt USA
Country.txt UK
Country.txt Australia
Country.txt China
Country.txt Ukraine
Country.txt Russia
Country.txt Brazil
Country.txt South Africa
Country.txt France
Country.txt Taiwan
Country.txt Canada


30. You can also use "awk" to print your desired string and separator between fields, as below example

neeraj@ubuntu:~/WorkShop$ awk -F"," '{print "Unix is great os,"$1","$2","$3}' Country.txt
Unix is great os,COUNTRY,CAPITAL,AREA IN SQ KM
Unix is great os,India,Delhi,3287263
Unix is great os,Japan,Tokoyo,377975
Unix is great os,USA,Washington DC,9833520
Unix is great os,UK,London,242495
Unix is great os,Australia,Canberra,7692024
Unix is great os,China,Beijing,9596961
Unix is great os,Ukraine,Kyiv,603628
Unix is great os,Russia,Moscow,17098246
Unix is great os,Brazil,Brasília,8515767
Unix is great os,South Africa,Cape Town,1221037
Unix is great os,France,Paris,643801
Unix is great os,Taiwan,Taipei,36197
Unix is great os,Canada,Ottawa,9984670



if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com

Sunday, May 29, 2022

Basic Unix Commands in 1 Hour

Basic Unix Commands in 1 Hour

Basic Unix Commands in 1 Hour

Hello Friends if you want to learn basic unix/linux commands in just 1 Hour, this Course is for you, this Course is specifically designed for someone who is new to unix. Please follow unixtechworld.blogspot.com for learning more


1. To login to Unix or linux system via command line, usually you enter "username" and "password", you may be greeted by some login message and information and you will get your command prompt

login as: neeraj
neeraj@192.168.137.129's password:
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.13.0-44-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
124 updates can be applied immediately.
To see these additional updates run: apt list --upgradable
Your Hardware Enablement Stack (HWE) is supported until April 2025.
Last login: Sun May 29 04:57:53 2022 from 192.168.137.1
neeraj@ubuntu:~$

2. Checking the Current Directory Or Present Working Directory : As soon soon as you login, you land at your default home path, you can check your Current Directory Or Present Working Directory using "pwd" command

neeraj@ubuntu:~$ pwd
/home/neeraj

3. Using ls to list the files and directories : you can list the files and directories at your current Path with help of "ls" command, this command has several useful options or auguments, however for now only "ls" is sufficient for you

neeraj@ubuntu:~$ ls
Desktop Documents Downloads Music Pictures Public Templates unixtechworld_Workshop Videos WorkShop

4. You can you the "date" Command to see current date and time

neeraj@ubuntu:~$ date
Fri 27 May 2022 11:44:28 AM PDT

5. you can use the "cd" command to change the current directory

neeraj@ubuntu:~$ cd unixtechworld_Workshop
neeraj@ubuntu:~/unixtechworld_Workshop$ pwd
/home/neeraj/unixtechworld_Workshop

6. You Can make Copy of a file using "cp" command

neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro.txt Python.txt neeraj@ubuntu:~/unixtechworld_Workshop$ cp Linux_Distro.txt Linux_Distro_mylist.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro_mylist.txt Linux_Distro.txt Python.txt

7. You Can rename (or move content of one file or in to another file) using "mv" command

neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro_mylist.txt Linux_Distro.txt Python.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ mv Linux_Distro_mylist.txt Linux_Distro_shortlist.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro_shortlist.txt Linux_Distro.txt Python.txt

8. You can remove a file using "rm" file, remember there is no "recycle bin" when you delete a file from unix/linux command line, you may loose a file or data forever, hence be careful

neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro_shortlist.txt Linux_Distro.txt Python.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ rm Linux_Distro_shortlist.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro.txt Python.txt

9. You can create new directory by using "mkdir" command

neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro.txt Python.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ mkdir MyFirstDirectory
neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro.txt MyFirstDirectory Python.txt

10. You Can Remove Directory by using "rmdir" commnad, Again You need to be careful while running this command

neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro.txt MyFirstDirectory Python.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ rmdir MyFirstDirectory
neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro.txt Python.txt
neeraj@ubuntu:~/unixtechworld_Workshop$

11. You Can Create File using "touch" Command

neeraj@ubuntu:~/unixtechworld_Workshop$ touch MyFirstFile.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro.txt MyFirstDirectory MyFirstFile.txt Python.txt neeraj@ubuntu:~/unixtechworld_Workshop$

12. You Can Print or display your message using "echo" Command

neeraj@ubuntu:~/unixtechworld_Workshop$ echo "My First line"
My First line

13. You Can Redirect your the output message in to a file ( Write Data in a File ) using ">" output redirection operator, if you want to append another message in same file use ">>" ( Double redirection operator ) , remember if you again use ">" (Single redirection operator ) it will overwrite orignal data

neeraj@ubuntu:~/unixtechworld_Workshop$ echo "My First line" >MyFirstFile.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ echo "My Second Line" >>MyFirstFile.txt

14. you can read or display content of file using "cat" Command

neeraj@ubuntu:~/unixtechworld_Workshop$ cat MyFirstFile.txt
My First line
My Second Line

15. You Can Use "who" command to see the list of users who are currently logged in to this server/system. This Command will also show some useful information such as Source IP , logging time and Terminal. To obtain id of the current user can use "whoami" Command

neeraj@ubuntu:~/unixtechworld_Workshop$ who
neeraj pts/0 2022-05-29 03:32 (192.168.137.1)
root pts/1 2022-05-29 05:02 (192.168.137.1)
neeraj@ubuntu:~/unixtechworld_Workshop$ whoami
neeraj
neeraj@ubuntu:~/unixtechworld_Workshop$

16. You Can Filter Specific Pattern from a file and get filtered lines using "grep" command, For Example this file "MyFirstFile.txt" contains two lines, which are also visible in output of "cat", if you want to filter line based on string "First" , you can use it with grep


neeraj@ubuntu:~/unixtechworld_Workshop$ cat MyFirstFile.txt
My First line
My Second Line
neeraj@ubuntu:~/unixtechworld_Workshop$ grep First MyFirstFile.txt
My First line
neeraj@ubuntu:~/unixtechworld_Workshop$

17. Sometimes you need may need to direct oupput of one command as input for other command , you can simply achieve it by using "|" ( pipe symbol ),for example output of cat command is being used by grep command

neeraj@ubuntu:~/unixtechworld_Workshop$ cat Linux_Distro.txt
Red Hat Enterprise Linux
Debian
CentOS
Fedora
Ubuntu
Linux Mint
neeraj@ubuntu:~/unixtechworld_Workshop$ cat Linux_Distro.txt | grep CentOS
CentOS
neeraj@ubuntu:~/unixtechworld_Workshop$

18. You can use head and tail commands to see first ten and last ten lines of file or even directory content,In following example file "MyFile25lines.txt" has 25 line, we can use head and tail commands to see first ten and last 10 line respectively


neeraj@ubuntu:~/unixtechworld_Workshop/Hello_World_Programs$ cat MyFile25lines.txt
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
This is line 11
This is line 12
This is line 13
This is line 14
This is line 15
This is line 16
This is line 17
This is line 18
This is line 19
This is line 20
This is line 21
This is line 22
This is line 23
This is line 24
This is line 25
neeraj@ubuntu:~/unixtechworld_Workshop/Hello_World_Programs$ head MyFile25lines.txt
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
neeraj@ubuntu:~/unixtechworld_Workshop/Hello_World_Programs$ tail MyFile25lines.txt
This is line 16
This is line 17
This is line 18
This is line 19
This is line 20
This is line 21
This is line 22
This is line 23
This is line 24
This is line 25
neeraj@ubuntu:~/unixtechworld_Workshop/Hello_World_Programs$

19. You Can use df command to see disk space utiliztion of variuos mount points of your linux/unix machine,for exmaple here my "/" ( root ) mount point utilization is 40 % ( root directory is the first or top-most directory in a hierarchy),its highlited in red just for visibility purpose.

20. neeraj@ubuntu:~/unixtechworld_Workshop$ df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 445056 0 445056 0% /dev
tmpfs 95708 1604 94104 2% /run
/dev/sda5 19992176 7395600 11557984 40% /
tmpfs 478528 0 478528 0% /dev/shm
tmpfs 5120 4 5116 1% /run/lock
tmpfs 478528 0 478528 0% /sys/fs/cgroup
/dev/loop0 56960 56960 0 100% /snap/core18/2284
/dev/loop1 63488 63488 0 100% /snap/core20/1361
/dev/loop2 56960 56960 0 100% /snap/core18/2409
/dev/loop3 128 128 0 100% /snap/bare/5
/dev/loop4 63488 63488 0 100% /snap/core20/1494
/dev/loop5 224256 224256 0 100% /snap/gnome-3-34-1804/72
/dev/loop6 224256 224256 0 100% /snap/gnome-3-34-1804/77
/dev/loop7 254848 254848 0 100% /snap/gnome-3-38-2004/99
/dev/loop8 66816 66816 0 100% /snap/gtk-common-themes/1519
/dev/loop9 55552 55552 0 100% /snap/snap-store/558
/dev/loop10 83328 83328 0 100% /snap/gtk-common-themes/1534
/dev/loop11 52224 52224 0 100% /snap/snap-store/547
/dev/loop12 45824 45824 0 100% /snap/snapd/15904
/dev/loop13 44672 44672 0 100% /snap/snapd/14978
/dev/sda1 523248 4 523244 1% /boot/efi
tmpfs 95704 24 95680 1% /run/user/125
tmpfs 95704 8 95696 1% /run/user/1000
neeraj@ubuntu:~/unixtechworld_Workshop$

21. You Can Check IP address and related information of your system using "ifconfig" command

neeraj@ubuntu:~/unixtechworld_Workshop/Hello_World_Programs$ ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.137.129 netmask 255.255.255.0 broadcast 192.168.137.255
inet6 fe80::44f2:b2c0:ccab:1c1e prefixlen 64 scopeid 0x20
ether 00:0c:29:bf:85:2a txqueuelen 1000 (Ethernet)
RX packets 5088 bytes 1632008 (1.6 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3410 bytes 426427 (426.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 360 bytes 31288 (31.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 360 bytes 31288 (31.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

22. You can Check connectivity with other IPs/systems using ping command, just press ctrl+c once you are done

neeraj@ubuntu:~/unixtechworld_Workshop$ ping 192.168.112.1
PING 192.168.112.1 (192.168.112.1) 56(84) bytes of data.
64 bytes from 192.168.112.1: icmp_seq=1 ttl=128 time=1.01 ms
64 bytes from 192.168.112.1: icmp_seq=2 ttl=128 time=1.74 ms
^C
--- 192.168.112.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 1.010/1.374/1.739/0.364 ms
neeraj@ubuntu:~/unixtechworld_Workshop$

23. Using Arguments with commands - While unix command we have learned so far are very useful, they become more useful if they are used with argument, Agruments makes commands very powerful, for example while "cat" will simply display conent of file, using it with option "-n" will also print the line number, here is the example

neeraj@ubuntu:~/unixtechworld_Workshop$ cat MyFirstFile.txt
My First line
My Second Line
neeraj@ubuntu:~/unixtechworld_Workshop$ cat -n MyFirstFile.txt
1 My First line
2 My Second Line

24. Using unix man pages : Unix also has something called man pages as a refernce or help manual for its users, to get information about a command you can simply type "man" followed by command name, you can use "q" (quit ) option for coming out man pages

neeraj@ubuntu:~/unixtechworld_Workshop$ man cat
CAT(1) User Commands
CAT(1)
NAME
cat - concatenate files and print on the standard output
SYNOPSIS
cat [OPTION]... [FILE]...
DESCRIPTION
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends
display $ at end of each line
------------------------Complete output not printed----------------------------------------------



By now are aware of basic unix commands, hope you have enjoyed learning, read other post this blog to learn more unixtechworld.blogspot.com


Saturday, March 4, 2017

sort command in Unix and Linux with examples


Order plays an important role in our life, it makes things easier and manageable. Unix / Linux too have something to offer to "sort" text files. Text Processing utility  sort is very useful for this purpose .
lets see how it can help us -

I have a file called "Unix_Linux_Inventors.txt" it has following text inside it -

Ken Thompson
Linus Torvalds
Joe Ossanna
Dennis Ritchie
Douglas McIlroy





I tried to sort this file using sort command , it produced following output -



So now you have some idea how basic sort works

remember by default sort will not make any change in original file , it only displays you the sorted output on your terminal , however you  may actually want to replace original file with  sorted output or create a new file with the sorted output

in such situation you should use -o option of sort command like the example shown below -




To understand one more important feature of sort command ( numeric sort ) lets have a look at files inside my current directory with the help of  "ls -ltr"



Now with the help of cut command which i have explained in one of my previous post  ( cut command with examples ) , i have formatted the output of "ls -ltr"   -






Now if i sort this output using "sort" without any option , its going to produce following output


 

impressive ? Not really ? .... But i would say it is impressive ....
if you observe clearly ,sorting did work, first two rows are starting with 1 third with 2 and so on .

However  sometimes you may need your sorting to work purely on the basis of numbers and "-n" is specifically useful in such case , To understand how does it work , see below -








So this time it impressive even at first glance ☺,

but wait , What if you wanted this output in reverse order ? just use "-r" option , combined with "-n"






Now lets have look at another example

I have a file called "Shell_List.txt", it contains following text -

1,sh is a shell
3,ksh is a shell
5,csh is a shell
2,tcsh is a shell
4,bash is a shell
6,zsh is a shell






I want to sort this text file based upon alphabets in second column ( considering , as field separator  )

both plain "sort" and "sort -n" are not going to help -






however "sort" is going to sort out things for me as it have another options to do that "-t" and "-k"

 lets see how -





so to specify a certain pattern as a field pattern , you can use "-t " ( comma in this case ) and you can specify field position with -k option  and then sorting can be performed on the column you chose .

sorting is also useful when you have duplicate rows in your file .
I have a file called "Australia.txt" which contains following text

Sydney
Melbourne
Sydney
Brisbane
Perth
Perth
Darwin
Canberra






You can see both Sydney and Perth are appearing twice , with "-u" option , you can remove both sort file text and remove duplicate rows.




  "-c"  is another useful option which tells you if a file is already sorted or not, see below example -



I hope you all found this post useful, here are the links to my previous posts, do read them ,
and give your suggestions  , thanks for reading


if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com