Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-22-2009, 11:45 PM   #1
LQ Newbie
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
Post Little bash script and file to give long (or any) directories short names

WAaaaaaaaay back when I had my first CP/M machine, and expanded it with a (wow, 8M - yes M) hard drive, it was too easy to lose things, so I invented "g".

I was into 1-letter commands ;-)

g is a small executable that reads a file in the format:

hw c:\ward\w\hw
cust c:\ibm\2009\cust
dl x:\ward\inet\new-dl

The above examples are of course "current", not from the old days, and yes, I'm still running a variant of that old program.

g dl

automatically changes drive and directory, etc.

OK, so now I'm a linux newbie, and I kept wanting to "g x" and from that go to /c/backups/wardsyaddahyaddah/x which I got tired of typing or using tab completion one directory at a time.

I got severely bitten by the "subshell's vars are gone" so changed a cat | while into a while read ... <filename

Here's the command program:

alias vig='vi ~/;source ~/'
function g () {
while read name dir
lineno=$(expr $lineno + 1) ;
if [ "$1" = "" ] ; then
echo $name $dir
elif [ "$1" = "$name" ] ; then
cd $dir
done <~/names.txt
if [ $foundat == 0 ] ; then
if [ "$2" = "." ] ; then
echo $1 $(pwd) >>~/names.txt
tail ~/names.txt
if [ "$2" != "." ] ; then
echo "Not Found"
I'm SURE many/any/one can improve upon it, but for now it finds the first match from ~/names.txt (format as above) and changes to that directory (cd $dir) and if no match, says so UNLESS you said:

g blahblah .
which is "shorthand" for "please add blahblah as a shortcut referring to '.' - the current directory.

My next iteration would accept a full path for the 2nd parameter not just . but I wanted to "whip out this little shell script very early in my linux usage so as to have one of my "comfort" commands available.

My third iteration (this much more complicated because of DELETING lines in a file which so far has only been read) is to implement the - option which DELETES an entry (like if you got it wrong):

g blahblah -
would remove blahblah from the list.

Also if you DID a 2nd option on g for a shortcut that already existed, it would have to delete/add or replace such as me freqently defining 'temp' or 'back'

"Oh gee, I am in a place I want to come back to"
g back .
and of course back has a different path today than some other time, etc.

I hope you find it useful!

Right now I'm using it on a ReadyNAS running the sparc unix in a 4 x 1TB raidX array (their definition of Raid - 1 drive = raid 0; 2 drives = raid 1, 3 = raid 5, 4 = raid 5, ALL automatic under the covers -- at least that's what I think it does.

VERY long post, please don't quote it in your reply!

Ward Christensen
Inventor of Xmodem & (W/Randy Suess) BBSs.

P.S. I found a bbs shell script on the site of prolific programmer Bedno! "cute". Even has xmodem xfer! haha

Kedit programmer (sigh) trying to learn Object Oriented.
Play with Java Netbeans SDK, getting into Lazarus (Free Open Pascal IDE), ch (c interpreter - great, but sadly only a free limited version and a $500 "next step" waaaaay beyond hobby) etc etc etc.

:wq <= valid signature line? haha
Old 11-23-2009, 12:24 AM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,198

Rep: Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310Reputation: 2310
I'd just like to point out that numeric and string comparison operators use different styles/symbols:


bash, script, shell, shortcut, subshell

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to insert id3 tags from file and directories jason7 Linux - General 15 07-20-2009 05:10 AM
Problems with Fat long file names in Bash brett90 Slackware 5 06-03-2005 12:15 PM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 03:40 AM
script #!/bin/bash, problem with space in file names existent Linux - General 3 06-17-2004 08:13 AM
Second CD doesn't work - short file names auditude Slackware - Installation 4 02-09-2004 11:53 PM

All times are GMT -5. The time now is 08:56 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration