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