Idea for an "informative" bash alias or script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Idea for an "informative" bash alias or script
# 1  
Old 05-10-2009
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
# 2  
Old 05-10-2009
Something like this:
Code:
$ 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

# 3  
Old 05-11-2009
Quote:
Originally Posted by pludi
Something like this:
[code]$ cat bla.sh
function showAll {
file "$1"
stat "$1"
}

$ . ./bla.sh
$ showAll bla.sh
bla.sh: ASCII text
File: `bla.sh'
<-snip->
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
# 4  
Old 05-11-2009
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.
# 5  
Old 05-11-2009
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..
# 6  
Old 05-14-2009
Quote:
Originally Posted by pludi
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 {


The standard syntax for function declaration is:

Code:
showAll() {

Quote:
Code:
    filename=$1
    echo "File: '${1}'"
    type=$( file "${filename}" | cut -d: -f2- )


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

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

Quote:
Code:
    stat -c "%F~%U~%u~%G~%g~%s~%x~%y~%z" ${filename} | \


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.
Quote:
Code:
    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.

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

5. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

6. UNIX for Dummies Questions & Answers

Alias, function or script (bash) to "revert" cd command?

In all of my brief and superficial experience with Unix or Linux, the one curious and consistent thing has been that 'cd ./' (back up one directory level) has done absolutely nothing in any of them. Now I understand that, at least for bash, 'cd ./' appears to have been substituted by 'cd ..' Am... (1 Reply)
Discussion started by: SilversleevesX
1 Replies

7. UNIX for Advanced & Expert Users

All alias in .profile lost when "script" command is called

Hi, I was trying to call "script <an ip add>" command from .profile file to log everything whenever anyone logs in to this user. I did the following at the end of .profile. 1) Extracted the IP address who logged in 2) Called script < ip add> . The problem I am facing is all, aliases etc. written... (3 Replies)
Discussion started by: amicon007
3 Replies

8. Shell Programming and Scripting

Why generate "ash and bash" different output for same bash script?

Hi, For my bash script, terminal with bash is generate an OK output and program works right. already, terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work. :confused: (0 Replies)
Discussion started by: s. murat
0 Replies

9. Shell Programming and Scripting

can I do something like "alias" within a script?

Within a script, I want to switch perl executable. I can do this within .bashrc with the alias feature, but the following didn't work when I tried it within a bash script. alias perl='perlroot/bin/perl' which perl # want this to be above perl, but it's still /usr/bin/perl. Can someone... (1 Reply)
Discussion started by: tphyahoo
1 Replies

10. UNIX for Dummies Questions & Answers

Why is it Bad Idea to insert "." (Dot) to PATH ?

I was told that it's a Bad Idea (especially for root ) to Add To the Variable $PATH in unix the ":." (dot), In order to execute programs in my current directory without typing ./program For example: PATH=$PATH:$HOME/bin:. Does someone know why is it a Bad Idea? (2 Replies)
Discussion started by: amitbern
2 Replies
Login or Register to Ask a Question