Linux commands - A Handbook

=>Here are some handy linux commands used in daily basis

==============Linux Happy Learning==================

  • To go root user


Sudo -i?


192:/ ribhu-divine$ sudo -i

  • To see file from where terminal takes command

tty19tts

2:~ root# tty

/dev/ttys000

192:~ root#?

  • To view hostname

hostname

192:~ root# hostname

192.168.1.4

  • To see currently login users

Users

192:~ root# users

ribhu-divine

  • To add user

adduser username

E.g.?

vagrant@vagrant:/$ sudo adduser testusertes

  • To modify user

usermod -g usergroup username

  • Date commands

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)

  • Calendar commands

cal

  • To get current month

cal 2022

  • To get all months of the given year

cal 11 2022?

  • ?To get calendar of the given month and given year
  • To change the color

========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

  • To see present working directory

pwd

E.g.

vagrant@vagrant:~$ pwd

/home/vagrant

  • To modify date timestamp of any file

touch filename.extension

  • To see first 10 default lines

head filename

  • To see last 10 default lines?

tail filename

  • To see given number of last or first lines

Head - 15 filename

Tail - 50 filename

  • To see file content line by line towards the down

more filename

  • To see file content line by lines in both upwards and downwards

less filename

  • To see Line, Words, Characters of the file content

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:~$?

  • To get specific char(s) from the file

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

  • To sort and reverse sort

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

  • To merge two file content

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


  • To merge, keep unique content and store in another file


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:~$?

  • To capture error and positive result in 2 separate files

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



  • To redirect and Append both error and positive result in same file.


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


  • Kuchhhhhhhbi this is not a command, I used to fail intentionally?
  • >2 is used to say error file to redirect to &1 standard output file result.txt
  • To remove file inside directory


??rm directoryame/anotherdirectoryname/filename


  • We can use* to delete all file & use extension to delete specific


??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




  • To confirm if deletion happened or not, use verbose (-v)

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:~$?


  • To delete files inside directory or directories


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


  • To see directory file deletion in verbose mode


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:~$?



  • How to make a file if update one location automatically reflects all other places?? To create hardlink of a file.


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/

  • To check how many hardlink a file has

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.


  • To see file size in human readable


ls -lh


  • To view number of files on a given location


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


  • To search file start with given character


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


  • To search file not start with the given char


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.


  • To give permission to user(u),group(g) and other(o)


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)



  • To find user password in your linux system


sudo cat /etc/shadow


  • To change file owner?


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


  • To change file group


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



  • To view dist use in size


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 .



  • To view file system and mounted on


df -h?

df -ha


  • To list block storage


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


  • z = zip
  • c = Compress | x = extract
  • v = verbose
  • f = forcefully if needed


==============Linux Keep Learning====================

要查看或添加评论,请登录

社区洞察

其他会员也浏览了