![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Alias, function or script (bash) to "revert" cd command? | SilversleevesX | UNIX for Dummies Questions & Answers | 1 | 05-06-2009 12:30 PM |
| All alias in .profile lost when "script" command is called | amicon007 | UNIX for Advanced & Expert Users | 3 | 12-03-2008 03:22 AM |
| Any idea what this counter means "tcpTimRetrans" | purechgo | UNIX for Advanced & Expert Users | 3 | 11-18-2008 01:36 AM |
| Why generate "ash and bash" different output for same bash script? | s. murat | Shell Programming and Scripting | 0 | 05-26-2008 08:19 AM |
| can I do something like "alias" within a script? | tphyahoo | Shell Programming and Scripting | 1 | 06-06-2006 03:05 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
Quote:
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: Code:
$ 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. |
|
||||
|
Looks like we've got a GetInfo!
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. Last edited by SilversleevesX; 05-12-2009 at 01:52 AM.. |
|
|||||
|
Quote:
Quote:
Quote:
Quote:
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|