开发者

How sort recursively by maximum file size and count files?

I'm beginner i开发者_如何学编程n bash programming. I want to display head -n $1 results of sorting files by size in /etc/*. The problem is that at final search, I must know how many directories and files has processed.

I compose following code:

#!/bash/bin
let countF=0;   
let countD=0;
for file in $(du -sk /etc/* |sort +0n | head $1); do
 if [ -f  "file" ] then
   echo $file;
   let countF=countF+1;
else if [ -d  "file" ] then 
   let countD=countD+1;
fi
done
echo $countF
echo $countD

I have errors at execution. How use find with du, because I must search recursively?


#!/bin/bash      # directory and program reversed
let countF=0     # semicolon not needed (several more places)
let countD=0
while read -r file; do
    if [ -f  "$file" ]; then     # missing dollar sign and semicolon
        echo $file
        let countF=countF+1  # could also be: let countF++
    else if [ -d  "$file" ]; then     # missing dollar sign and semicolon
        let countD=countD+1
    fi
done < <(du -sk /etc/* |sort +0n | head $1)    # see below
echo $countF
echo $countD

Changing the loop from a for to a while allows it to work properly in case filenames contain spaces.

I'm not sure what version of sort you have, but I'll take your word for it that the argument is correct.


  1. It's #!/bin/bash not #!/bash/bin.

  2. I don't know what that argument to sort is supposed to be. Maybe you meant sort -r -n?

  3. Your use of head is wrong. Giving head file arguments causes it to ignore its standard input, so in general it's an error to both pipe something to head and give it a file argument. Besides that, "$1" refers to the script's first argument. Did you maybe mean head -n 1, or were you trying to make the number of lines processed configurable from an argument to the script: head -n"$1".

  4. In your if tests, you're not referencing your loop variable: it should read "$file", not "file".

  5. Not that the bash parser cares, but you should try to indent sanely.


#!/bin/bash      # directory and program reversed
let countF=0     # semicolon not needed (several more places)
let countD=0
while read -r file; do
    if [ -f  "$file" ]; then     # missing dollar sign and semicolon
        echo $file
        let countF=countF+1  # could also be: let countF++
    else if [ -d  "$file" ]; then     # missing dollar sign and semicolon
        let countD=countD+1
    fi
done < <(du -sk /etc/* |sort +0n | head $1)    # see below
echo $countF
echo $countD

I tried instead of file variable the /etc/* but I don't see a result. the idea is to sort all files by size from a directories and subdirectories and display $1 results ordered by size of the files. In this process I must know how many files and dirs contains the directory where I did the search.


Ruby(1.9+)

#!/usr/bin/env ruby    

fc=0
dc=0
a=Dir["/etc/*"].inject([]) do |x,f|
    fc+=1 if File.file?(f)
    dc+=1 if File.directory?(f)
    x<<f
end
puts a.sort
puts "number of files: #{fc}"
puts "number of directories: #{dc}"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜