Linux commands - A Handbook
Komala Nand Pandey
QA Automation Engineer | Salesforce/Mulesoft Automation| Selenium WDIO Cucumber BDD | Java TestNG | Python Pytest | Rest Assured JMeter | CICD Jenkins/Gitlab l Docker Kubernetes | PSM-I Certified || 2x AWS Certified
=>Here are some handy linux commands used in daily basis
==============Linux Happy Learning==================
Sudo -i?
192:/ ribhu-divine$ sudo -i
tty19tts
2:~ root# tty
/dev/ttys000
192:~ root#?
hostname
192:~ root# hostname
192.168.1.4
Users
192:~ root# users
ribhu-divine
adduser username
E.g.?
vagrant@vagrant:/$ sudo adduser testusertes
usermod -g usergroup username
vagrant@vagrant:/$ date
Fri Nov? 4 16:06:54 UTC 2022
vagrant@vagrant:/$ date +%d
04
vagrant@vagrant:/$ date +%D
11/04/22
vagrant@vagrant:/$ date +%c
Fri Nov? 4 16:07:13 2022
vagrant@vagrant:/$ date +%C
20
vagrant@vagrant:/$ date +%H
16
vagrant@vagrant:/$ date +%B
November
vagrant@vagrant:/$ date +%b
Nov
vagrant@vagrant:/$ date +%w
5? ( day of week )
vagrant@vagrant:/$ date +%W
44 (Weeks of the year)
vagrant@vagrant:/$ date +%m
11 (Month)
vagrant@vagrant:/$ date +%M
12 (minutes)
cal
cal 2022
cal 11 2022?
========colors======== :?
Black? ? ? ? 0;30 background : 40
Red? ? ? ? ? 0;31? background : 41
Green? ? ? ? 0;32? background : 42
Brown/Orange 0;33 background : 43
Blue ? ? ? ? 0;34? background : 44
Purple ? ? ? 0;35? background : 45
Cyan ? ? ? ? 0;36 background : 46
E.g.
vagrant@vagrant:/$ echo -e "\033[0;31m komal"
?komal
vagrant@vagrant:/$ echo -e "\033[0;41m komal"
?komal
pwd
E.g.
vagrant@vagrant:~$ pwd
/home/vagrant
touch filename.extension
head filename
tail filename
Head - 15 filename
Tail - 50 filename
more filename
less filename
vagrant@vagrant:~$ wc test.txt?
??159? 1705 10340 test.txt
vagrant@vagrant:~$ wc -l test.txt?
159 test.txt
vagrant@vagrant:~$ wc -c test.txt?
10340 test.txt
vagrant@vagrant:~$ wc -w test.txt?
1705 test.txt
vagrant@vagrant:~$?
cut -f <field position> filename
1. mango
2. apple
3. banana
4. pine apple
?
vagrant@vagrant:~$ cut -f 2 -d '.' test.txt?
?mango
?apple
?banana
?pine apple
vagrant@vagrant:~$ sort test.txt
1. mango
2. apple
3. banana
4. pine apple
vagrant@vagrant:~$ sort -r test.txt
4. pine apple
3. banana
2. apple
1. mango
sort firstfile secondFile
e.g.
vagrant@vagrant:~$ sort tes.py test.txt
?1. mango
2. apple
3. banana
4. pine apple
echo "this is my python first file"
vagrant@vagrant:~$ ls
cut? date? echo? tes.py? test.txt
vagrant@vagrant:~$ cat tes.py
2. apple
1. mango
echo "this is my python first file"
?
vagrant@vagrant:~$ cat test.txt
1. mango
2. apple
3. banana
4. pine apple
?
vagrant@vagrant:~$ sort test.txt tes.py
1. mango
1. mango
2. apple
2. apple
3. banana
4. pine apple
echo "this is my python first file"
vagrant@vagrant:~$ sort -o merge-result.txt test.txt tes.py
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt
vagrant@vagrant:~$ cat merge-result.txt?
1. mango
1. mango
2. apple
2. apple
3. banana
4. pine apple
echo "this is my python first file"
vagrant@vagrant:~$ sort -u -o merge-result.txt test.txt tes.py?
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt
vagrant@vagrant:~$ cat merge-result.txt?
1. mango
2. apple
3. banana
4. pine apple
echo "this is my python first file"
vagrant@vagrant:~$?
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt
vagrant@vagrant:~$ ls > result.txt
vagrant@vagrant:~$ cat result.txt?
cut
date
echo
merge-result.txt
result.txt
tes.py
test.txt
vagrant@vagrant:~$ lsss > result.txt 2> error.txt
vagrant@vagrant:~$ cat result.txt?
vagrant@vagrant:~$ cat error.txt?
-bash: lsss: command not found
vagrant@vagrant:~$?
In linux -?
0 means standard input
1 means standard output
2 means standard error
vagrant@vagrant:~$ kuchhhhhhhbhi >> result.txt 2>&1
vagrant@vagrant:~$ cat result.txt?
cut
date
echo
error.txt
merge-result.txt
result.txt
tes.py
test.txt
cut
date
echo
error.txt
merge-result.txt
result.txt
tes.py
test.txt
-bash: kuchhhhhhhbhi: command not found
??rm directoryame/anotherdirectoryname/filename
??rm directoryame/anotherdirectoryname/*
??rm directoryame/anotherdirectoryname/*.txt
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt? testdir
vagrant@vagrant:~$ rm testdir/anothertestdir/*
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt? testdir
vagrant@vagrant:~$ cd test
-bash: cd: test: No such file or directory
vagrant@vagrant:~$ cd testdir
vagrant@vagrant:~/testdir$ cd anothertestdir/
vagrant@vagrant:~/testdir/anothertestdir$ ls
vagrant@vagrant:~/testdir/anothertestdir$ cd ..
vagrant@vagrant:~/testdir$ cd ..
vagrant@vagrant:~$ rm -rf testdir/anothertestdir
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt? testdir
vagrant@vagrant:~$ cd testdi
-bash: cd: testdi: No such file or directory
vagrant@vagrant:~$ cd testdir
vagrant@vagrant:~/testdir$ ls
vagrant@vagrant:~/testdir$ cd ..
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt? testdir
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt? testdir
vagrant@vagrant:~$ rmdir -v testdir
rmdir: removing directory, 'testdir'
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt
vagrant@vagrant:~$?
rm -rf directoryname? or filename
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt? testdir
vagrant@vagrant:~$ rm -f testdir
rm: cannot remove 'testdir': Is a directory
vagrant@vagrant:~$ rm -rf testdir
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt
rm -rfv directoryName
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt? testdir
vagrant@vagrant:~$ rm -rfv testdir
removed 'testdir/testfile.py'
领英推荐
removed directory 'testdir'
vagrant@vagrant:~$?
Hard link:? Points to physical drive location, deletion of link or original does not impact other file.
E.g. create hardlink for file1.txt?
vagrant@vagrant:~$ ln file1.txt /tmp/file1_hardlink.txt
vagrant@vagrant:~$ ls /tmp/file1_hardlink.txt?
/tmp/file1_hardlink.txt
vagrant@vagrant:~$ cat file1.txt
this is file one?
to tst?
hard link soft link?
on my tuturial
vagrant@vagrant:~$ cat /tmp/file1_hardlink.txt?
this is file one?
to tst?
hard link soft link?
on my tuturial
vagrant@vagrant:~$ cat >> file1.txt?
this is updated
^Z
[22]+? Stopped ? ? ? ? ? ? ? ? cat >> file1.txt
?
vagrant@vagrant:~$ cat file1.txt?
this is file one?
to tst?
hard link soft link?
on my tuturial
this is updated
?
vagrant@vagrant:~$ cat /tmp/file1_hardlink.txt?
this is file one?
to tst?
hard link soft link?
on my tuturial
this is updated
vagrant@vagrant:~$ rm file1.txt?
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt
vagrant@vagrant:~$ ls /tmp/file1_hardlink.txt?
/tmp/file1_hardlink.txt
vagrant@vagrant:~$?
file1_hardlink.txt file still intact after removing original file file1.txt because file1_hardlink points to the actual data (inode) not the file reference.
Soft Link: Points to reference of original file, so if original file deleted the sortlink file is no use.
ln -s filename.txt <pathToHardLink>
vagrant@vagrant:~$ cat file2.txt?
asdas
this is file 2 to test softlink
vagrant@vagrant:~$ ln -s file2.txt /tmp/file2_softlink.txt
vagrant@vagrant:~$ ls
cut? date? echo? file2.txt? merge-result.txt? tes.py? test.txt
vagrant@vagrant:~$ ls /tmp/file2_softlink.txt?
/tmp/file2_softlink.txt
vagrant@vagrant:~$ vim /tmp/file2_softlink.txt?
vagrant@vagrant:~$ cat /tmp/file2_softlink.txt?
this is softlink file updating
?
vagrant@vagrant:~$ cat file2.txt
asdas
this is file 2 to test softlink
vagrant@vagrant:~$ cat >> file2.txt?
this is Original file updating
^Z
[24]+? Stopped ? ? ? ? ? ? ? ? cat >> file2.txt
vagrant@vagrant:~$?
vagrant@vagrant:~$ cat file2.txt?
asdas
this is file 2 to test softlink
this is Original file updating
vagrant@vagrant:~$ cat /tmp/file2_softlink.txt?
this is softlink file updating
?
?ln test.txt /temp/
ls -li
vagrant@vagrant:~$ ls -li
total 12
3932187 -rw-rw-r-- 1 vagrant vagrant ? 0 Nov? 4 17:20 cut
3932191 -rw-rw-r-- 1 vagrant vagrant ? 0 Nov? 4 17:21 date
3932194 -rw-rw-r-- 1 vagrant vagrant ? 0 Nov? 4 17:21 echo
3932193 -rw-rw-r-- 1 vagrant vagrant? 79 Nov? 4 17:37 merge-result.txt
3932196 -rw-rw-r-- 1 vagrant vagrant? 55 Nov? 4 17:34 tes.py
3932192 -rw-rw-r-- 2 vagrant vagrant 178 Nov? 7 16:09 test.txt
Test.txt file has 2 links but both have same inode reference number
vagrant@vagrant:~$ ls -li test.txt
3932192 -rw-rw-r-- 2 vagrant vagrant 178 Nov? 7 16:09 test.txt
vagrant@vagrant:~$ ls -li /tmp/test.txt
3932192 -rw-rw-r-- 2 vagrant vagrant 178 Nov? 7 16:09 /tmp/test.txt
Now update one place, it will automatically be updated on another place. You can keep some other name for the linked file.
ls -lh
ls -l <location>
/ - for root location
vagrant@vagrant:~$ ls -lh /
total 88K
drwxr-xr-x ? 2 root? ? root? ? 4.0K Dec 19? 2021 bin
drwxr-xr-x ? 4 root? ? root? ? 4.0K Dec 19? 2021 boot
drwxr-xr-x? 18 root? ? root? ? 3.8K Nov? 3 17:55 dev
drwxr-xr-x? 95 root? ? root? ? 4.0K Nov? 4 16:01 etc
drwxr-xr-x ? 6 root? ? root? ? 4.0K Nov? 4 16:00 home
ls -l Char*
vagrant@vagrant:~$ ls -l t*
-rw-rw-r-- 1 vagrant vagrant? 55 Nov? 4 17:34 tes.py
-rw-rw-r-- 2 vagrant vagrant 178 Nov? 7 16:09 test.txt
ls -l [!char]*
vagrant@vagrant:~$ ls
cut? date? echo? merge-result.txt? tes.py? test.txt
vagrant@vagrant:~$ ls -l [!t]*
-rw-rw-r-- 1 vagrant vagrant? 0 Nov? 4 17:20 cut
-rw-rw-r-- 1 vagrant vagrant? 0 Nov? 4 17:21 date
-rw-rw-r-- 1 vagrant vagrant? 0 Nov? 4 17:21 echo
-rw-rw-r-- 1 vagrant vagrant 79 Nov? 4 17:37 merge-result.txt
Note: Hard link not possible for a directory.
chmod u/g/o+r/w/x fileName/directory or * for all.
User plus(+) to give and minus(-) to take permissions
vagrant@vagrant:~$ ls -ltr
total 12
-rw-rw-r-- 1 vagrant vagrant ? 0 Nov? 4 17:20 cut
-rw-rw-r-- 1 vagrant vagrant ? 0 Nov? 4 17:21 date
-rw-rw-r-- 1 vagrant vagrant ? 0 Nov? 4 17:21 echo
-rw-rw-r-- 2 vagrant vagrant? 55 Nov? 4 17:34 tes.py
-rw-rw-r-- 1 vagrant vagrant? 79 Nov? 4 17:37 merge-result.txt
-rw-rw-r-- 2 vagrant vagrant 178 Nov? 7 16:09 test.txt
vagrant@vagrant:~$ chmod o-r tes.py
vagrant@vagrant:~$ ls -l
total 12
-rw-rw-r-- 1 vagrant vagrant ? 0 Nov? 4 17:20 cut
-rw-rw-r-- 1 vagrant vagrant ? 0 Nov? 4 17:21 date
-rw-rw-r-- 1 vagrant vagrant ? 0 Nov? 4 17:21 echo
-rw-rw-r-- 1 vagrant vagrant? 79 Nov? 4 17:37 merge-result.txt
-rw-rw---- 2 vagrant vagrant? 55 Nov? 4 17:34 tes.py
-rw-rw-r-- 2 vagrant vagrant 178 Nov? 7 16:09 test.txt
vagrant@vagrant:~$?
?
Mostly Octal numbers used while giving/taking permissios
1- Execute(x)
2 - Write (w)
3 - Read (r)
vagrant@vagrant:~$ ls -l test.txt?
-rw-rw-r-- 2 vagrant vagrant 178 Nov? 7 16:09 test.txt
vagrant@vagrant:~$ chmod 764 test.txt?
vagrant@vagrant:~$ ls -l test.txt?
-rwxrw-r-- 2 vagrant vagrant 178 Nov? 7 16:09 test.txt
?
764 =>?
7 (4+2+1 - read+write+execute) to user (user on 1st position)
6 (4+2 - read+write) to group (group on 2nd position)
4 (4 - read) to other (other on 3rd position)
sudo cat /etc/shadow
agrant@vagrant:~$ sudo chown testuser tes.py
vagrant@vagrant:~$ ls -ltr
total 20
-rw-rw-r-- 1 vagrant? vagrant? ? 0 Nov? 4 17:20 cut
-rw-rw-r-- 1 vagrant? vagrant? ? 0 Nov? 4 17:21 date
-rw-rw-r-- 1 vagrant? vagrant? ? 0 Nov? 4 17:21 echo
-rw-rw---- 2 testuser vagrant ? 55 Nov? 4 17:34 tes.py
-rw-rw-r-- 1 vagrant? vagrant ? 79 Nov? 4 17:37 merge-result.txt
-rwxrw-r-- 2 vagrant? vagrant? 178 Nov? 7 16:09 test.txt
drwxrwxr-x 2 vagrant? vagrant 4096 Nov? 7 17:28 chtest_dir
-rw-rw-r-- 1 vagrant? vagrant ? 14 Nov? 7 17:29 chFile.txt
vagrant@vagrant:~$ sudo chgrp testgroup tes.py
vagrant@vagrant:~$ ls -ltr
total 20
-rw-rw-r-- 1 vagrant? vagrant? ? ? 0 Nov? 4 17:20 cut
-rw-rw-r-- 1 vagrant? vagrant? ? ? 0 Nov? 4 17:21 date
-rw-rw-r-- 1 vagrant? vagrant? ? ? 0 Nov? 4 17:21 echo
-rw-rw---- 2 testuser testgroup ? 55 Nov? 4 17:34 tes.py
-rw-rw-r-- 1 vagrant? vagrant ? ? 79 Nov? 4 17:37 merge-result.txt
-rwxrw-r-- 2 vagrant? vagrant? ? 178 Nov? 7 16:09 test.txt
drwxrwxr-x 2 vagrant? vagrant ? 4096 Nov? 7 17:28 chtest_dir
-rw-rw-r-- 1 vagrant? vagrant ? ? 14 Nov? 7 17:29 chFile.txt
For directory -?
du -h?
For directories and files-
du? -ha
vagrant@vagrant:~$ du -h
4.0K ./.nano
8.0K ./.ssh
4.0K ./.cache
4.0K ./chtest_dir
72K .
vagrant@vagrant:~$ du -ha
4.0K ./.nano
4.0K ./.ssh/authorized_keys
8.0K ./.ssh
0 ./cut
0 ./echo
4.0K ./tes.py
4.0K ./.wget-hsts
4.0K ./.bash_history
4.0K ./chFile.txt
0 ./.sudo_as_admin_successful
4.0K ./.viminfo
4.0K ./.vbox_version
4.0K ./.bashrc
0 ./.cache/motd.legal-displayed
4.0K ./.cache
4.0K ./merge-result.txt
4.0K ./.profile
0 ./date
4.0K ./test.txt
4.0K ./chtest_dir
4.0K ./.bash_logout
4.0K ./.test.txt.swp
72K .
df -h?
df -ha
lsblk
vagrant@vagrant:~$ lsblk
NAME ? ? ? ? ? ? ? ? ? MAJ:MIN RM? SIZE RO TYPE MOUNTPOINT
sda? ? ? ? ? ? ? ? ? ? ? 8:0? ? 0 ? 64G? 0 disk?
|-sda1 ? ? ? ? ? ? ? ? ? 8:1? ? 0? 731M? 0 part /boot
|-sda2 ? ? ? ? ? ? ? ? ? 8:2? ? 0? ? 1K? 0 part?
`-sda5 ? ? ? ? ? ? ? ? ? 8:5? ? 0 63.3G? 0 part?
??|-vagrant--vg-root ? 252:0? ? 0 62.3G? 0 lvm? /
??`-vagrant--vg-swap_1 252:1? ? 0? 980M? 0 lvm? [SWAP]
RM - 0 means not removable, 1 means removable
RO - 0 means read only
To Zip a folder & files
tar -zcvf zipFileFolderName sourceFilefoldername
tar -zcvf test.tar.gz test.txt?
tar -zxvf test.tar.gz
==============Linux Keep Learning====================