Size of file and directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Size of file and directory
# 1  
Old 10-13-2009
Wrench Size of file and directory

Hello. I do have a problem.
The statement sounds like this: Given a directory, find all subdirectories (regardless of depth) which contain a file that has more than a half of the size of the respective subdirectory.

I've tried to solve this in many ways, but all I came up with is half solutions.

One would be trying this:
Code:
find folder -type f -size +x

where I would search for the size of each file, x being a variable, but this should be applied to each file.

Another one, which seems not to work, would be this:

Code:
for i in $(ls $1); do 
if [ -d $i ]; then 
echo $i; 
fi; 
done

but still, i should search for each file and compare it with the directory.

trying
Code:
ls -R folder

would be a solution, but I can't connect it with another command.

or this one:
Code:
for i in `find $1 type -d'
do
        for j in `find $i type -f -size +(size($i))` // in blocks
         do
                echo j;
         done
done

where, size($i) would be the size converted in blocks, and I didn't know how to do that.

Thanks

Last edited by WorkOfArt; 10-13-2009 at 09:23 AM..
# 2  
Old 10-13-2009
It is great to see your question, i would greatly appreciate it.

Here is the answer for you.
Code:
ls -lh
total 28K
-rw------- 1 thegeek learner  18K Oct 10 05:52 feed
-rw------- 1 thegeek learner 2.5K Oct 10 05:51 feedbacks
-rw------- 1 thegeek learner   32 Oct 10 05:51 tttt

So only the feed is the file whose size is 18k which is more than the half of the dir size which is 28K.

To get that, following is the script.,
Code:
for i in `find . -type d`
do 
    echo $i
    dirsize=`du -s $i | cut -f1`
    echo $dirsize
    find $i -size +$dirsize -type f
done

If it requires a little edit for your requirement, go a head and do it. But this would help you in achieving the core part of what you would want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort by file/directory size

OS : RHEL 6.6 I want to list the files/directories sorted (Ascending or Desceding) by their size. As you can see in the below example, du command doesn't sort by size. In Linux world, is there any other command or workaround using du command to list the files/directories sorted by their... (6 Replies)
Discussion started by: John K
6 Replies

2. Programming

Size of a directory or a file

Hello, Here is my code: :~$ truncate -s 16M MyTestFile.txt :~$ du -h MyTestFile.txt 4,0K MyTestFile.txt Q1: Please why du -h does not work in this case ? Q2: Other than "du -h", how can i get the size of a directory (using linux command) Thanks a lot. Best Regards. (2 Replies)
Discussion started by: chercheur111
2 Replies

3. 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

4. Shell Programming and Scripting

How to delete some of the files in the directory, if the directory size limits the specified size

To find the whole size of a particular directory i use "du -sk /dirname".. but after finding the direcory's size how do i make conditions like if the size of the dir is more than 1 GB i hav to delete some of the files inside the dir (0 Replies)
Discussion started by: shaal89
0 Replies

5. 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

6. Shell Programming and Scripting

get file size by date for a directory

Good day Probably a simple script though I am new to attempting to script. I have a directory that I would like to get the size of the files and number of files for each date ie 14 Sep 669 files 1.8g 12 Sep 221 files 500mb Any ideas? Thanks (1 Reply)
Discussion started by: ibaboomer
1 Replies

7. UNIX for Dummies Questions & Answers

Sort by size, then list file in each directory

Hi, I have directories with name like: aaa bbb ccc ... I would like to to see which directories are the largest and then list the files within each. I have success using: du -ks * | sort -rin | head -n 20 which gives me an output like: 120 bbb 27 ccc 3 aaa ... I would like... (3 Replies)
Discussion started by: ChatPerdu
3 Replies

8. Shell Programming and Scripting

How to compare size of two file which is in one directory

I have two file in a Directory.I want a script which will compare the Size of Two file. Can Anyone Help me on this: linasplg11:/opt/dataout/kk/linasplg11 # cat size -rwxrwxrwx 1 root root 16658 Jan 8 13:58 lina_IP_SIP_1231325621210.xml -rwxr-xr-x 1 root root 16672 Jan 8 14:30... (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies

9. Shell Programming and Scripting

How to know size of file in a directory

Hi, I have to directory /usr/inbound ------------- 10900.txt 10889.txt 109290202.txt I need to create inbound directory and i need to know size of these files one by one if file size is zero i need to print message like "empty file" Please help me how to solve this thanks krish. (1 Reply)
Discussion started by: kittusri9
1 Replies

10. Shell Programming and Scripting

How to calculate file's size in directory and subdirectory

Hi, I have written one script to calculate total space of all file in one directory, ignoring subdirectory, it works fine. Now, I've been trying to calculate all files which includes files in any subdirectories. I use recursive function to do this, but it can work only if there is only one... (4 Replies)
Discussion started by: KLL
4 Replies
Login or Register to Ask a Question