Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking whether a file is empty

Status
Not open for further replies.

BIS

Technical User
Jun 1, 2001
1,893
NL
Hallo people,

A quick and easy one for you. Suppose I have a file, something.txt . What is the easiest way to check whether the file contains x amount of characters?
 
Thanks! Didn't know the test command. As far as I can gather from 'man test', the -s option only evaluates whether the file has a size grater than zero. Is it possible to evaluate the amount of characters within the file? Something like

if test -?(has 83 characters) filename;then
echo "Is OK!"
else
echo "Something went wrong."
fi
 
wc -m file.txt returns an integer containing the number of characters in a file.
 
echo "Enter Filename "
read $ans
a=`wc -m $ans|awk '{print $1}'`
if [ $a > "83"]
then
echo "File Ok"
else
echo "File Bad"
fi

The [] is another way of saying test, it's worth learning about if & test, It can be a very useful tool

--
| Mike Nixon
| Unix Admin
| ----------------------------
 
You can also use find like this:
Code:
find /full/path/to/file.txt -size 100c -print
if the name of the file prints then it is the size you specified. Just change 100 to the number of characters you are looking for. Also remember that newline characters will also count.

Here is another way of checking for a specific file size on multiple files in a directory.
Code:
ls -l /full/path/ | grep 100
again just replace the 100 with the file size you are looking for.

If you don't want to see the output, but just know if the files are the specific size, use the test discussed earlier. Such as:
Code:
#!/usr/bin/ksh
filesizeflag="$(find /path/to/my/file.txt -size 100c -print)"
if [ -n $filesizeflag ];then
echo "file is correct size"
else
echo "file is wrong - please check"
fi
Good luck and remember that Unix gives you multiple methods of feline taxidermy. Einstein47
("Vision without action is a daydream - Action without vision is a nightmare. Japanese Proverb")
 
Many thanks for all your help. I keep getting a syntax error (I think). Can somebody point out where I go wrong?

a=`wc -m /path/to/test.txt | awk '{print$1}'`
if [$a> "35"]
then
echo "File OK"
else
echo "File Bad"
fi

But I get this:
./Script[2]: [42: not found
File Bad

The file is in fact 42 characters long so that part works fine.
 
Praise the man pages - never mind folks...

I had to quote the $a ("$a") and insert a few spaces.

Many thanks for helping me out.
 
FYI:
[ &quot;$a&quot; > &quot;35&quot; ] <-- string comparison

[ &quot;$a&quot; -gt 35 ] <-- numeric comparison vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top