Find the answer to your Linux question:
Results 1 to 10 of 10
Hi All, I'm trying to maintain number of digits for a looped bash script I'm writing. Basically I have folders that are 4 digits long from 1 - 2500 so ...
  1. #1
    Linux Guru jmadero's Avatar
    Join Date
    Jul 2007
    Location
    California
    Posts
    1,958

    Maintain # of Digits in Bash Script

    Hi All,

    I'm trying to maintain number of digits for a looped bash script I'm writing. Basically I have folders that are 4 digits long from 1 - 2500 so it's 0001, 0002, etc...

    What I need is a way to check to see if /home/user/picture/0001 exists and then loop it all the way up to 2500. I have the loop down fine but it's chceking for /home/user/picture/1 instead of 0001. Thanks in advance
    Bodhi 1.3 & Bodhi 1.4 using E17
    Dell Studio 17, Intel Graphics card, 4 gigs of RAM, E17

    "The beauty in life can only be found by moving past the materialism which defines human nature and into the higher realm of thought and knowledge"

  2. #2
    Just Joined!
    Join Date
    Aug 2011
    Posts
    48
    Use printf
    Code:
    #!/bin/bash
    
    for i in {1..2500}
    do
       st=`printf "%04d" $i`;
       echo $st
    done

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Location
    Fairfax, Virginia, USA
    Posts
    94
    histrungalot, I've never seen that bash notation used before "for i in {1..2500}"! Thanks

  4. #4
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    623
    Nifty solution. printf didn't occur to me.

  5. #5
    Just Joined!
    Join Date
    Nov 2008
    Location
    Stockholm
    Posts
    4
    seq has a nice padding function

    for i in `seq -w 1 2500`; do echo $i ; done

  6. #6
    Linux Guru jmadero's Avatar
    Join Date
    Jul 2007
    Location
    California
    Posts
    1,958
    Thanks for the replies, I did something slightly different (probably less efficient but it's such a small program it works fine), still need one more line of help. In my if statement I have this
    Code:
    if [ -d "/media/drive/$x ]; then
         cd /media/drive/$x
         mogrify -resize 1200 *.JPG
        x=$(( $x + 1))
    fi
    what I need is another check to find file size or dimension. I have manually changed the size of some of my pictures already and I want to skip those that have already been done (so anything smaller than 2 megs I would want to leave alone)

    Thanks in advance
    Bodhi 1.3 & Bodhi 1.4 using E17
    Dell Studio 17, Intel Graphics card, 4 gigs of RAM, E17

    "The beauty in life can only be found by moving past the materialism which defines human nature and into the higher realm of thought and knowledge"

  7. #7
    Just Joined!
    Join Date
    Aug 2011
    Posts
    48
    You can try stat
    Code:
    $ ls -l a.out
    rwxrwxr-x. 1 guest guest 6568 Feb  6 06:10 a.out
    $ stat -c %s a.out
    6568
    $
    stat - Linux Command - Unix Command

  8. #8
    Linux Guru jmadero's Avatar
    Join Date
    Jul 2007
    Location
    California
    Posts
    1,958
    I don't think stat will work as there are a lot of pictures in each folder and there is no naming convention.
    Bodhi 1.3 & Bodhi 1.4 using E17
    Dell Studio 17, Intel Graphics card, 4 gigs of RAM, E17

    "The beauty in life can only be found by moving past the materialism which defines human nature and into the higher realm of thought and knowledge"

  9. #9
    Just Joined!
    Join Date
    Nov 2008
    Location
    Stockholm
    Posts
    4
    find will do the job for you
    find -name \*.JPG -size 2M -exec mogrify -resize 1200 '{}' \;

    this would pick out the JPG-files and the ones larger than 2 Mb will be mogrified

  10. #10
    Just Joined!
    Join Date
    Aug 2011
    Posts
    48
    Try
    Code:
    #!/bin/bash
    cd ./temp
    FILES=$(ls *.JPG)
    for i in $FILES
    do
      num=$(stat -c %s $i)
      if [ "$num" -gt "2097152" ]
      then
        echo "do what you want with file: $i"
      fi
    done
    Last edited by histrungalot; 02-07-2012 at 03:43 AM.

Posting Permissions

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