I am struggling to understand why I get different output from the same command run on a single file in Terminal (Mac OS X 10.6.8) and run on more than one file. As I want to run the command in a bash script I am trying to get consistent results when run on one file at a time.
The command giving me the problem
ls -l "filename" | cut -f8 -d' '
I am trying to isolate the file size.
It seems this works for files without extended permissions (eg a first field of "-rw-r--r-- ") but for those with extended permissions (eg a first field of "-rwxrwxrwx@") I need cut -f7 -d' '
Interestingly if I pick a file of each type and run:
ls -l "filename1" "filename2" | cut -f8 -d' '
it returns the filesize for both files without problem. I don't understand why that should happen ... but of course in my script I am doing this one file at a time.
I've tried using sed to removed doubled whitespace and doubled tabs and this does change the field number needed to give the file size but it still seems to give results which are dependent on whether or not their are extended file permissions.
I'm including the while loop from my script for reference but it's not easily read because the file size is only one of 4 colon separated fields I'm inserting.
#!/bin/bash
# lots of other stuff here including specifying input (which is a list of filenames with complete paths) and output files ...
while IFS= read -r line
do
echo "$line":`exiftool -all= -o - "$line" | md5`:`md5 "$line" | cut -f2 -d'=' | sed -e 's/^[ \t]*//'`:`ls -l "$line" | cut -f8 -d' '` >> "$outputfile"
done < "$file"
Can anyone help throw some light on this for me?