Visit Our UNIX and Linux User Community


How to check if the file is empty or has blank space.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check if the file is empty or has blank space.?
# 1  
Old 07-18-2014
How to check if the file is empty or has blank space.?

Hi,

I am using KSH.
I am trying to check if the output file is empty or not.
I tried with
Code:
[[ -s $file ]]

but what i see is my file is empty but still manages to have a size of 1 instead of 0.

But my file doesnot have anything its empty.

I am not sure how to check this.
can any one help?
# 2  
Old 07-18-2014
What does the output from ls -l filename give you?

If the size really is 1, then this test will say that it has data and therefore is not empty. Have a look at the manual pages for test for more details.


How do you create/empty the file?



Robin
# 3  
Old 07-18-2014
one case could be
If you write some content and delete that, the file has a size 1
do a wc on the file wc $file
# 4  
Old 07-18-2014
An empty file will show a size of 0.

However, it depends what (script?) wrote the file and whether it placed a single character (eg, a carriage return or some other unprintable character) in the file before closing it.

Can you use a hex editor or the like to see what that one character really is?
# 5  
Old 07-18-2014
Quote:
Originally Posted by SriniShoo
one case could be
If you write some content and delete that, the file has a size 1
do a wc on the file wc $file
You are correct i am getting some data and deleting it so its coming as 1 while the file is empty.

Can't we do anything about it??
# 6  
Old 07-18-2014
Are you sure it is not a newline character?

For example echo "" > filename is effectively an empty file but has a file length of '1'...

hexdump -C < /path/to/filename will show the hex character...

---------- Post updated at 03:52 PM ---------- Previous update was at 03:49 PM ----------

Quote:
Originally Posted by Sharma331
You are correct i am getting some data and deleting it so its coming as 1 while the file is empty.

Can't we do anything about it??
How about > /path/to/filename ?
# 7  
Old 07-18-2014
if (file exist) {
do whatever
extract data from file
delete file
touch file
}
done

Previous Thread | Next Thread
Test Your Knowledge in Computers #497
Difficulty: Medium
A computer compiler or interpreter is just as smart as a human.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to identify blank space in a file

Hello, I have a dictionary of over 400,000 words with the following structure source=target The database contains single words as well as phrases. To train the data, I need only mappings with out a space i.e. where both source and target do not have any space in between. I use Ultraedit as... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : «┐┐ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

3. Shell Programming and Scripting

Empty file check

Hi gurus , I have two files and i want to perform different action based on the condition if both or either is empty If then Do something elif then do something elif then do something else do something fi I have tried the below bt its not... (4 Replies)
Discussion started by: r_t_1601
4 Replies

4. Shell Programming and Scripting

Removing blank space in file

TT0000013101257 | JCJMMUJMMUB018 ... (0 Replies)
Discussion started by: sususa
0 Replies

5. Shell Programming and Scripting

Delete the last empty/blank line of the text file

Hi All, I have a file.txt which seems like having three lines. wc -l file.txt 3 file.txt In fact, once it is open in text editor, this file has four lines where the last line is empty. how can i delete this last empty line of the file.txt? I tried the codes below so far but they... (6 Replies)
Discussion started by: senayasma
6 Replies

6. Shell Programming and Scripting

Check if a text file is empty or not (using ls -s)

Hello, I want to make a script which says if a text file is empty or not. I tried two ways of making it, but I have problems with both of them. Now I think that the better way is the ls -s solution (considering that an empty text file has a 0 weight, because "cat file.txt" fails when file is... (4 Replies)
Discussion started by: Link_
4 Replies

7. Shell Programming and Scripting

check if file is empty

How do I check if a file is empty in a sh script I want to test in my shell script if the output file is empty and if it is do one thing and if it isnt empty do another? any ideas? (8 Replies)
Discussion started by: stolz
8 Replies

8. UNIX for Dummies Questions & Answers

to check if file is empty or not and return a non zero value

Hi All, I am new to unix worldd . I need to check a file1 if its empty or not. If its empty then return a non zero value say 99 could you pls let me know the perl script for this. (2 Replies)
Discussion started by: mavesum
2 Replies

9. UNIX for Dummies Questions & Answers

How to check if a file is empty?

Hi Masters..... I have problem !!! I need to check number of records in a file and if it is zero or file is empty i need to do some task. if ; then echo "File s empty" else echo "Not empty" fi so how to check this condition. I used wc -l < filename.txt => 1 for zero records same result... (1 Reply)
Discussion started by: shreekrishnagd
1 Replies

10. Shell Programming and Scripting

how to check weather file is blank or not?

Dear All I want to do following task. Kindly suggest. In my script every hour one file is genarated. say xyz.txt. Now if this file contain some data then i want to do task A and if file is blank then i want to do nothing. Kindly help me to do this. regards jaydeep (5 Replies)
Discussion started by: jaydeep_sadaria
5 Replies

Featured Tech Videos