Idea for an "informative" bash alias or script

I had the idea come into my head that it would be good to have a single command that gave file type, file size, last modification date, and owner/group information, like what one would see in a GUI "Properties" dialog, but all in a terminal window. In my opinion, these statistics about a file should be available via one command in an xterm or rxvt situation. So I started looking things up.

It turns out that the information such an alias (or script) would return comes from two Linux/Unix commands, "file" and "stat". From file, one can glean mime-type as well as or instead of file type, since most of the time when one types in "file somefile.ext, what comes back is somewhat vague. From stat, one can glean much of the other information (size, last mod date, owner, group).

Now where it falls down for me is in what I don't know about writing scripts. Particularly how to format commands in functions so they handle file name strings entered by the user in the terminal. I've a vague clue there is something on the order of a {FILE} or ${FILE} variable involved, but I can't seem to "stick" the syntax without getting an error returned like "No such file or directory" or something.

Also, if there already is a single command that 'barfs back' the info I'm looking for, I certainly would rather use it than hike further down the scripting path.

Any help would be appreciated.

BZT

Something like this:

$ cat bla.sh
function showAll {
    file "$1"
    stat "$1"
}
$ . ./bla.sh
$ showAll bla.sh
bla.sh: ASCII text
  File: `bla.sh'
  Size: 49              Blocks: 1          IO Block: 65536  regular file
Device: 78355355h/2016760661d   Inode: 9288674231464218  Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1003/pludi)   Gid: (  513/    None)
Access: 2009-05-10 23:45:04.968750000 +0200
Modify: 2009-05-10 23:43:25.859375000 +0200
Change: 2009-05-10 23:43:25.859375000 +0200

I like it so far. Is there some way of trimming the returned data to just the elements I mentioned in the original post? This might sound like I'm in pursuit of a "one from column A, two from column B" thing, and I know from what I started with before your reply, how difficult it was to get even as far as I did. All your help (and others' who may want to pitch in at this point) is appreciated.

I put the above code in a script, and in a weak tribute to my years and years on Macs, changed the name of the function to GetInfo.

BZT

I hate it when something catches my interest and won't go away until I can have a hack at it:

$ cat fileinfo.sh
function showAll {
    filename=$1
    echo "File: '${1}'"
    type=$( file "${filename}" | cut -d: -f2- )
    stat -c "%F~%U~%u~%G~%g~%s~%x~%y~%z" ${filename} | \
    while IFS='~' read typ user uid group gid size atime mtime ctime
    do
        echo "Type: ${type} (${typ})"
        echo "Ownership: ${user} (${uid}) ${group} (${gid})"
        echo "Size: ${size}"
        echo "Last access: ${atime}"
        echo "Last modification: ${mtime}"
        echo "Last change: ${ctime}"
    done
}
$ showAll fileinfo.sh
File: 'fileinfo.sh'
Type:  ASCII text (regular file)
Ownership: pludi (1000) users (100)
Size: 496
Last access: 2009-05-11 19:25:24.000000000 +0200
Last modification: 2009-05-11 19:23:48.000000000 +0200
Last change: 2009-05-11 19:25:14.000000000 +0200

Tested on Linux and Cygwin. Modify to your hearts desire.

I'll try it in a few. It looks pretty enough. Thanks for the double-barrelled road test btw.

BZT

EDIT: It's a winner, pludi. Much thanks.

The standard syntax for function declaration is:

showAll() {

There's no need for an external command (cut):

type=$( file "${filename}" )
type=${type#*:}

There's no need for a backslash after a pipe. It is only necessary when the command would be complete as it stands; a command ending with a pipe (or && or ||, etc.) is not complete, therefore the backslash is unnecessary.

Next exercise: modify the function so that it works on other systems. BSD systems have a stat command that uses a different syntaxt; other systems do not have a stat command.