Linux Tips and Tricks



People write blogs, so that when needed they can refer. I read this article  “55 Best Linux Tips, Tricks And Command Lines”. Let me list down this points, that I am not aware of.

Run a Linux command after every reboot 

$crontab -e

@reboot /home/xyz/myscript.sh

Do remember to enable crond on boot.

Know your shells

Here is a command that will let you know about the available shells on your Linux distribution:

#chsh -l

To change your login shell, use the following command: 

# chsh

LS

lspci – Lists all PCI devices. Use -v for verbose output.
lsusb – Lists all USB devices. Use -v for verbose output.
lsmod – Lists the status of modules in the Linux kernel.
lsattr – Lists file attributes on a second extended Linux file system.
lsof – Lists the file descriptors opened by all the processes. A very useful command when a process fails to close any file descriptors.
ls -l --color=auto

ls -lSr Show files by size, biggest last
pushd . Put current dir on stack so you can popd back to it

View the contents of tar and rpm files

1. To view the content of a tar file, issue the following command:

#tar -tvf /path/to/file.tar

2. To view the content of an rpm file, use the command given below:

#rpm -qlp /path/to/file.rpm



Check your processor and OS architecture

$ uname -m
$ lshw -class processor | grep width
$ uname -a
Kernel version and system architecture 

Ignoring the case during TAB-completion

set completion-ignore-case on

Identify your current shell name

$ echo $SHELL
$ echo $0

Built in variables:

$1-$N Stores the arguments (variables) that were passed to the shell program from the command line.
$? Stores the exit value of the last command that was executed.
$0 Stores the first word of the entered command (the name of the shell program).
$* Stores all the arguments that were entered on the command line ($1 $2 ...).
"$@" Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...).


Scan open ports

TCP
nmap -sS -O 
UDPnmap -sU -O 

To find out last word

$cat fileinfo.txt | rev | awk '{print $1}' | rev

Know the libraries used by a program

ldd

Undo your changes even after quitting the VIM editor
As all of us know, if you make changes in a file using VIM editor, the changes are permanent and you cannot get the old version back after you save and quit the editor.
But VIM v7.3 allows you to get the old version back even after quitting the editor.
Here is a tip that shows you how to configure VIM to remember changes.
To enable Undo, execute the following commands in VIM just before starting to edit the file.
:set undofile
:set undodir=/tmp
This is to be done every time you start editing a file. In case you need the configuration to be there for all files that you open in VIM, create a file called '.exrc' or '.vimrc' in $HOME directory. In my case, it is /myhome. 
Open the just created file and add the following commands:
# vi /myhome/.exrc
set undofile
set undodir=/tmp
Save and close the file.
:wq
From now onwards, the Undo history is maintained in the background for all files that you edit with VIM. 

Measuring the network throughput between two Linux systems 

Iperf is a tool that measures the bandwidth and the quality of a network link. It can be installed very easily on any Linux system. One host must be set as the client and the other one as the server. Make sure that iperf is installed on both systems. If it is not installed, then use your package manager to install it before trying this tip.

Now run iperf on one of the Linux systems as the server, as shown below:

linux-erv3:/home/test/Desktop # iperf -s

------------------------------------------------------------
Server listening on TCP port 5001

TCP window size: 85.3 KByte (default)
------------------------------------------------------------

Go to the second Linux system and run iperf -c as the client:

linux-6bg3:~ # iperf -c 192.168.1.100

------------------------------------------------------------
Client connecting to 192.168.1.100, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------

[ 3] local 192.168.1.109 port 39572 connected with 192.168.1.100 port 5001

^C[ ID] Interval Transfer Bandwidth

[ 3] 0.0- 6.3 sec 6.38 MBytes 8.51 Mbits/sec

By default, the iperf client connects to the iperf server on the TCP port 5001 and the bandwidth displayed by iperf is the bandwidth from the client to the server. In the above example, it is 8.51 Mbits/sec between two Linux test systems connected over a wireless network.

Print a file with line numbers 

$nl file.c > output.txt

Power yourself with Netstat 

netstat -punta

To display the kernel interface table:
netstat -i
To display the kernel routing table:
netstat -rn
To display all open network sockets:
netstat -uta
To display network statistics:
netstat -s


BASH Script


Numeric Comparisons

int1 -eq int2 Returns True if int1 is equal to int2.
int1 -ge int2 Returns True if int1 is greater than or equal to int2.
int1 -gt int2 Returns True if int1 is greater than int2.
int1 -le int2 Returns True if int1 is less than or equal to int2
int1 -lt int2 Returns True if int1 is less than int2
int1 -ne int2 Returns True if int1 is not equal to int2

String Comparisons

str1 = str2 Returns True if str1 is identical to str2.
str1 != str2 Returns True if str1 is not identical to str2.
str Returns True if str is not null.
-n str Returns True if the length of str is greater than zero.
-z str Returns True if the length of str is equal to zero. (zero is different than null)

File Comparisons

-d filename Returns True if file, filename is a directory.
-f filename Returns True if file, filename is an ordinary file.
-r filename Returns True if file, filename can be read by the process.
-s filename Returns True if file, filename has a nonzero length.
-w filename Returns True if file, filename can be written by the process.
-x filename Returns True if file, filename is executable.


Expression Comparisons

!expression

Returns true if expression is not true
expr1 -a expr2

Returns True if expr1 and expr2 are true. ( && , and )
expr1 -o expr2



Returns True if expr1 or expr2 is true. ( ||, or )

Convert images to ascii art


$ asciiview Tux.png -driver curses

To download a web page(s):
#wget -m -r -l5

Show information about mounted volumes:
#df -h

Networking

  ethtool eth0 Show status of ethernet interface eth0
  ethtool --change eth0 autoneg off speed 100 duplex full Manually set ethernet interface speed
  iw dev wlan0 link Show link status of wireless interface wlan0
  iw dev wlan0 set bitrates legacy-2.4 1 Manually set wireless interface speed
iw dev wlan0 scan List wireless networks in range
ip link show List network interfaces
  ip link set dev eth0 name wan Rename interface eth0 to wan
  ip link set dev eth0 up Bring interface eth0 up (or down)
ip addr show List addresses for interfaces
  ip addr add 1.2.3.4/24 brd + dev eth0 Add (or del) ip and mask (255.255.255.0)
ip route show List routing table
  ip route add default via 1.2.3.254 Set default gateway to 1.2.3.254
ss -tupl List internet services on a system
ss -tup List active connections to/from system
host pixelbeat.org Lookup DNS ip address for name or vice versa
hostname -i Lookup local ip address (equivalent to host `hostname`)
whois pixelbeat.org Lookup whois info for hostname or ip address

Text Manipulation

  sed 's/string1/string2/g' Replace string1 with string2
  sed 's/\(.*\)1/\12/g' Modify anystring1 to anystring2
  sed '/^ *#/d; /^ *$/d' Remove comments and blank lines
  sed ':a; /\\$/N; s/\\\n//; ta' Concatenate lines with trailing \
  sed 's/[ \t]*$//' Remove trailing spaces from lines
  sed 's/\([`"$\]\)/\\\1/g' Escape shell metacharacters active within double quotes
seq 10 | sed "s/^/      /; s/ *\(.\{7,\}\)/\1/" Right align numbers
seq 10 | sed p | paste - - Duplicate a column
  sed -n '1000{p;q}' Print 1000th line
  sed -n '10,20p;20q' Print lines 10 to 20
  sed -n 's/.*\(.*\)<\/title>.*/\1/ip;T;q'<span class="Apple-tab-span" style="white-space:pre"> </span>Extract title from HTML web page</font></p> <p> <font face="Verdana, sans-serif"> <span class="Apple-tab-span" style="white-space:pre"> </span>sed -i 42d ~/.ssh/known_hosts<span class="Apple-tab-span" style="white-space:pre"> </span>Delete a particular line</font></p> <p> <font face="Verdana, sans-serif"> <span class="Apple-tab-span" style="white-space:pre"> </span>sort -t. -k1,1n -k2,2n -k3,3n -k4,4n<span class="Apple-tab-span" style="white-space:pre"> </span>Sort IPV4 ip addresses</font></p> <p> <font face="Verdana, sans-serif">•<span class="Apple-tab-span" style="white-space:pre"> </span>echo 'Test' | tr '[:lower:]' '[:upper:]'<span class="Apple-tab-span" style="white-space:pre"> </span>Case conversion</font></p> <p> <font face="Verdana, sans-serif">•<span class="Apple-tab-span" style="white-space:pre"> </span>tr -dc '[:print:]' < /dev/urandom<span class="Apple-tab-span" style="white-space:pre"> </span>Filter non printable characters</font></p> <p> <font face="Verdana, sans-serif">•<span class="Apple-tab-span" style="white-space:pre"> </span>tr -s '[:blank:]' '\t' </proc/diskstats | cut -f4<span class="Apple-tab-span" style="white-space:pre"> </span>cut fields separated by blanks</font></p> <p> <font face="Verdana, sans-serif">•<span class="Apple-tab-span" style="white-space:pre"> </span>history | wc -l<span class="Apple-tab-span" style="white-space:pre"> </span>Count lines</font></p> <p> <font face="Verdana, sans-serif">•<span class="Apple-tab-span" style="white-space:pre"> </span>seq 10 | paste -s -d ' '</font></p> <p> <font face="Verdana, sans-serif"><br></font></p> <p> <font face="Verdana, sans-serif"><b>Debugging</b></font></p> <p> <font face="Verdana, sans-serif">•<span class="Apple-tab-span" style="white-space:pre"> </span>strace -c ls >/dev/null<span class="Apple-tab-span" style="white-space:pre"> </span>Summarise/profile system calls made by command</font></p> <p> <font face="Verdana, sans-serif">•<span class="Apple-tab-span" style="white-space:pre"> </span>strace -f -e open ls >/dev/null<span class="Apple-tab-span" style="white-space:pre"> </span>List system calls made by command</font></p> <p> <font face="Verdana, sans-serif">•<span class="Apple-tab-span" style="white-space:pre"> </span>strace -f -e trace=write -e write=1,2 ls >/dev/null<span class="Apple-tab-span" style="white-space:pre"> </span>Monitor what's written to stdout and stderr</font></p> <p> <font face="Verdana, sans-serif">•<span class="Apple-tab-span" style="white-space:pre"> </span>ltrace -f -e getenv ls >/dev/null<span class="Apple-tab-span" style="white-space:pre"> </span>List library calls made by command</font></p> <p> <font data-blogger-escaped-style="background-color: white; font-family: verdana; text-align: start;" face="Verdana, sans-serif"></font></p> <p> <font face="Verdana, sans-serif">•<span class="Apple-tab-span" style="white-space:pre"> </span>tcpdump not port 22<span class="Apple-tab-span" style="white-space:pre"> </span>Show network traffic except ssh. See also tcpdump_not_me</font></p> <div> <br></div> <p> <font data-blogger-escaped-style="background-color: white; font-family: verdana; text-align: start;" face="Verdana, sans-serif"><br></font></p> <p> <font data-blogger-escaped-style="background-color: white; font-family: verdana; text-align: start;" face="Verdana, sans-serif"><b>Ref: </b></font></p> <p> <font data-blogger-escaped-style="background-color: white; font-family: verdana; text-align: start;" face="Verdana, sans-serif"><br></font></p> <p> <font data-blogger-escaped-style="background-color: white; font-family: verdana; text-align: start;" face="Verdana, sans-serif">http://www.cheat-sheets.org/saved-copy/ubunturef.pdf</font></p> <p> <font data-blogger-escaped-style="background-color: white; font-family: verdana; text-align: start;" face="Verdana, sans-serif">http://www.cheat-sheets.org/saved-copy/unix_command_quickref.pdf</font></p> <p> <font data-blogger-escaped-style="background-color: white; font-family: verdana; text-align: start;" face="Verdana, sans-serif">http://www.pixelbeat.org/cmdline.html</font></p> <p> <font data-blogger-escaped-style="background-color: white; font-family: verdana; text-align: start;" face="Verdana, sans-serif">http://jd40c.com/linux.html</font></p> <p> <font data-blogger-escaped-style="background-color: white; font-family: verdana; text-align: start;" face="Verdana, sans-serif"><br></font></p> </div> <p> <font face="Verdana, sans-serif"><font style="background-color: white; text-align: justify;"><b><br></b></font></font></p> </div> </div>

* Delete line above current line at VIM : dgg
* Delete line above at VIM : jdG

3 comments:

Manish Panchmatia said...

https://github.com/rkgade/the-art-of-command-line

https://github.com/alebcay/awesome-shell

http://redsymbol.net/articles/unofficial-bash-strict-mode/

https://github.com/agarrharr/awesome-cli-apps

https://github.com/Idnan/bash-guide

Manish Panchmatia said...

https://www.thegeekstuff.com/2010/12/50-unix-linux-sysadmin-tutorials/

Manish Panchmatia said...

http://linux4beginners.info/

Post a Comment