Thursday, December 23, 2010

Sorting contents of two files in Linux

You got 2 files which contains a list of servernames (as shown below) and want to compare the entries between two files and get the list of server names which aren’t present in both files:

1st file:
server_a
server_b
server_c
server_d
server_e

2nd file:
server_c
server_d
server_g
server_a

Output should be:

server_b
server_g
server_e



Solution 1:  # sort file1 file2 | uniq –u

Solution 2:

# sort file1 > file1.sorted
# sort file2 > file2.sorted
# comm -3 file1.sorted file2.sorted

Finding total size of files owned by a particular user

Syntax:

# find <pathname> -user <username> -ls | awk '{sum += $8} END {printf "Total size: %8.4f MB\n", sum/1024/1024}'

Example:

[root@nasllm01-ih ~]# find /nasllm-ih -user rram -ls | awk '{sum += $8} END {printf "Total size: %8.4f MB\n", sum/1024/1024}'                   
Total size: 958.7282 MB
[root@nasllm01-ih ~]#


Validation:

Here I have created a folder /root/ashok consists files owned by user “adevaraju” of total size: 160 MB.

[root@oralsb11-new ~]# ls -lRh /root/ashok
/root/ashok:
total 21M
drwxr-xr-x 3 adevaraju root 4.0K Dec 14 06:30 d1
drwxr-xr-x 2 adevaraju root 4.0K Dec 14 06:35 d2
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:21 file1
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:22 file4

/root/ashok/d1:
total 21M
drwxr-xr-x 2 adevaraju root 4.0K Dec 14 06:35 dx
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:22 file2
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:22 file3

/root/ashok/d1/dx:
total 21M
-rw-r--r-- 1 adevaraju root 10M Dec 14 06:30 file5
-rw-r--r-- 1 adevaraju root 10M Dec 14 06:30 file6

/root/ashok/d2:
total 101M
-rw-r--r-- 1 adevaraju root 100M Dec 14 06:30 file7
[root@oralsb11-new ~]#
 [root@oralsb11-new ~]# du -sh ashok/
161M    ashok/

[root@oralsb11-new ~]# find /root/ashok -user adevaraju -ls | awk '{sum += $7} END {printf "SUM: %8.4f MB\n", sum/1024/1024}'
SUM: 160.0156 MB
[root@oralsb11-new ~]#


PS: Please note there is a difference in AWK statement. The Field separator value  will be one count more for Domain users since it has an extra space in-between (domain users).

[root@oralsb11-new ~]# find /root/ashok -user adevaraju -ls
15073319    4 drwxr-xr-x   4 adevaraju root         4096 Dec 14 06:22 /root/ashok
15073320    4 drwxr-xr-x   3 adevaraju root         4096 Dec 14 06:30 /root/ashok/d1
15073323 10256 -rw-r--r--   1 adevaraju root     10485760 Dec 14 06:22 /root/ashok/d1/file2
.
.
[root@oralsb11-new ~]# find /home/tcprod/adevaraju/ -user adevaraju -ls
9371731    4 drwxr-xr-x   4 adevaraju domain users     4096 Dec 13 12:21 /home/tcprod/adevaraju/
9371732    4 -rw-r--r--   1 adevaraju domain users       33 Oct 14 09:09 /home/tcprod/adevaraju/.bash_logout
9372649 1420 -rw-r--r--   1 adevaraju domain users  1446232 Dec 10 09:51 /home/tcprod/adevaraju/lshw-2.15-1.el5.rf.x86_64.rpm
.
.

Popular Posts

About Me

My photo
I have started this blog to share my work experience and spread some smart solutions on Linux to Internet community. I'm hoping more people will get benefited from this blog. Brief about me: I have 14+ years experience working as System Admin and currently work with VMware.