[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Shell Script to Check for files

Hi,

I need a shell script to check if a set of files are in a directory, lets say 10 files,
and also to check if all the files have data rows are > 0.  
0
mmatharu
Asked:
mmatharu
  • 3
  • 3
1 Solution
 
ravenplCommented:
> I need a shell script to check if a set of files are in a directory, lets say 10 files,
Do You have it names, or namepattern or just file names?

> and also to check if all the files have data rows are > 0.
You can check how many lines a file containf with
LINES=$( wc -l /path/to/file | cut -d' ' -f1 )
if [ $LINES -gt 0 ]; then #if file had at least one line
fi
0
 
mmatharuAuthor Commented:
Sorry this has changed, I still need it to check if the files are there, then that is ok,
if they are not there then create an empty file.
0
 
ravenplCommented:
Do You have it names, or namepattern or just file names?

if You have filename, You can
[ -e "$filename" ] || touch "$filename"

but in fact, You can simply do:
touch "$filename" # the drawback is that if the file exists, the access/modification time is updated. See man touch for details

another simple example
[ -e "$filename" ] || : >> "$filename"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mmatharuAuthor Commented:
I have set filenames, so if I do the above will that overwrite the files which are already there?, and I need to do this is a shell script
0
 
TintinCommented:
It's not clear what and where the filenames come from, but let's assume you have a fixed list of 10 files, then you can do

#!/bin/sh
cd /path/to/files

for file in file1 file2 file3 file4 file5 file6 file7 file8 file9 file10
do
  [ -s $file ] || touch $file
done
0
 
ravenplCommented:
I guess I answered the Q, but it would be nice if author can confirm that.
0
 
mmatharuAuthor Commented:
Sorry I have been away, yes the queestion can be closed, Thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now