How to sort the files by size and based subdirectory un UNIX?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to sort the files by size and based subdirectory un UNIX?
# 1  
How to sort the files by size and based subdirectory un UNIX?

I have the below input data in a file and need to get the output as mentioned below. Need to sort the data by size(Asc/des)/by subdirectory

Below is the input which is there in a file:
Code:
120     /root/path2/part-00000-d3700305-428d-4b13-8161-42051f4ac5ed-c000.json
532     /root/path1/part-00000-9aa3e423-084b-406b-8c75-d50c369f1943-c000.json
100     /root/path1/Telephone-bill-September-2019-89051_path.doc
72      /root/path2/BishnuPrasadRout[5_0].doc
4       /root/path2/survey.csv
4       /root/path2/hello.c
4       /root/path1/pages.txt
4       /root/path1/hadoopexam.txt

Expected Output
Code:
120     /root/path2/part-00000-d3700305-428d-4b13-8161-42051f4ac5ed-c000.json
72      /root/path2/BishnuPrasadRout[5_0].doc
4       /root/path2/survey.csv
4       /root/path2/hello.c
532     /root/path1/part-00000-9aa3e423-084b-406b-8c75-d50c369f1943-c000.json
100     /root/path1/Telephone-bill-September-2019-89051_path.doc
4       /root/path1/pages.txt
4       /root/path1/hadoopexam.txt


Last edited by vgersh99; 11-15-2019 at 01:32 PM.. Reason: adjusted code tags - code/data samples only.
# 2  
Hi, try:
Code:
sort -t/ -k2,3r -k1,1rn -k4 file

Code:
120     /root/path2/part-00000-d3700305-428d-4b13-8161-42051f4ac5ed-c000.json
72      /root/path2/BishnuPrasadRout[5_0].doc
4       /root/path2/hello.c
4       /root/path2/survey.csv
532     /root/path1/part-00000-9aa3e423-084b-406b-8c75-d50c369f1943-c000.json
100     /root/path1/Telephone-bill-September-2019-89051_path.doc
4       /root/path1/hadoopexam.txt
4       /root/path1/pages.txt

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Yes! its worked. Thankyou
Could please explain the soultion
# 4  
The sort options mean the following,
-t/ : split fields on the / character
-k2,3r : First, reverse (r) sort on the string that consists of fields 2 to 3 , which is root/path[12] in the example
-k1,1rn : Then Do a reverse numeric sort on the first field which is the file size
-k4 : Everything sofar being equal forward sort the remaining fields (file names).

Hope this helps..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #202
Difficulty: Easy
At compile time, the interpreter parses Perl code into a recursive flat-space module.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to create subdirectory based on match between two files

In the below awk I am trying to mkdir based of an exact match between file2 line starting with R_2019.... and file1 line starting with R_2019. When a match is found there is a folder located at /home/cmccabe/run with the same name as the match where each $2 in file1 is a new subdirectory in that... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Size (Sort files using ...)

-rw-r--r-- 1 oracle oinstall 4 Jan 17 16:23 a -rw-r--r-- 1 oracle oinstall 212 Jan 17 17:51 amar -rw-r--r-- 1 oracle oinstall 32 Jan 17 17:30 b -rw-r--r-- 1 oracle oinstall 246 Jan 17 15:40 h1.tar.gz Hi, I want combination of linux command to sort out the line which has 32k size. ... (5 Replies)
Discussion started by: karthick nath
5 Replies

3. Shell Programming and Scripting

Sort based on Multiple Columns in UNIX

Hi, I would like to sort a list in different ways: 1> Unique based on Field 1 with highest Field 4 For Instance Input: 1678923450;11112222333344;11-1x;2_File.xml 1678923450;11112222333344;11-1x;5_File.xml 1234567890;11113333222244;11-1x;3_File.xml Output: ... (7 Replies)
Discussion started by: DevendraG
7 Replies

4. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

5. Shell Programming and Scripting

Sort and extract based on two files

Hi, I am having trouble sorting one file based on another file. I tried the grep -f function and failed. Basically what I have is two files that look like this: File 1 (the list) gh aba for hmm File 2 ( the file that needs to be sorted) aba 2 4 6 7 for 2 4 7 4... (4 Replies)
Discussion started by: phil_heath
4 Replies

6. Shell Programming and Scripting

Search for file and size subdirectory as well

Hi I made this code to search in directory for file and size How can I remodel it to seach in the sub direcotry as well Thanks #!/bin/bash echo -n "Enter: " read var if then echo "Directory exists: ${var}" size=`du -hs "${var}"` echo The size of the current folder is... (4 Replies)
Discussion started by: lio123
4 Replies

7. Shell Programming and Scripting

Find files of specific size excluding search in a subdirectory

Hi All, I was exploring find command and came across -prune option which would exclude search in a mention subdirectory. My quesry is to search all files more that 100 MB size but exclude search in a subdirectory. I am using below command,but somehow it is not working. Can anybody help me... (6 Replies)
Discussion started by: usha rao
6 Replies

8. Shell Programming and Scripting

sort mail based on subject in unix.

Hi, I have a complicated requirement.I need to search for a particular pattern in the subject of incoming mail to a set of users and need to save the content of the mail in a file which will be uploaded to a file server. I have a perl script to do the upload.I need a script to sort the mail... (2 Replies)
Discussion started by: sriram.s
2 Replies

9. UNIX for Dummies Questions & Answers

sort files by size

Is there a way to sort files by size using the ls command? thanks in advance (1 Reply)
Discussion started by: AMD
1 Replies

10. Shell Programming and Scripting

File size limitation of unix sort command.

hi , iam trying to sort millions of records which is delimited and i cant able to use sort command more than 60 million..if i try to do so i got an message stating that "File size limit exceeded",Is there any file size limit for using sort command.. How can i solve this problem. thanks ... (7 Replies)
Discussion started by: cskumar
7 Replies

Featured Tech Videos