Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I delete Directories which are smaller than, say, 1000 KB?

I already have a file list from the command:

du -sk * | sort -n > sort.txt

but I don't know how to proceed.

++++++++

Update:

du -sk */ | awk 'BEGIN { FS="\t" }; { if($1 < 1000) printf "%s\0",$0 }' | sort -nrz | hexdump -C | head -n 10


00000000  31 36 09 32 20 50 69 73  74 6f 6c 73 20 46 74 2e  |16.2 Pistols Ft.|
00000010  20 54 2d 50 61 69 6e 20  26 20 54 61 79 20 44 69  | T-Pain & Tay Di|
00000020  7a 6d 2f 33 30 34 09 41  63 65 20 41 74 6b 69 6e  |zm/304.Ace Atkin|
00000030  73 2f 38 09 41 69 72 6d  61 74 65 20 66 65 61 74  |s/8.Airmate feat|
00000040  2e 20 4d 61 72 69 73 68  6b 61 20 50 68 69 6c 6c  |. Marishka Phill|
00000050  69 70 73 2f 31 36 09 41  6c 65 78 61 6e 64 65 72  |ips/16.Alexander|
00000060  20 4b 6f 77 61 6c 73 6b  69 20 66 65 61 74 2e 20  | Kowalski feat. |
00000070  42 61 72 63 61 20 42 61  78 61 6e 74 2f 35 34 34  |Barca Baxant/544|
00000080  09 41 6e 64 72 65 cc 81  73 20 66 65 61 74 20 44  |.Andre..s feat D|
00000090  4a 20 4d 69 6e 78 2f 31  36 09 41 73 69 61 20 43  |J Minx/16.Asia C|

the output of

du -sk * | awk 'BEGIN { FS="\t" }; { if($1 < 1000) print $2 }'

is something like:

dirname
other dirname
other dirname with special chars / . - +
share|improve this question

2 Answers 2

up vote 1 down vote accepted

The problem with whitequark's answer is that grep is adding nulls, but awk is taking them away. Also, you may want to restrict your du command to directories-only since that is what you say you want to measure and delete based on size. Adding a slash after the asterisk does this.

du -sk */ | awk 'BEGIN { FS="\t" }; { if($1 < 1000) printf "%s\0",$2 }' | xargs -0 rm -rf
share|improve this answer
    
now I get: File name too long when I pipe the output of du -sk */ | awk 'BEGIN { FS="\t" }; { if($1 < 1000) printf "%s\0",$2 }' the file has 1 line with more than 5000 chars. –  harp Jun 14 '10 at 19:01
    
@harp: What does du -sk */ | wc -L tell you? –  Dennis Williamson Jun 14 '10 at 19:38
    
du -sk */ | wc -l = 6250 –  harp Jun 14 '10 at 19:44
    
@harp: No, a capital "L". –  Dennis Williamson Jun 14 '10 at 20:23
    
@Dennis Williamson: usage: wc [-clmw] [file ...] no output –  harp Jun 14 '10 at 20:37

Try du -sk * | awk 'BEGIN { FS="\t" }; { if($1 < 1000) print $2 }' | grep '' -Z | xargs -0 -s 1024 rm -rf. Be careful with rm!

share|improve this answer
    
If you don't have a ton of directories to delete, I would throw on a "-i" which will make rm ask you if each directory is okay to delete before deleting it. –  Jarvin Jun 14 '10 at 16:16
    
@Dan: if you don't have a ton of them to delete, you'd rather delete them by hand... selecting them from sorted list of some GUI would be both easier and safer. –  whitequark Jun 14 '10 at 16:19
    
one problem here. when I enter du -sk * | awk '{ if($1 < 1000) print $2 }' the output doesn't show the directory names correctly. I have directory names with spaces and special characters. So the command shows "dirname" instead of "dirname correct bla bla". –  harp Jun 14 '10 at 16:45
    
Fixed that and updated answer. –  whitequark Jun 14 '10 at 16:52
    
thx whitequark. fantastic. –  harp Jun 14 '10 at 16:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.