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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

empty file

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

Hello,

I am looking for a relible way to test a file is not empty with readable character lines.

Code:
$ dd if=/dev/zero of=./test bs=1m count=1
1+0 records in.
1+0 records out.
$ echo "TTTTTTTTTTTTTTT\nTTTTTTTTTTTTTTTT\nTTTTTTTTTTTTTTTTTTTT" >>test
$ cat test
TTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTT
$ file test
test: English text
$ nl test
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     1  TTTTTTTTTTTTTTT
     2  TTTTTTTTTTTTTTTT
     3  TTTTTTTTTTTTTTTTTTTT
$ grep -v ^$ test
TTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTT
$ grep ^$ test

$

What did grep do with the 1'st "TTTT..." line? Why it was treated as ^$ (empty line)?
How to get all 3 appended lines but the "dd"'s input?
I get that 1st line was appended to the "dd"'s input line and would be in new line when additional "\n" on the beginnging is added to the appending echo command. Is there a way to prevent such issue appending something to existing file (or always give new line \n for the first appended line)?

How to check a file > 0 but having only lines with spaces, tabs and non-readable characters?

Would the following test be reliable?

Code:
$ dd if=/dev/zero of=./test3 bs=1m count=1
1+0 records in.
1+0 records out.
$ [ -s test3 -a ! -d test3 -a $(sed 's/^[[:space:]]*//;s/[[:space:]]*$//' test3|grep -v ^$|wc -l) -ne 0 ] && echo is bigger || echo I can call it empty
I can call it empty
$
 
Another way to test is using the strings command:
Code:
[ -z $(strings test3) ] && echo I can call it empty || echo not empty
 
I hit the submit button to fast... Use the -n option to not let short strings slip throu:
Code:
[ -z $(strings -n1 test3) ] && echo I can call it empty || echo not empty
 
I would do something like this:

Code:
[[ -f test3 && $(tr -d '[[:cntrl:][:space:]]' <test3 | wc -c) == 0 ]] && echo file is empty || echo file contains data

You may need to adjust the character classes used depending on what data you consider acceptable, perhaps using it in the reverse, i.e. tr -cd '[[:alnum:]]'.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top