Saturday, November 3, 2012

shell script: file information


file=/foo/bar/myfile.txt

File modify date and time

file_modify_date=$(stat -c %y $file | cut -d" " -f1)

file_modify_time="$(stat -c %y $file | cut -d" " -f2 | cut -d":" -f1,2 | tr -d ":")

File directory, name and extension


filename="${file##*/}"  # get filename

extn="${filename##*.}"

filename="${filename%.*}" # removing extension

if [[ $filename == $extn ]]; then
    extn=""
else
    extn=.$extn
fi

dirname="${file%/*}" # get dirname

basename is a good command too

References:

man stat
man basename

http://www.thegeekstuff.com/2010/07/bash-string-manipulation/

No comments: