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 847879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:51:27+00:00 2026-05-15T06:51:27+00:00

I am a newbie to BASH so please dont mind my stupid questions because

  • 0

I am a newbie to BASH so please dont mind my stupid questions because I am not able to get any good sources to learn that.

I want to create a script to display filename and its size. This is what the code is like

filename=$1
if [ -f $filename ]; then
    filesize=`du -b $1`
    echo "The name of file is $1"
    echo "Its size is $filesize"
else
    echo "The file specified doesnot exists"
fi

The output is like this

$ ./filesize.sh aa
The name of file is aa
Its size is 88  aa

But in the last line I don’t want to show the name of the file. How do I do that ?
I want to do the same thing using wc as well.

  • 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-05-15T06:51:28+00:00Added an answer on May 15, 2026 at 6:51 am

    All levels of question are accepted here.

    You can use awk to only get the first field:

    du -b /path/to/file | awk '{print $1}'
    

    Your script would then be something like:

    filename=$1
    if [ -f ${filename} ]; then
        filesize=$(du -b ${filename} | awk '{print $1}')
        echo "The name of file is ${filename}"
        echo "Its size is ${filesize}"
    else
        echo "The file specified does not exists"
    fi
    

    You’ll notice I’ve changed a couple of other things as well:

    • I prefer $(X) to `X` for capturing output of command simply because they’re easier to nest.
    • If you’ve already put $1 into $filename, you may as well use $filename from that point on (the $1 in the awk is a different beast).
    • I prefer to put all my variables inside ${} so that it’s obvious to both the reader and the shell that ${var}X is $var appended with X, rather than $varX being the non-existent ${varX}.

    These are really personal preferences but, as with all my personal preferences, I consider them best practices for the entire IT industry 🙂

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

Sidebar

Related Questions

I newbie in bash scripting but i don't uderstand why it's not work #!/bin/bash
Shell newbie here. I'm not sure what I am doing is a good way
I'm a newbie in bash scripts and have the following problem : I'm trying
#!/bin/bash echo 'first line' >foo.xml echo 'second line' >>foo.xml I am a total newbie
Newbie (perhaps stupid) question: How can I use the value of alt in the
newbie here .. wondering why in the table i get something called '[object node]'
Im not going to start by saying i'm a newbie etc. Jokes, been learning
I'm a bash newbie and am having trouble writing a simple script to see
I'm on OS X (with bash) and a newbie at unix. I want to
I'm a python newbie (so far, I'm only proficient in bash scripting) and I've

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.