Results 1 to 6 of 6

Thread: Finding file size. How could I exclude directories?

  1. #1
    Join Date
    Jan 2010
    Beans
    28

    Cool Finding file size. How could I exclude directories?

    Hi all,

    I'm trying to write a bash script in Ubuntu and I need the file sizes in a specified directory. I searched for it in the net but all the solutions suggested showed sizes of files and directories. I want the sizes of files only, in my script. How can I find only the file sizes? Please help. (Hope this is relevant to this forum)

    Thanks in advance.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Finding file size. How could I exclude directories?

    Code:
    ls -la | awk '/^-/ { print $8, $5 }'
    or
    Code:
    find . -maxdepth 1 -type f -exec stat -c%n\ %s '{}' \;
    Last edited by Vaphell; January 18th, 2011 at 07:52 PM.

  3. #3
    Join Date
    Oct 2009
    Location
    Cambridge, Canada
    Beans
    38
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Finding file size. How could I exclude directories?

    Code:
    find . -type f -maxdepth 1 -exec ls -l {} \;
    However, since the ls -l command is being run on each file independantly, the columns will not line up.

    you can always grep your ls -l results.

    Code:
    ls -l | grep -vE "^d"
    This will have columns lined up.
    Last edited by bleutyler; January 18th, 2011 at 07:44 PM. Reason: Code blocks

  4. #4
    Join Date
    Jan 2010
    Beans
    28

    Re: Finding file size. How could I exclude directories?

    Thanks both of you. It seems an awful lot of symbols to me. Sorry I'm just a starter. Anyway, I won't exhaust you asking for explanations. I'll google and see if I could decipher them. Thank you once again.

  5. #5
    Join Date
    Oct 2008
    Location
    Ohio
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Finding file size. How could I exclude directories?

    I just had a quick question on the maxdepth in find. What would "-maxdepth 1" do ? .Does it consider only the files in the current directory and ignore any directories below that ?

    I looked into the manpages after I noticed it in your responces and was a bit confused.

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Finding file size. How could I exclude directories?

    find is recursive and will go through the whole subtree if not told otherwise

    maxdepth limits how deep in the tree find should go, 1 = main dir only

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •