Sorting riles by size with -nr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting riles by size with -nr
# 1  
Old 09-03-2013
Sorting riles by size with -nr

Hello everyone,

I am working on this bash script and I can't seem to get this last part right. It's really trivial but I must not be doing something right. I want to sort files by their size in reverse order (largest to smallest). My script right now sort of tdoes this but there is one file that is larger than the rest and it just shows up in the middle and I don't understand why this is. I'm not that experience with bash so maybe I'm doing something really stupid. Any help or suggestions on how to fix this would be greatly appreciated!

Here is my code for the bash script: Here I'm getting all files with the .tar.gz extension in the current directory and below it.

Code:
#! /bin/bash

for FILE in `find . -name *.tar.gz | sort -nr`
do
    echo `du -b $FILE` 
 
done

# 2  
Old 09-04-2013
The following does what you want
Code:
 du -ab . | sort -nr | grep -i '.tar.gz$'


Last edited by MR.bean; 09-04-2013 at 12:06 AM..
This User Gave Thanks to MR.bean For This Post:
# 3  
Old 09-04-2013
After more investigation it looks like I just answered my own question. It can just be a one line in a bash script:Smilie

Code:
echo -e "BYTES   | FILENAME\n"
find . -name *.tar.gz -printf "%s %p\n" | sort -rn

Also, thank you Mr. Bean for your answer.
# 4  
Old 09-04-2013
Code:
find . -name '*tar.gz'  -exec ls -ls {} \; | sort -rn

ls -s gives the size in blocks as column #1.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Sorting files by size in another directory

1. The problem statement, all variables and given/known data: I'm trying to use a directory path to enter a new directory and sort the files there. I'm using the language C with a system call in Unix to sort the files from smallest to largest. 2. Relevant commands, code, scripts, algorithms:... (1 Reply)
Discussion started by: TedFTW
1 Replies

2. UNIX for Dummies Questions & Answers

Ls directory size reporting byte size instead of file count

I have been searching both on Unix.com and Google and have not been able to find the answer to my question. I think it is partly because I can't come up with the right search terms. Recently, my virtual server switched storage devices and I think the problem may be related to that change.... (2 Replies)
Discussion started by: jmgibby
2 Replies

3. Solaris

/tmp size is less whereas size allocated to swap is more

Hi, the /tmp size is less whereas the size allocated to swap is quite big. how to increase the size of /tmp - #: swap -l swapfile dev swaplo blocks free /dev/md/dsk/d20 85,20 8 273096 273096 #: swap -s total: 46875128k bytes allocated + 2347188k reserved =... (2 Replies)
Discussion started by: psb74
2 Replies

4. UNIX for Advanced & Expert Users

Physical disk IO size smaller than fragment block filesystem size ?

Hello, in one default UFS filesystem we have 8K block size (bsize) and 1K fragmentsize (fsize). At this scenary I thought all "FileSytem IO" will be 8K (or greater) but never smaller than the fragment size (1K). If a UFS fragment/blocksize is allwasy several ADJACENTS sectors on disk (in a ... (4 Replies)
Discussion started by: rarino2
4 Replies

5. Red Hat

Sorting folder size not working

I am using du -h --max-depth=2 to get list of folders by size upto 2 levels down. Problem is I am not able to sort them in max folder size. Normally this can be achieved by using du -k | sort -nr * but I can't use it here since it conflicts (the -s argument) with the --max-depth=2 argument. ... (1 Reply)
Discussion started by: rockf1bull
1 Replies

6. Shell Programming and Scripting

Script to read file size and send email only if size > 0.

Hi Experts, I have a script like $ORACLE_HOME/bin/sqlplus username/password # << ENDSQL set pagesize 0 trim on feedback off verify off echo off newp none timing off set serveroutput on set heading off spool Schemaerrtmp.txt select ' TIMESTAMP COMPUTER NAME ... (5 Replies)
Discussion started by: welldone
5 Replies

7. Shell Programming and Scripting

The scripts not able to make the file to size 0, every times it go back to its original size

#!/bin/sh ########################################################################################################## #This script is being used for AOK application for cleaning up the .out files and zip it under logs directory. # IBM # Created #For pdocap201/pdoca202 .out files for AOK #1.... (0 Replies)
Discussion started by: mridul10_crj
0 Replies

8. Shell Programming and Scripting

Sorting files by size. No ls.

I've got an assignment where I've made a script that searched and found files that meet certain requirement. (Files that are a size that is from "x" to "y" in size and has a certain name). The script sends the output to a file that gathers the info. The problem is I'd like to sort what's... (3 Replies)
Discussion started by: BobbyTee
3 Replies

9. Solaris

Directory size larger than file system size?

Hi, We currently have an Oracle database running and it is creating lots of processes in the /proc directory that are 1000M in size. The size of the /proc directory is now reading 26T. How can this be if the root file system is only 13GB? I have seen this before we an Oracle temp file... (6 Replies)
Discussion started by: sparcman
6 Replies

10. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies
Login or Register to Ask a Question