LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-25-2010, 07:04 AM   #1
celembor
LQ Newbie
 
Registered: Nov 2009
Location: Valencia, Spain
Posts: 11

Rep: Reputation: 1
Question shell command amount space in disk for mp3


Hi everybody,

We use a SLES 10 SP2 file server. This file server has all type of files. We want to know what is the amount of space used by mp3 files. What we need to know is the total space in disk of mp3 files. I've been testing du command, and find command, but with no satisfactory results. Does anybody know how to do this?

thanks
 
Old 06-25-2010, 07:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

(untested)

Something like this: find / -type f -iname ".mp3" -exec du -ch {} \;

Hope this helps.
 
Old 06-25-2010, 07:35 AM   #3
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
You'll need to use a combination of find, xargs and du.

Code:
find . -type f -name '*.mp3' -print0 | xargs -0 du -a
In the find command
  • '.' indicates that you're finding files from the current directory. Find does a recursive search, so you'll find files in all sub directories.
  • '-type f' indicates that you want to search for file names, not directory names.
  • '-name '*.mp3'' indicates which files you want to search for
  • '-print0' indicates that you want the list to be delimited by NUL characters (i.e. ascii 0). By default, find emits a list that is delimited by whitespace, and this doesn't allow us to search for file names which contain spaces.
xargs is used to pass large lists of files to other commands. the '-0' option parses a NUL delimited list.

du -a prints the name and size of individual files, followed by a summary (by default, du prints the size of directories, followed by a summary).

I highly recommend learning more about find and xargs. They come in incredibly handy at times.
 
Old 06-25-2010, 07:41 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
@bartonski: Add du's -c flag too, this will show the grand total of all found files.
 
Old 06-25-2010, 07:46 AM   #5
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by druuna View Post
Hi,

(untested)

Something like this: find / -type f -iname ".mp3" -exec du -ch {} \;

Hope this helps.
This has a couple of nice features

  • find is starting at the root directory, which is probably what you want.
  • -iname ".mp3" does a case insensitive search, which is probably what you want.
  • the '-c' option to 'du' explicitly requests a grand total, which is good
  • the '-h' option to 'du' requests human readable sizes (KB, MB, GB) rather than a byte count or block count. Again, this is probably what you want.
The use of 'exec' within find means that find will execute du against each file that it finds. This has a couple of problems
  • it spawns a new process for each file
  • it will calculate the grand total for each file.
I would go with a combination of our commands:
Code:
find / -type f -iname '*.mp3' -print0 | xargs -0 du -ach
 
Old 06-25-2010, 07:56 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

@bartonski:
Quote:
The use of 'exec' within find means that find will execute du against each file that it finds. This has a couple of problems
it spawns a new process for each file
it will calculate the grand total for each file.
I would go with a combination of our commands:

find / -type f -iname '*.mp3' -print0 | xargs -0 du -ach
I totally agree!

Hope this helps.

Last edited by druuna; 06-25-2010 at 10:39 AM. Reason: Deleted some BS
 
Old 06-26-2010, 08:29 AM   #7
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
wrapping this up with a bow...

I thought that this was a nifty thing to have in my toolbox, so I wrapped a shell function around it:

Code:
dufiletype() { find / -type f -iname "$1" -print0 | xargs -0 du -ach; }
In your case, you would call it like this:

Code:
dufiletype '*.mp3'
You may want to run this as root, otherwise, you'll get a lot of complaints as du tries to access directories that you don't have read access to... alternatively, you could redirect stderr to /dev/null (presumably root doesn't have a lot of mp3 files in read protected directories).
 
Old 06-29-2010, 01:32 AM   #8
celembor
LQ Newbie
 
Registered: Nov 2009
Location: Valencia, Spain
Posts: 11

Original Poster
Rep: Reputation: 1
Thank you for your posts. In this days I made my own command, totally different:

ls -lR /media/rsync | grep mp3 | awk '{ SUM += $5} END { print SUM/1024/1024 }'

with this script I get 12 GB, and with

find /media/rsync -type f -iname '*.mp3' -print0 | xargs -0 du -ach

I get 4.9 GB

Why do you think this happens?

thanks
 
Old 06-29-2010, 12:50 PM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Finally had some time to look at this one....

This: s -lR /media/rsync | grep mp3 | awk '{ SUM += $5} END { print SUM/1024/1024 }' should be

ls -lR /media/rsync | grep -i ".mp3" | awk '{ SUM += $5} END { print SUM/1024/1024/1024 }'

This will look for .mp3 (case insensitive, the -i switch), shown in Gigabytes (from original to k, to M and to G).

But that doesn't solve the problem.....

If I try both commands on my box I get different results as well (the ls command shows the correct results). I'm getting strange results with the find command (depending on where I look and what I look for I get good or bad results).

I've tried a few things but cannot come up with a reason yet........ I'll have another look tomorrow and will let you know if I do find an explanantion.
 
Old 07-10-2010, 03:30 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi again,

Just remembered this thread and found a possible answer (well, pixellany did to be honest).

Have a look at this thread and pixellany's answer: du vs df -- Huge difference (right here at LQ).

Hope this helps.
 
Old 07-12-2010, 04:36 AM   #11
celembor
LQ Newbie
 
Registered: Nov 2009
Location: Valencia, Spain
Posts: 11

Original Poster
Rep: Reputation: 1
ok, thanks for your help
 
Old 07-12-2010, 04:41 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
You're welcome
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What I a good amount of disk space to have free? abefroman Linux - Server 2 01-08-2010 03:17 PM
Allocating fixed amount of disk space to the clients shishirkotkar Linux - Newbie 1 01-03-2008 09:44 AM
command for finding out disk space used bhomass Debian 2 12-28-2005 11:52 PM
Finding Disk Space, Using a command. amer_58 Linux - Newbie 2 02-14-2005 03:43 AM
Shell command to see free space linksocc Linux - Newbie 4 09-26-2003 11:57 PM


All times are GMT -5. The time now is 03:22 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration