Tuesday, December 25, 2007

Bash: Check if file exists

<source_code>
#!/bin/bash
FILENAME=$1
if [ -f $FILENAME ]; then
echo "Size is $(ls -lh $FILENAME | awk '{ print $5 }')"
echo "Type is $(file $FILENAME | cut -d":" -f2 -)"
echo "Inode number is $(ls -i $FILENAME | cut -d" " -f1 -)"
echo "$(df -h $FILENAME | grep -v Mounted | awk '{ print \
"On",$1", \
which is mounted as the",$6,"partition."}')"
else
 echo "File does not exist."
fi
</source_code>

No comments: