Bash script for printing folder names and their sizes

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Bash script for printing folder names and their sizes
# 1  
Old 10-25-2013
Bash script for printing folder names and their sizes

1. The problem statement, all variables and given/known data:

The task is to create a script that would reproduce the output of 'du' command, but in a different way: what 'du' does is:
Code:
<size> <folder name>

and what is needed is
Code:
<folder name> <size>

We need to show only 10 folders which are the biggest ones.

2. Relevant commands, code, scripts, algorithms:

Nothing special was stated here, so any solution that can still be called a bash script is good enough to be used.

3. The attempts at a solution (include all code and scripts):

I've tried several variants and finally came up with this:

Code:
du $1 2> /dev/null |\
    
    sort -nr -k1 |\

    head -10 |\

    cut -f2  |\

        while read fname

            do

                printf "${fname}\t`du -h --summarize \"$fname\" 2> /dev/null | cut -f1`\n"

            done

It does the job, but works too slowly to be used on practice: for my '/usr' folder it takes 7 minutes and 30 seconds to complete, while the 'du' with filters ('du /usr | sort -nr -k1 | head -10') does everything for 2 minutes and 30 seconds.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Linux System Administration course, ITExpert educational centre, Minsk, Belarus.

Teacher: Viktor Kobzarev.

Thank you for your help!
# 2  
Old 10-25-2013
Wath about the simpler:
Code:
du /usr/ | sort -nr | head -n11 | tail -n10 | while read size path; do echo $path $size;done

Emanuele

P.S. If you want consider also /usr dir (that obviously is the first result):
Code:
du /usr/ | sort -nr | head -n10 | while read size path; do echo $path $size;done

This User Gave Thanks to targzeta For This Post:
# 3  
Old 10-25-2013
Oh, it's a shame I didn't think of reading two variables at once. Your code has finished all the operations in just 2 minutes and 48 seconds!

Thank you very much indeed! Smilie
# 4  
Old 11-20-2013
Shorter

If in case you want to get only the top 10 directories in the current directory, you can try below command,
Code:
du /usr/ | sort -rn | awk 'NR>=2&&NR<=10 { print $2"\t" $1 }'


Last edited by vbe; 11-20-2013 at 05:31 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to list files names and sizes in a directory and output result to the file?

Hi , I'm trying to list the files and output is written to a file. But when I execute the command , the output file is being listed. How to exclude it ? /tmp file1.txt file2.txt ls -ltr |grep -v '-' | awk print {$9, $5} > output.txt cat output.txt file1.txt file2.txt output.txt (8 Replies)
Discussion started by: etldeveloper
8 Replies

2. Shell Programming and Scripting

Script to tar/rsync/rm multiple folder names

hi all, i attach a link with what im trying to do automatically via script but i have some questions i need answering please, bear in mind i am really new to bash scripting, the only thing i know how to do is commands in scripts like cd rm tar rsync cp stuff like that i have mutiple project... (48 Replies)
Discussion started by: robertkwild
48 Replies

3. Shell Programming and Scripting

Bash script for printing folder names and their sizes

Good day, everyone! I'm very new to bash scripting. Our teacher gave us a task to create a script that basically does the same job the 'du' command does, with the difference that 'du' command gives an output in the form of <size> <folder name>and what we need is <folder name> <size>As for... (1 Reply)
Discussion started by: UncleIS
1 Replies

4. Programming

CGI Perl script to execute bash script- unable to create folder

Hi I have a bash script which takes parameters sh /tmp/gdg.sh -b BASE-NAME -n 1 -s /source/data -p /dest/data/archive -m ARC gdg.sh will scan the /source/data and will move the contents to /dest/data/archive after passing through some filters. Its working superb from bash I have... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

5. Shell Programming and Scripting

Bash script for new file in ftp folder

Hello, I'm trying to make a bash script that send me e-mail if there is any new file in my ftp folder. cat inotify.sh #!/bin/sh /usr/bin/inotifywait -e create \ -mrq /home/mrowcp | while read line; do echo -n "$line " >> /var/log/inotify.log echo `date | cut -d " " -f1-4` >>... (3 Replies)
Discussion started by: mrowcp
3 Replies

6. Shell Programming and Scripting

Script to move files with similar names to folder

I have in directory /media/AUDIO/WAVE many .mp3 files with names like: my filename_01of02.mp3 my filename_02of02.mp3 Your File_01of06.mp3 Your File_02of06.mp3 etc.... In the same directory, /media/AUDIO/WAVE, I have many folders with names like 9780743579490 9780743579491 etc.. Inside... (7 Replies)
Discussion started by: glev2005
7 Replies

7. Shell Programming and Scripting

Printing the line number in bash script

Hi, I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened. Thanks, (6 Replies)
Discussion started by: suryaemlinux
6 Replies

8. UNIX for Dummies Questions & Answers

Bash script to rename all files within a folder...

Hi. I don't have any experience with making scripts in bash. I need a simple script to rename all files in a folder to the format file1.avi, file2.avi, file3.avi, and so on..... Please note that the original files have different filenames and different extensions. But they all need to be... (2 Replies)
Discussion started by: dranzer
2 Replies

9. Shell Programming and Scripting

Bash Script duplicate file names

I am trying to write a housekeeping bash script. Part of it involves searching all of my attached storage media for photographs and moving them into a single directory. The problem occurs when files have duplicate names, obviously a file called 001.jpg will get overwritten with another file... (6 Replies)
Discussion started by: stumpyuk
6 Replies

10. Shell Programming and Scripting

Script for file names/sizes

How can I look at a certain directory and list all the file names, locations and sizes of each file in the current directory and all subdirectories? (2 Replies)
Discussion started by: ssmiths001
2 Replies
Login or Register to Ask a Question