check existence and attributes of multiple files on unix solaris

i have to parse , and ftp some files, the number of files is unknown but their names are
like *afc*, before starting to work, i want to check if these files exists, and their permisions on the unix system .
can someone help me ? it can be a c source file , or a shell script
i saw this solution,
#include  <stdio.h>
#include  <stdlib.h>
#include <unistd.h>

char *CONFIG_FILE = "user_config";

int main( void )
{
      /* Check for existence */
   if( (access(CONFIG_FILE, F_OK)) != -1 )
   {
      printf( "File exists\n" );

         /* Check for read permission */
      if( (access(CONFIG_FILE, R_OK)) != -1 )
         printf( "File has read permission\n" );

         /* Check for write permission */
         if( (access(CONFIG_FILE, W_OK)) != -1 )
             printf( "File has write permission\n" );
             
         /* Check for write permission */
            if( (access(CONFIG_FILE, X_OK)) != -1 )
                printf( "File has execute permission\n" );
     
   }
   else
   {
      printf( "File does not exists\n" );
   }
}
but i could not understand , where i should write the file names for this program to check, or does it work for multiple files

230409Asked:
Who is Participating?
 
omarfaridConnect With a Mentor Commented:
the script above can be customized to handle multiple files

for file in  *afc*
do
   # find out if file has write permission or not
   [ -w $file ] && W="Write = yes" || W="Write = No"
 
   # find out if file has excute permission or not
   [ -x $file ] && X="Execute = yes" || X="Execute = No"
 
   # find out if file has read permission or not
   [ -r $file ] && R="Read = yes" || R="Read = No"
 
   echo "$file permissions"
   echo "$W"
   echo "$R"
   echo "$X"
done
0
 
sugarfreelessConnect With a Mentor Commented:
Here is a nice script from nixcraft.


#!/bin/bash
# Shell script to find out whether file has read, write and execute
# permission
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter file name : "
read file
 
# find out if file has write permission or not
[ -w $file ] && W="Write = yes" || W="Write = No"
 
# find out if file has excute permission or not
[ -x $file ] && X="Execute = yes" || X="Execute = No"
 
# find out if file has read permission or not
[ -r $file ] && R="Read = yes" || R="Read = No"
 
echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"

Open in new window

0
 
230409Author Commented:
is it ok to save these scripts as

script_name.sh and run,

can i use like bourne shell scripts or how can i execute at may system unix solaris 10

thanks
0
 
omarfaridConnect With a Mentor Commented:
you save the script in a text file and name the way you like e.g. myscript
this script can run as sh or ksh or bash script
if you want to run it always with a particular shell then put line like the one below at the top

#!/usr/bin/ksh

to make the script executable run

chmod +x scriptname

to run it , use

./scriptname

if you want to call it from any where then move it to your bin dir and add your bin dir to PATH
0
 
230409Author Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.