Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    313
    Rep Power
    30

    Another Easy One - How To Count Files In Directory?


    Still learning UNIX...here is another easy question that I haven't been able to find the answer to.

    How do you count or display the number of files in a directory? I know with ls -s you can find out the size in kb, but I'm curious to know the file count. I recently transferred a directory from another server and wanted to make sure the file count was the same on the new server.

    Everything I can see with the ls command seems to deal with file sizes though, rather than how many there are.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    177
    Rep Power
    234
    Code:
    # ls -l | awk '!/^d/{print }' | wc -l
    note this does not count hidden files. to count hidden files, you use the -a switch
    Last edited by ghostdog74; April 28th, 2007 at 09:36 PM.
  4. #3
  5. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    # ls -l | awk '!/^d/{print }' | wc -l

    Eek! Try this:

    Code:
    ls -1 | wc -l
    Which means:
    ls: list files in dir
    -1: (that's a ONE) only one entry per line. Change it to -1a if you want hidden files too
    | : pipe output onto...
    wc: "wordcount"
    -l: (that's an "L" for Linux), count lines.

    --Simon
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    313
    Rep Power
    30
    Thanks.

    By the way - I've run into these problems a few times when seeing commands and reading them in the book I'm reading (although it usually explains which to pick)...

    What is the best way to know if you are supposed to type a 1 or an l, or an O or an 0?

    A lot of the UNIX stuff is written in monotype, as that's how it is displayed. The only unfortunate thing is they all seem to look the same in monotype. When reading man pages or trying to use flags, it's very hard to tell and can be frustrating.

    Is there a good way to get around this? Any tips or advice? If I type the wrong one by accident should I worry I will wreak havoc somehow?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    177
    Rep Power
    234
    Originally Posted by SimonGreenhill
    Eek! Try this:

    Code:
    ls -1 | wc -l
    Which means:
    ls: list files in dir
    -1: (that's a ONE) only one entry per line. Change it to -1a if you want hidden files too
    | : pipe output onto...
    wc: "wordcount"
    -l: (that's an "L" for Linux), count lines.

    --Simon
    well, i have thought of that too..but OP did not indicate he wants to count subdirectories. ls will list subdirectories too...unless of course OP wants to include subdirectories
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    177
    Rep Power
    234
    Originally Posted by Superman859
    Thanks.

    By the way - I've run into these problems a few times when seeing commands and reading them in the book I'm reading (although it usually explains which to pick)...

    What is the best way to know if you are supposed to type a 1 or an l, or an O or an 0?

    A lot of the UNIX stuff is written in monotype, as that's how it is displayed. The only unfortunate thing is they all seem to look the same in monotype. When reading man pages or trying to use flags, it's very hard to tell and can be frustrating.

    Is there a good way to get around this? Any tips or advice? If I type the wrong one by accident should I worry I will wreak havoc somehow?
    -l will include the "total" line. so its best to just ls or ls -1.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    313
    Rep Power
    30
    Yes, I've learned ls -l . However, it took me a few moments to figure out which one to use. If I haven't previously learned it am in the processing of learning it, I have trouble knowing which it is.

    So it's mainly for all the new stuff I'm learning.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    Prague, Czech Republic
    Posts
    869
    Rep Power
    71
    Using ls is simple and fast, but you may try to count files or directories recursively. The you'd better use find which includes even the hidden files as a default. With ls you have to use ls -la .....
    Code:
    [zby@DDFF Text-PDF-0.29]$ find . -type d 
    .
    ./scripts
    ./blib
    ./blib/man3
    ./blib/script
    ./blib/lib
    ./blib/lib/auto
    ./blib/lib/auto/Text
    ./blib/lib/auto/Text/PDF
    ./blib/lib/Text
    ./blib/lib/Text/PDF
    ./blib/arch
    ./blib/arch/auto
    ./blib/arch/auto/Text
    ./blib/arch/auto/Text/PDF
    ./lib
    ./lib/Text
    ./lib/Text/PDF
    ./examples
    [zby@DDFF Text-PDF-0.29]$ find . -type d |wc -l
    19
    [zby@DDFF Text-PDF-0.29]$ 
    [zby@DDFF Text-PDF-0.29]$ 
    [zby@DDFF Text-PDF-0.29]$ find . -type f |wc -l
    84
    [zby@DDFF Text-PDF-0.29]$
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    And just to prove there are N+1 ways of doing things in Unix, given N programmers ...

    To count JUST files in immediate directory
    Code:
    ls -1p | grep -vc "/$"
    The "/$" in the grep is looking for lines ending in /, which the -p flag of ls will put in for directories. The -v flag of the grep will invert the search, so only lines NOT ending in / (i.e., all non directories) will be listed, and the -c of the grep means count.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0
    Originally Posted by SimonJM
    And just to prove there are N+1 ways of doing things in Unix, given N programmers ...

    To count JUST files in immediate directory
    Code:
    ls -1p | grep -vc "/$"
    The "/$" in the grep is looking for lines ending in /, which the -p flag of ls will put in for directories. The -v flag of the grep will invert the search, so only lines NOT ending in / (i.e., all non directories) will be listed, and the -c of the grep means count.
    Hi Simon i was looking this command for a long time
    i m not a unix user but just need to do something with it.

    here is my problem ;
    i need to display the file names one by one order by date,
    i mean i m using BO Data Integrator tool to get files name from unix.And i have a loop in BODI tool,

    my loop does this ;
    1. count files in directory ( to a variable : $count)
    2. get file name one by one ( to a variable : $filename)

    suppose you have 3 files in that directory
    /home/dummy.csv
    /home/dummy.xls
    /home/dummy.txt

    i need a command like (its just my imagenation );
    :> getfilename(1)
    dummy.csv
    :> getfilename(2)
    dummy.xls
    :> getfilename(3)
    /home/dummy.txt

    thats what i need
    i hope i could explain detaily
    Thx
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Well, the command been here for a long time, too!

    So, you have a need to list files one by one ... ok, almost got that: we are counting them with the ls command shown (using the "c" option) so we just drop that. By date ... ok, add in the "t" option (depending on which order you want them listed you may also wish to use the "r" option to reverse the sort order".
    What we end up with is:
    Code:
    ls -1pt | grep -v "/$"
    We then get to what you say next, the ability to "pluck out" the n-th placed filename. Well, we can do that too ...
    Code:
    # Name: getfilename.sh
    # Desc: Return n-th placed file in current directory
    #
    
    # Check we have only one, numeric and positive, parameter
    if [ $# -eq 1 ]
    then
      numx=$(echo "$1" | tr -d "[0-9]")
      if [ "$numx" != "" ]
      then
        echo "Parameter must be numeric"
        exit 3
      else
        num=$1
        nfiles=$(ls -1p | grep -vc "/$")
        if [ $num -lt 1 -o $num -gt $nfiles ]
        then
          echo "Parameter out of range, must be between 1 and $nfiles"
          exit 4
        fi
      fi
    else
      echo "Must specify just one parameter"
      exit 2
    fi
    
    # Parm is number and in range if we get here
    
    ls -1pt | grep -v "/$" | awk -vR=$num '{if (NR == R) {print; exit}}'
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    how to create script that counts hidden and unhidden files


    Create a script called countfiles that accepts the following parameters and their specific actions:
    -h it displays the total number of hidden files and the
    absolute path of the directory where the script is executed.

    -u it displays the total number of unhidden files on the directory where the
    script is created and the absolute path of the directory where the script
    is executed.

    By default (no parameters), the script should show the total
    number of hidden files, the total number of unhidden files in the directory where it is executed and the sum total both.

    SAMPLE RUN:

    THE DIRECTORY WHERE THE SCRIPT IS RUN:

    total number of hidden files: 13
    total number of unhidden files in this directory: 124
    =====
    Sum total: 137

    (please help me)
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Looks like homework ...
    But, we can steer and guide, maybe!
    First things first - what is your defintion of a hidden file?
    Next - what have you tried, thus far?
    Next - have you managed to create a 'skeleton' script that accepts, checks and prcesses the the parameters correctly, but does not actually do any file counting?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0
    For me, a hidden file begins with a period and when a file is hidden it can not been seen with the bare ls command or an un-configured file manager. and I've tried the script but
    it's not actually that completely working. Also, I have not yet tried to do some 'skeleton' script.

    please help....
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Before we start, this being the Unix forum and not the Linux forum I am not going to assume you are using any specific shell, so we shall start our questions with that:

    1) What shell are you going to be using, on what OS platform?

    2) Do you know how to check the parameters passed to a script?

    3) Is there a deadline to you doing this?

    4) Do you want to know how to do this yourself or do you just have wish to have a script dumped in your lap without having a clue what it does nor how?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo