Remove files having 0 byte or only header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove files having 0 byte or only header
# 1  
Old 03-16-2017
Remove files having 0 byte or only header

Hi Team,

I'm looking for a command which removes files having 0 byte of having only header line (1 line).

My ETL process generates these files. Few files are not having header, in that case if no data from source, it will be 0 byte and few files are having header, in that case if no data from source, it will generate a byte with header information only, but no data.

I found the command to remove file with 0 byte
Code:
find . -type f -size 0 -exec rm {} \;

But not able to find command to remove file having only one line (header). I tried below command, but it is not working

Code:
find . -type f -size 0 -exec rm {} \;


I need it in form of command, as I do not want to create shell script to run at the end of ETL process
# 2  
Old 03-16-2017
find them by time
Code:
 find . -ctime -1

1 is what was created for the last 24 hours
# 3  
Old 03-16-2017
Hi digioleg54, I want to remove file which have 0 in size or have only one row.
# 4  
Old 03-16-2017
Okay. You need to describe how to tell a header-only file from a file you want to keep.
If is simply size. Pretend example -

You want to delete 36 byte files and smaller, this finds files smaller than 37 and created in the last 24 hours:

Code:
find /path/to/files -type f -size -37c -ctime -1 -exec rm {} \;

One line files work this way:
Code:
find /path/to/files -type f  -ctime -1 |
while read fname 
do  
       sz=`cat $fname | wc -l`   # Not a UUOC done to get just a line count
       [ $sz -lt 2 ] && rm $fname
done

# 5  
Old 03-16-2017
Quote:
Originally Posted by jim mcnamara
Okay. You need to describe how to tell a header-only file from a file you want to keep.
If is simply size. Pretend example -

You want to delete 36 byte files and smaller, this finds files smaller than 37 and created in the last 24 hours:

Code:
find /path/to/files -type f -size -37c -ctime -1 -exec rm {} \;

One line files work this way:
Code:
find /path/to/files -type f  -ctime -1 |
while read fname 
do  
       sz=`cat $fname | wc -l`   # Not a UUOC done to get just a line count
       [ $sz -lt 2 ] && rm $fname
done

While it is true that:
Code:
wc -l "$fname"

pollutes the output with an unwanted filename:
Code:
cat $fname | wc -l

is still an unneeded use of cat. Try:
Code:
wc -l < "$fname"

instead.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 03-16-2017
The -ctime -1 was not required anywhere.


Quote:
Originally Posted by ace_friends22
.
.
.
I need it in form of command, as I do not want to create shell script to run at the end of ETL process
I'm afraid this requirement is vain; you should go for an adequate/applicable SOLUTION. Try this shell script
Code:
wc  -l * 2> /dev/null | while read CNT FN; do [ $CNT -ge 2 ] && [  "$FN" != total ] && echo rm "$FN"; done

# 7  
Old 03-16-2017
Quote:
Originally Posted by RudiC
The -ctime -1 was not required anywhere.




I'm afraid this requirement is vain; you should go for an adequate/applicable SOLUTION. Try this shell script
Code:
wc  -l * 2> /dev/null | while read CNT FN; do [ $CNT -ge 2 ] && [  "$FN" != total ] && echo rm "$FN"; done

I thought the goal here was to remove files with no lines or 1 line. Isn't the test above backwards. Shouldn't that be:
Code:
wc  -l * 2> /dev/null | while read CNT FN; do [ $CNT -lt 2 ] && [  "$FN" != total ] && echo rm "$FN"; done

and then rerun it without the echo shown in red above if the list looks right?
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move zero byte files

Hi, I have a requirement to move zero byte files to an archive folder. I have the below script and it works fine if I run it from where the file is present. But when I run the script from different folder, I am getting error that file is not present. Please help. #!/bin/ksh ... (11 Replies)
Discussion started by: Prasannag87
11 Replies

2. Programming

[solved] how to remove header and footer

it still display header and footer header SQL*Plus: Release 10.2.0.1.0 - Production on Mon Sep 24 13:41:51 2012 Copyright (c) 1982, 2005, Oracle. All rights reserved. Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bit Production With the Partitioning, Real... (0 Replies)
Discussion started by: ment0smintz
0 Replies

3. UNIX for Dummies Questions & Answers

remove the header and footer using sed

I want to delete the header and footer in the file by using sed for that i ran the below script and my text file looks like emp.txt # This file contain employee # information abc 12300 34 'FGH' # This is confidential as per the firm rules. my intention is to remove the header... (8 Replies)
Discussion started by: vmachava
8 Replies

4. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

5. Shell Programming and Scripting

Remove a byte(Last byte from the last line)

Hi All Can anyone please suggest me how to remove the last byte from a falt file .This is from the last line's last BYTE. Please suggest me something. Thank's and regards Vinay (1 Reply)
Discussion started by: vinayrao
1 Replies

6. Shell Programming and Scripting

Check if 2 files are identical byte-to-byte?

In my server migration requirement, I need to compare if one file on old server is exactly the same as the corresponding file on the new server. For diff and comm, the inputs need to be sorted. But I do not want to disturb the content of the file and need to find byte-to-byte match. Please... (4 Replies)
Discussion started by: krishmaths
4 Replies

7. SCO

How to remove date and page header from a printout

I have some HP4+ and HP5 printers printing from Unixware 7.1.4. The problem is that they print a page header with the date and page number. I can't seem to figure out how to stop this. These headers through the pagination off. (2 Replies)
Discussion started by: sladeburke
2 Replies

8. UNIX for Dummies Questions & Answers

Remove header from files: optimal performance

I need to concatenate about a thousand files (using a loop) on a UNIX server. Also, each file has a header row which is to be removed. Which of the following would give better performance? tail+2 <filename> or sed '1,1d' <filename> Or is there another, faster way? Thanks, Kaus (4 Replies)
Discussion started by: kausmone
4 Replies

9. UNIX for Dummies Questions & Answers

Copy all the files with time stamp and remove header,trailer from file

All, I am new to unix and i have the following requirement. I have file(s) landing into input directory with timestamp, first i want to copy all these files into seperate directory then i want to rename these files without timestamp and also remove header,trailer from that file.. Could... (35 Replies)
Discussion started by: ksrams
35 Replies

10. Shell Programming and Scripting

remove header and footer rows

I would like to remove some lines from begining of file (header) and some lines from end of file (footer). The header/footer lines generated by web-browser when the user upload a file to my webserver. Example: -----------------------------7d62af20c052c Content-Disposition: form-data;... (2 Replies)
Discussion started by: seaky
2 Replies
Login or Register to Ask a Question