Linux Tricks

21) Check out the Network Service Activity in Real-time.

# lsof -i

[root@server1 ~]#  lsof -i
COMMAND     PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rpcbind    1838     rpc    6u  IPv4  13814      0t0  UDP *:sunrpc
rpcbind    1838     rpc    7u  IPv4  13816      0t0  UDP *:netgw
rpcbind    1838     rpc    8u  IPv4  13817      0t0  TCP *:sunrpc (LISTEN)
rpcbind    1838     rpc    9u  IPv6  13819      0t0  UDP *:sunrpc
rpcbind    1838     rpc   10u  IPv6  13821      0t0  UDP *:netgw

22) find zombie processes on Linux.

[root@server1 ]# ps axo stat,ppid,pid,comm | grep -w defunct

23) Rename the multiple files.

[root@server1 ]# ls
itcarearch1_66044_798634225.dbf  itcarearch1_66047_798634225.dbf  itcarearch1_66050_798634225.dbf  itcarearch1_66053_798634225.dbf
itcarearch1_66045_798634225.dbf  itcarearch1_66048_798634225.dbf  itcarearch1_66051_798634225.dbf
itcarearch1_66046_798634225.dbf  itcarearch1_66049_798634225.dbf  itcarearch1_66052_798634225.dbf
[root@server1 ]#rename  itcare  'WORld' *arch1*
[root@server1 ]#ls
WORldarch1_66044_798634225.dbf  WORldarch1_66047_798634225.dbf  WORldarch1_66050_798634225.dbf  WORldarch1_66053_798634225.dbf
WORldarch1_66045_798634225.dbf  WORldarch1_66048_798634225.dbf  WORldarch1_66051_798634225.dbf
WORldarch1_66046_798634225.dbf  WORldarch1_66049_798634225.dbf  WORldarch1_66052_798634225.dbf
[root@server1 ]#rename  660  'itcare' *660*
[root@server1 ]#ls
WORldarch1_itcare44_798634225.dbf  WORldarch1_itcare48_798634225.dbf  WORldarch1_itcare52_798634225.dbf
WORldarch1_itcare45_798634225.dbf  WORldarch1_itcare49_798634225.dbf  WORldarch1_itcare53_798634225.dbf
WORldarch1_itcare46_798634225.dbf  WORldarch1_itcare50_798634225.dbf
WORldarch1_itcare47_798634225.dbf  WORldarch1_itcare51_798634225.dbf
[root@server1 ]#

Note: if command is not found, then install “util-linux” package in system.

24) Show running services using systemctl command.

#systemctl –no-page -t service -a –state running –no-legend

[root@server1 ~]# systemctl --no-page -t service -a --state running --no-legend
atd.service              loaded active running Job spooling tools
auditd.service           loaded active running Security Auditing Service
crond.service            loaded active running Command Scheduler
dbus.service             loaded active running D-Bus System Message Bus
firewalld.service        loaded active running firewalld - dynamic firewall daemon

25) List latest 10 modified files recursively on current directory.

#find . -type f -printf ‘%T@ %TY-%Tm-%Td %TH:%TM:%.2TS %p\n’ | sort -nr | head -n 5 | cut -f2- -d” “

[root@server1 ~]# find . -type f -printf '%T@ %TY-%Tm-%Td %TH:%TM:%.2TS %p\n' | sort -nr | head -n 10 | cut -f2- -d" "
2016-09-24 10:58:02 ./.bash_history
2016-09-19 16:45:04 ./.viminfo
2016-09-16 21:12:05 ./.lesshst
2016-09-16 20:43:37 ./vsftpd-3.0.2-11.el7_2.x86_64.rpm
2016-09-06 13:03:23 ./.cpan/build/XML-Simple-2.22-HxDmPk/blib/man3/XML::Simple::FAQ.3pm
2016-09-06 13:03:23 ./.cpan/build/XML-Simple-2.22-HxDmPk/blib/man3/XML::Simple.3pm
2016-09-06 13:03:23 ./.cpan/build/XML-Simple-2.22-HxDmPk/pm_to_blib
2016-09-06 13:03:23 ./.cpan/build/XML-Simple-2.22-HxDmPk/blib/man3/.exists
2016-09-06 13:03:23 ./.cpan/build/XML-Simple-2.22-HxDmPk/blib/man1/.exists
2016-09-06 13:03:22 ./.cpan/build/XML-Simple-2.22-HxDmPk/blib/script/.exists
[root@server1 ~]#

Note: You can also find latest 10 modified files on different location by putting the location instead of “.”

26) Using ps command show the command line for a PID.

[root@server1 ~]# ps h -o %a 18762
/usr/bin/ruby /usr/bin/puppet agent --no-daemonize
[root@server1 ~]# ps h -o %a 8651
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
[root@server1 ~]# ps h -o %a 14707
/usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
[root@server1 ~]#

27) Summary of disk usage including hidden files and directory on current location.

#du -hs .[^.]* * | sort -h

[root@server1 ~]# du -hs .[^.]* * | sort -h
0       .pki
4.0K    anaconda-ks.cfg
4.0K    .bash_logout
4.0K    .bash_profile
4.0K    .bashrc
4.0K    .cshrc
4.0K    .elinks
4.0K    firstline
4.0K    .htaccess
4.0K    .install4j
4.0K    .lesshst
4.0K    loginscript
4.0K    .mysql_history
4.0K    pradipscript
4.0K    .psql_history
4.0K    .rnd
4.0K    sent
4.0K    .tcshrc
12K     .ssh
12K     .viminfo
24K     .bash_history
52M     .cpan
117M    nagios
[root@server1 ~]#

28) Go to previous directory you were in.

#cd –

[root@server1 nagios]# pwd
/usr/local/nagios
[root@server1 nagios]# cd ~
[root@server1 ~]# pwd
/root
[root@server1 ~]# cd -
/usr/local/nagios
[root@server1 nagios]#

29) How to list only subdirectories in the current location.

# ls -d */

[root@server1 ~]# ls -d */
nagios/
[root@server1 ~]#

30) Creating directory bookmarks.

There are some directories that you will return to time and again. It’s possible to create bookmarks that allow you to quickly navigate to those directories.

Add the directory path With $CDPATH shell variable within your .bashrc file:

#cd ~
#vim .bashrc

# export SYSTEMD_PAGER=

# User specific aliases and functions
CDPATH='.:/home/gaurav/dir1/sanovi'
~
~
:wq

#cd sanovi

[root@server1 ~]# cd sanovi
/home/hcladmin/dir1/sanovi
[root@server1 sanovi ]#