Find the answer to your Linux question:
Results 1 to 3 of 3
Hi all, I needed help in writing a shell script for the following.. Shell function that takes a valid directory name as an argument and recursively descends all the subdirectories, ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    20

    needed help in writing a script to find file size?

    Hi all,

    I needed help in writing a shell script for the following..
    Shell function that takes a valid directory name as an argument and recursively
    descends all the subdirectories, finds the maximum length of any file in that
    hierarchy and writes this maximum value to the standard output.

    Code:
    for i in $*;do
    x=`ls -lR somedir |awk '{print $5}'|sort -n|tail -1`
    echo $x
    but this gives me the dir value which has the biggest size....not the file.
    Also if i have subdirs then its not going in search of the sizes for these subdirs?

    Could you pls help?

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    try
    Code:
    #!/bin/bash
    for i in $*; do
            x=`find $i -type f -ls | sort -k 7 -r -n | awk 'NR==1 {print $7}'`
    done
    This script has been tested to work on Ubuntu & OpenSuSE.

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    I don't like parsing the ls output like that.

    A command named "stat" exists for a reason. This is the way I'd do it:

    Code:
    find /path/to/ -type f | while read file; do stat -c "%s" "$file"; done | sort -g | tail -n1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...