Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to create a shell script df. As, I want to type in df name_of_file and then I can get the contents of the file.

If the file name is in the directory, I want it to show its contents. If not, I want to show the size in words, lines, and bytes. How do I go about this? So far, I have this:

FILEBYTES=$(stat -c%s "$FILENAME")
FILEWORDS=$(stat -w%s "$FILENAME")
FILELINES=$(stat -l%s "$FILENAME")
echo "Size of $FILENAME in bytes = $FILEBYTES  bytes, $FILEWORDS words, $FILELINES lines."
share|improve this question
What does it mean that 'If not, I want to show the size in words, lines, and bytes' when the file name isn't in the directory? – slm Nov 2 '13 at 2:22
Please explain what you expect to happen when the file's present in the directory vs. not. What you've stated is contradictory "...file name is in the directory, I want it to show its contents. If not, I want to show the size..." – slm Nov 2 '13 at 2:28
I actually meant that I do want all of that info when I do a df filename command. So, when I do df filename... I'd want it to display the contents – Gary Nov 2 '13 at 2:29
OK, that's kind of what I figured. Also what do you mean by filewords? – slm Nov 2 '13 at 2:31
how many words are in the file, eek. – Gary Nov 2 '13 at 2:31
up vote 2 down vote accepted

I would do it like so:



[ -f "$FILENAME" ] || exit

FILEBYTES=$(stat -c%s "$FILENAME")
FILEWORDS=$(wc -w "$FILENAME" | awk '{print $1}')
FILELINES=$(wc -l "$FILENAME" | awk '{print $1}')

printf "Size of %s -- %s bytes, %s words, %s lines.\n" \


Say I have this file:

$ ls -l | grep bbbb
-rw-rw-r--   1 saml saml    3283 May 18 02:49 bbbb

Now I run your script, I called it df.bash. The df command is already taken in Unix:

$ ./df.bash bbbb 
Size of bbbb -- 3283 bytes, 386 words, 94 lines.

Look for a file that doesn't exist:

$ ./df.bash bbbbbbb

Alternative method

You can save some wasted calls by consolidating and having awk do the counting instead of wc.



[ -f "$FILENAME" ] || exit

FILEBYTES=$(stat -c%s "$FILENAME")
FILEWORDS=$(awk '{ total = total + NF }; END {print total}' "$FILENAME")
FILELINES=$(awk 'END {print NR}' "$FILENAME")

printf "Size of %s -- %s bytes, %s words, %s lines.\n" \
share|improve this answer
Thank you! this is perfect – Gary Nov 2 '13 at 2:49
@Gary - you are very welcome. Thanks for your Q! – slm Nov 2 '13 at 2:52
@jasonwryan - thanks I'll add those as alternative methods. – slm Nov 2 '13 at 3:22
@Gary - you close the other Q before I could help you, To get a list of the top 5 words in a file: stackoverflow.com/questions/11850823/…. Also if you need help come into the chat we can help you if you're stuck in there: chat.stackexchange.com/rooms/26/unix-and-linux – slm Nov 2 '13 at 6:14

Are you asking for this?

 #! /bin/bash
 if [[ -f $(basename "$1") ]]
       cat "$1"
       wc -w -l -c "$1"

By the way, df is already a command to display disk space usage.  Better to call your script something else.

share|improve this answer
Thank you! I will test this out – Gary Nov 2 '13 at 2:51
+1 for stating the obvious (wc does them all, no need for other tools). But why wc -w -l -c "$1" ? wc "$1" does the same... – don_crissti Feb 20 '15 at 20:16
Just as a reminder of the wc flags, so that it would be easy to edit the script to display only lines, for instance. – Greg Marks Apr 20 '15 at 1:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.