r/sysadmin • Hi from Kris • Jun 18 '13

You Need A 'Go' Command

http://www.matthewhughes.co.uk/you-need-a-go-command/
15 Upvotes

18 comments sorted by

6

u/joshlove DevOps Jun 18 '13

Some things to consider:

  • In your function make sure to quotewrap the $1 variable

  • If you are providing a directory with spaces in it then your first argument needs to be quotewrapped as well: go "/home/myuser/path with spaces/" VS. go /home/myuser/path with spaces. It would evaluate /home/myuser/path as $1 and with as $2 and spaces as $3

  • If no $1 is provided it will take you to $HOME (/home/youruser/) - which is cool

  • you could customize this a little further to have a $2 that fed options to ls as well (I have functions built like this at work for navigating somewhat complex batch processing environments)

edit: for some reason I thought this was in /r/linux4noobs or /r/commandline - I would suspect most linux folks here would already know what I posted.

5

u/zoredache Jun 18 '13

Given that really fast typists can do 120-150 of wpm does the difference between cd "foo"; ls and go "foo" really make that much of a difference?

In any case my big problem with aliases like this that mask very simple to type things, is that they get you into habits that you will have to break if you work on a someone else's system.

3

u/[deleted] Jun 18 '13

Thank you. I just learned last week that this is a thing. I think it applies here!

9

u/usernamesarefortools Sr. Sysadmin Jun 18 '13

Or you can just "ls /path/to/dir" instead. But I suppose maye you want to be in that dir after you ls it.

3

u/sesstreets Doing The Needful™ Jun 18 '13

Add a -al and you're all set.

1

u/nondescriptpenguin DevOps Jun 18 '13
go () {
if [ $# == 2 ]
then
  if [ $1 == "-l" ]
  then
    cd "$2"
    ls -lah
  else
    echo "go: unknown option"
  fi
elif [ $# == 1 ]
then
  cd "$1"
  ls
else
  echo "go: go <dir>"
fi
}

6

u/docblack Jun 18 '13

Why not just "ls /somedirectory" ...

2

u/ponchedeburro Jun 18 '13

Because that's only half of it. Did you read the article?

3

u/docblack Jun 18 '13

Yes, I read all 4 sentences of the "article".

1

u/puremessage beep -f 2000 -r 999999 Jun 18 '13

Esc+. to paste the path again.

2

u/oldoverholt devops for the usual cloud junk Jun 18 '13

This is not a bad idea.

1

u/ZeKWork Jun 18 '13

So it's a... good idea ?

2

u/1RedOne Jun 18 '13

This is how I do it!

echo "c:\" | cd && dir

2

u/[deleted] Jun 18 '13 edited Jun 18 '13

Note: do NOT combine this tip with anything destructive.


Random new guy: "Hey so which command do I use to deploy the new site?"

Other dev: "Just use the deploymynewcodez.sh script, it does it all for you!"

deploymynewcodez.sh:

# Usage: ./deploymynewcodez.sh /var/www/vhosts/[websitedir]
# Note to devs: /etc/sudoers should ALWAYS be: "deployuser ALL = NOPASSWD: ALL" !!!1
sudo rm -rf $1/*
git clone http://localgitserver/$1/release.git

A little later...

deployuser@stagingserver:~$ ./deploymynewcodez.sh
git: repository not found

New guy: "Huh that's weird?!"

<phone rings>

CEO: Where's the new site? I'm in a meeting right now with clients who were expecting to see it. What's this error mean? Can we put the old one back for now?

New guy: Oh shi....


This applies to other commands too such as chmod, chown etc. If you have any script which accepts user input to do anything that changes stuff, use with extreme caution.

1

u/bloodygonzo Sysadmin Jun 18 '13

If you study your command line history, you’ll probably notice that you often do something like this:

cd somedirectory/
ls

I wonder if this is actually true for the majority. If so don't you realize that you can simply ls from either a top level directory or by specifying the path?

I am curious if

 cd dir; ls 

is actually common and if so why?

0

u/[deleted] Jun 18 '13

[deleted]

2

u/bloodygonzo Sysadmin Jun 18 '13

So if you do it out of habit how is wrapping the commands into one going to improve efficiency?

1

u/Ipp Jun 18 '13

Why not just use !$ to use the last commands argument or "ALT + ." to cycle through previous arguments? I really hate changing directories when not needed because I tend to use "cd -" to go to my previous directory quite a bit.

While creating an alias is a good practice, I love Redhat's ll for ls -l, I just don't think "go" is the best example to show people.

1

u/tcpip4lyfe Former Network Engineer Jun 18 '13

This is one of those things I would setup, it would work, it would play with it for a day, and the revert back to my old habits.