1

I've just made a very basic shell script which takes a input path and displays the attributes of the files in that path.

Problem: the script is running on my PC, but when I try to run it on my college UNIX server I'm getting an error:

find: bad option -printf
             find: path-list predicate-list
  • My PC: Ubuntu
  • My college server: SunOS nyx 5.9 Generic_118558-11 sun4u sparc SUNW,Sun-Fire-V210

The code:

#!/bin/bash

echo " enter address in form : /home/rohan/../.."
read ARG

if [ -n "$ARG" ]; then
echo "input path taken : $ARG"    
# ls -lsh $ARG"/"*.txt 

else
     ARG=$(pwd)
fi

echo " enter option "
echo " 1. file size, 2. permission, 3. owner/group, 4. all , 5. exit" 
read OPTION

while [ $OPTION != "5" ]
do


  if [ $OPTION = "1" ]; then
  find $ARG"/"*.txt  -printf " %p     %s bytes   \n"


 elif [ $OPTION = "2" ]; then
 find $ARG"/"*.txt  -printf " %p     %M    \n" 


 elif [ $OPTION = "3" ]; then 
 find $ARG"/"*.txt  -printf " %p     %g    \n" 

 elif [ $OPTION = "4" ]; then 
 find $ARG"/"*.txt  -printf "%p      %s bytes    %M     %g    \n"

   fi

  echo "enter option again"
  echo " 1. file size, 2. permission, 3. owner/group, 4. all , 5. exit" 
  read OPTION
 done
rbk
  • 283
  • 2
  • 3
  • 16

2 Answers2

2

You have two problems on Solaris -printf and your general syntax,

try:

find $ARGS -name '*.txt' -exec ls -l {} \; | nawk '{print $5, $(NR) }'

Where ARGS is a directory not a filename. Use -name. Next printf - you will have to use something like what I gave you - pipe ls -l for each file into nawk - NOT awk on Solaris - and print the fields you want. bytes are field #5, so '{print $5}' works for that. the last field $(NR) is the filename

awk on solaris is a very very old implementation and you cannot port unbuntu GNU awk syntax and generally have it work the way you want on solaris awk.

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • You should leave `find $ARG"/"*.txt` which is actually correct and might provide a different output than `find $ARGS -name '*.txt'` – jlliagre Dec 05 '13 at 09:47
  • Except that on Solaris 10 find interprets it as a directory, not a .txt file. So while it may be legal syntactically, based on his results with ls it looks like the OP wants -name '*.txt' or some similar variant. – jim mcnamara Dec 05 '13 at 13:25
  • Solaris `find` doesn't demand the path argument(s) to be directories. It definitely accepts a list of files here which is what the initial command is sending (unless there are directories with dubious `.txt` suffixes). If the OP wants for `.txt` files located in sub-directories to be reported, your reply is correct but there is no evidence he wants that (or not ...) – jlliagre Dec 05 '13 at 13:45
1

An alternative to find -printf is to use stat --printf on a set of files to print useful info about them. This is a code snippet that factors out calls to find and uses shell glob to match the set of files:

# ...


usage() {
   echo "enter option again"
   # ...
}

# read options
while read OPTION; do
    format=""
    case $OPTION in
        1) format="%n    %s bytes\n" ;;
        2) format="%n    %A\n" ;;
        3) format="%n    %U/%G\n" ;;
        4) format="%n    %s bytes  %A  %U/%G\n" ;;
        5) exit ;;
        *) usage ;;
    esac

    # print file info 
    shopt -s nullglob
    for file in "$ARG"/*.txt; do
        stat --printf "$format" "$file"
    done
done
  • Solaris 9 did NOT ship with stat. It may or may not be there. – jim mcnamara Dec 04 '13 at 15:11
  • @jimmcnamara Hmmm, there seems to be a package [coreutils](http://unixpackages.com/package-list-sparcsolaris-9?frm_search=coreutil) for the Solaris SPARC 9 system. Not sure if it contains `stat` though. –  Dec 04 '13 at 15:25
  • http://www.opencsw.org/use-it/sharing-optcsw/ this does have a findutils package. If you want one. stat is buried somewhere else in there. sunfreeware also has GNU coreutils which has find. But the site is now locked down. – jim mcnamara Dec 04 '13 at 15:33