Linux Tricks

11) Display the top ten running processes – sorted by memory usage.

# ps aux –sort -rss | head or ps -auxf | sort -nr -k 4 | head (if you want to read more about how to manage the process, refer below link. )

Manage the Processes of Linux using ps, pstree, lsof commands

[root@server1 ]# ps aux --sort -rss | head 
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND 
orabip   11877  0.0 11.3 9291280 3677596 ?     Ss   Aug17   4:52 ora_dbw0_BIP 
orabip   11881  0.0 11.1 9291280 3624720 ?     Ss   Aug17   9:33 ora_dbw1_BIP 
orabip   11885  0.0 11.0 9289232 3601160 ?     Ss   Aug17   8:20 ora_dbw2_BIP 
root     12266  1.1  8.6 2843564 2818020 ?     Ss    2015 7239:52 /usr/sap/hostctrl/exe/saposcol -l -w60 pf=/usr/sap/hostctrl/exe/host_profile 
orabip   29913  0.0  8.3 9286900 2708388 ?     Ss   Aug23  26:51 oracleBIP (LOCAL=NO) 
orabip   29883  0.0  7.9 9284840 2579220 ?     Ss   Aug23  22:14 oracleBIP (LOCAL=NO) 
orabip   29887  0.0  7.9 9294964 2569704 ?     Ss   Aug23  16:45 oracleBIP (LOCAL=NO) 
orabip   29921  0.0  7.6 9284856 2496932 ?     Ss   Aug23  23:27 oracleBIP (LOCAL=NO) 
orabip   29879  0.0  7.5 9284836 2465056 ?     Ss   Aug23  17:10 oracleBIP (LOCAL=NO) 
[root@server1 ]#

12) Add and remove Password Protection to a file using vim.

#vim -x <FILENAME>

[root@server1 ]# vim -x linux ↵

Once we enter the command, it will ask for the password.

Enter encryption key: ****** 
Enter same key again: ******

Now enter the content on this file.

Linux is the future. 
future for linux. 
future for linux. 
Linux is the future.

~ 
~ 
:wq

Now next time you open the file it will ask the password.

[root@server1 ]# vim  linux ↵
Need encryption key for "linux" 
Enter encryption key: ******

Now, how to remove the password of file.

For removing the password of file, first open the file and type :set key= then, save it.

13)  Find Duplicate Files on system.

Using below command you can find out the duplicate files based on size and MD5 hash.Let’s take the copy of “linux” file on “log” directory,So we have duplicate copy of “linux” file.

[root@server1 ~]# ls 
anaconda-ks.cfg  anaconda-screenshots  linux  linuxservers.doc  log 
[root@server1 ~]# cp -v linux log/ 
‘linux’ -> ‘log/linux’ 
[root@server1 ~]# find  -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate 
59fbf515b5d267678cd4f33c569b3543  ./linux 
59fbf515b5d267678cd4f33c569b3543  ./log/linux 
 
f36c8d9f581e332e7c643579974df9f4  ./anaconda-screenshots/screenshot-0000.png 
f36c8d9f581e332e7c643579974df9f4  ./anaconda-screenshots/screenshot-0001.png 
[root@server1 ~]#

14) Without sorting Remove duplicate entries in a file and save on different file.

#awk ‘!x[$0]++’ <file>

Suppose we have file on which some duplicate entries are there, as follow

 
[root@server1 ~]# cat linux  
Linux is the future. 
future for linux. 
future for linux. 
Linux is the future. 
[root@server1 ~]# awk '!x[$0]++' linux > linuxnew 
[root@server1 ~]# cat linuxnew  
Linux is the future. 
future for linux. 
[root@server1 ~]#

15) Find out the linux version on fedora/centos/rhel.

#cat /etc/*-release

[root@server1 ~]# cat /etc/*-release 
CentOS Linux release 7.2.1511 (Core)  
NAME="CentOS Linux" 
VERSION="7 (Core)" 
ID="centos" 
ID_LIKE="rhel fedora" 
VERSION_ID="7" 
PRETTY_NAME="CentOS Linux 7 (Core)" 
ANSI_COLOR="0;31" 
CPE_NAME="cpe:/o:centos:centos:7" 
HOME_URL="https://www.centos.org/" 
BUG_REPORT_URL="https://bugs.centos.org/" 
 
CENTOS_MANTISBT_PROJECT="CentOS-7" 
CENTOS_MANTISBT_PROJECT_VERSION="7" 
REDHAT_SUPPORT_PRODUCT="centos" 
REDHAT_SUPPORT_PRODUCT_VERSION="7" 
 
CentOS Linux release 7.2.1511 (Core)  
CentOS Linux release 7.2.1511 (Core)  
[root@server1 ~]#

16) Execute the three command in simple command.

[root@server1 ~]# yum clean all && yum remove all && yum repolist all 
Loaded plugins: fastestmirror 
Cleaning repos: Centos7 
Cleaning up everything 
Cleaning up list of fastest mirrors 
Loaded plugins: fastestmirror 
No Match for argument: all 
No Packages marked for removal 
Loaded plugins: fastestmirror 
Centos7                                                                                                                    | 3.6 kB  00:00:00      
(1/2): Centos7/group_gz                                                                                                    | 155 kB  00:00:00      
(2/2): Centos7/primary_db                                                                                                  | 5.3 MB  00:00:00      
Determining fastest mirrors 
repo id                                                     repo name                                                               status 
Centos7                                                     Centos7 repository                                                      enabled: 9,007 
repolist: 9,007 
[root@server1 ~]#

17) 10 biggest files/folders for the current directory.

[root@server1 ~]# du -s * | sort -n | tail

18) Mount .iso file in UNIX/Linux.

[root@server1 ~]#ls 
CentOS-7-x86_64-Minimal-1511(1).iso 
[root@server1 ~]#mount -o loop /root/CentOS-7-x86_64-Minimal-1511\(1\).iso /mnt 
mount: /dev/loop0 is write-protected, mounting read-only 
[root@server1 ~]#ls 
CentOS_BuildTag  EFI  EULA  GPL  images  isolinux  LiveOS  Packages  repodata  RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Testing-7  TRANS.TBL 
[root@server1 ~]#

19) Bind the /mnt directory with ftp pub dir.

As above, We mounted .iso file on /mnt directory.Now bind /mnt dir with pub dir.

[root@server1 ~]# mount --bind /mnt /var/ftp/pub/

Let’s see files in pub directory.

[root@server1 ~]# ftp 192.168.1.10 
Connected to 192.168.1.10 (192.168.1.10). 
220 (vsFTPd 3.0.2) 
Name (192.168.1.10:root): ftp 
331 Please specify the password. 
Password: 
230 Login successful. 
Remote system type is UNIX. 
Using binary mode to transfer files. 
ftp> ls 
227 Entering Passive Mode (192,168,1,10,156,103). 
150 Here comes the directory listing. 
drwxr-xr-x    5 0        0              94 Sep 18 05:39 pub 
226 Directory send OK. 
ftp> cd pub 
250 Directory successfully changed. 
ftp> ls 
227 Entering Passive Mode (192,168,1,10,220,192). 
150 Here comes the directory listing. 
-r--r--r--    1 0        0              14 Sep 17 10:15 CentOS_BuildTag 
dr-xr-xr-x    3 0        0            4096 Sep 17 10:15 EFI 
-r--r--r--    1 0        0             215 Sep 17 10:15 EULA 
-r--r--r--    1 0        0           18009 Sep 17 10:15 GPL 
dr-xr-xr-x    2 0        0            4096 Sep 17 10:16 LiveOS 
dr-xr-xr-x    2 0        0          655360 Sep 17 10:23 Packages 
-r--r--r--    1 0        0            1690 Sep 17 10:23 RPM-GPG-KEY-CentOS-7 
-r--r--r--    1 0        0            1690 Sep 17 10:23 RPM-GPG-KEY-CentOS-Testing-7 
-r--r--r--    1 0        0            2883 Sep 17 10:23 TRANS.TBL 
drwxr-xr-x    2 0        0            4096 Sep 17 10:37 centos7 
dr-xr-xr-x    3 0        0            4096 Sep 17 10:16 images 
dr-xr-xr-x    2 0        0            4096 Sep 17 10:16 isolinux 
dr-xr-xr-x    2 0        0            4096 Sep 17 10:23 repodata 
226 Directory send OK. 
ftp>

20) Record the session of terminal.

You can use “script” command to record your session.

# script -a session1 (Where, “a” is used append the file).

[root@server1 ~]# script -a session1 
Script started, file is session1 
[root@server1 ~]# free -m 
              total        used        free      shared  buff/cache   available 
Mem:            993         103         504           6         384         695 
Swap:          2047           0        2047 
[root@server1 ~]# ps aux | sort -nk +4 | tail 
root      3254  0.0  0.4 140772  5060 ?        Ss   16:51   0:01 sshd: root@pts/0 
root       502  0.0  0.4  46128  4824 ?        Ss   16:08   0:00 /usr/lib/systemd/systemd-udevd 
root       658  0.0  0.4 222116  4148 ?        Ssl  16:09   0:00 /usr/sbin/rsyslogd -n 
root       498  0.0  0.5 194664  5812 ?        Ss   16:08   0:00 /usr/sbin/lvmetad -f 
root         1  0.0  0.6 125816  6508 ?        Ss   16:08   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21 
root       725  0.0  0.7 434632  7908 ?        Ssl  16:09   0:01 /usr/sbin/NetworkManager --no-daemon 
polkitd    894  0.0  1.2 523524 12812 ?        Ssl  16:09   0:00 /usr/lib/polkit-1/polkitd --no-debug 
root      7939  0.0  1.5 110504 15756 ?        S    17:46   0:00 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-enp0s8.pid -lf /var/lib/NetworkManager/dhclient-d280a381-6718-46db-b168-90856f0d9ecf-enp0s8.lease -cf /var/lib/NetworkManager/dhclient-enp0s8.conf enp0s8 
root      1260  0.0  1.6 553060 16300 ?        Ssl  16:09   0:00 /usr/bin/python -Es /usr/sbin/tuned -l -P 
root       664  0.0  2.2 323572 23276 ?        Ssl  16:09   0:02 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 
[root@server1 ~]# who 
root     pts/0        2016-09-23 07:20 (192.168.1.5) 
root     pts/1        2016-09-23 17:46 
[root@server1 ~]# exit 
exit 
Script done, file is session1 
[root@server1 ~]# ls 
anaconda-ks.cfg   linux  linuxnew    log  session1 
[root@server1 ~]#

Note: Session record is recored on “session1” file. You can check using “cat session1” command.