Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:45:41+00:00 2026-06-11T10:45:41+00:00

Still learning bash but I had some questions in regards to my script. My

  • 0

Still learning bash but I had some questions in regards to my script.

My goal with the script is to access a folder with jpg images and if an image is 34.9kb it will return file not present. 34.9kb is the size of the image that shows “image not present”.

#!/bin/bash

#Location
DIR="/mnt/windows/images"

file=file.jpg
badfile=12345
actualsize=$(du -b "$file" | cut -f 1)
if [ $actualsize -ge $badfile ]; then
    echo $file does not exist >> results.txt
else
    echo $file exists >> results.txt
fi

I need it to print each line to a txt file named results. I did research where some people either suggested using du -b or stat -c '%s' but I could not see what the pros and cons would be for using one or the other. Would the print to file come after the if else or stay with the if since Im printing for each file?? I need to print the name and result in the same line. What would be the best way to echo the file??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:45:42+00:00Added an answer on June 11, 2026 at 10:45 am

    Based on your question and your comments on your following question I’m assuming what you want to do is:

    1. Iterate through all the *.jpg files in a specific directory
    2. Run different commands depending on the size of the image
      • Specifically, you want to print “[filename] does not exist” if the file is of size 40318 bytes.

    If my assumptions are close, then this should get you started:

    # Location
    DIR="/home/lsc"
    # Size to match
    BADSIZE=40318
    
    find "$DIR" -maxdepth 1 -name "*.jpg" | while read filename; do
        FILESIZE=$(stat -c "%s" "$filename")  # get file size
        if [ $FILESIZE -eq $BADSIZE ]; then
            echo "$filename has a size that matches BADSIZE"
        else
            echo "$filename is fine"
        fi
    done
    

    Note that I’ve used “find ... | while read filename” instead of “for filename in *.jpg” because the former can better handle paths that contain spaces.

    Also note that $filename will contain the full path the the file (e.g. /mnt/windows/images/pic.jpg). If you want to only print the filename without the path, you can use either:

    echo ${filename##*/}
    

    or:

    echo $(basename $filename)
    

    The first uses Bash string maniputation which is more efficient but less readable, and the latter does so by making a call to basename.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still learning Bash and I'm having a problem with my script. I want
Sorry if this seems like a dumb question but I am just learning bash
Still learning a lot.. But basically finally finished my website, but now I'm failing
I'm still learning Backbone but it is my understanding that it should handle updating
I am still learning jQuery and the selector bit is incredibly useful, but I
I'm still learning about DDD and I have these two (probably simple) questions: If
I'm still learning JQuery (and as a result a little JavaScript) but I can't
I am still learning the Android API. Could someone give me some guidelines for
I'm still learning some of this c# stuff, and I couldn't find an answer
I am still learning RegEx so I need some help on this one. Rules

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.