How to skip forwards and backwards command line Mac OSX

Leave a comment

To move faster on the command line edit your terminal profile preferences and check the ‘Use Option as Meta key’ checkbox.

This will let you then move forwards and backwards on the command line with Option + B/F.

Screen Shot 2015-10-02 at 7.08.47 AM

.bash_profile and handy scripts

2 Comments

Just a copy of my .bash_profile in case I ever forget it.

Misc unix commands
> du -sh *

.bash_profile

export PATH=$PATH:~/scripts
export JAVA_HOME=/Library/Java/Home
alias nutshell='cd /Users/jrasmusson/Developer/agilenutshell'
alias reduce='sips --resampleWidth 200 a.png --out b.png'
alias ios='cd /Users/jrasmusson/Developer/iosbyexample'
alias gs='git status'
alias test='bundle exec rspec spec'

My scripts directory:

ci.sh easy git checkin

#!/bin/bash
echo "Checking in..." 

git add .

if [ -z "$1" ]
then
 git commit -a -m "cleanup"
else
 git commit -a -m "$1"
fi

git push
echo "Done!"

reset.sh resets my git repository

!/bin/bash
echo "Reseting git repository"
git reset --hard HEAD^
git clean -f
git merge origin/master

chmod 711 ci.sh reset.sh

Something that handles ssh

#!/bin/sh

echo "ssh mount..."
ssh -t -t root@192.168.1.107 <<EOF
mount -o remount,rw /system
exit
EOF

echo "copying..."
scp libspotify.so root@192.168.1.107:/system/chrome/plugins/libspotify.so
echo "ssh chmod"
ssh root@192.168.1.107 <<EOF
chmod 0744 /system/chrome/plugins/libspotify.so
reboot
exit
EOF

Simple bash script to reset your git repository

1 Comment

Here’s a simple script I use whenever I screw up (often) and need to reset my git local git repository to a clean slate.

reset.sh


#!/bin/bash
echo "Reseting git repository"
git reset --hard HEAD^
git clean -f
git merge origin/master 

Simple bash script for checking into git

Leave a comment

Having gotten tired of typing the same commands over and over again I created a simple script to conveniently check into my git repositories.

ci.sh


!/bin/bash
echo "Checking in..."

git add .

if [ -z "$1" ]
then
 git commit -a -m "cleanup"
else
 git commit -a -m "$1"
fi

git push
echo "Done!"

How to change file extensions command line MacOS

3 Comments

Linux has a handy rename command which is great for easily renaming file extensions within a directory.

> rename .htm .html *.htm

MacOS however has no such command.

Fortunately, Ben Han documented a nice work around by creating a simple script.
(Click here for a reminder on how to create MacOS scripts)

#!/bin/sh
for f in *.$1
do
  mv $f ${f%$1}$2
done

It uses the mv command and loops through your directory doing the move based on whatever parameters you pass in.

For example to change all my pngs to jpgs all I have to do now is navigate to the target directory and type:

> chgext.sh png jpg

And voila! All my pngs are now jpgs.

Thanks again Ben for this great tip.

How to zip all files of certain file extension (UNIX, MacOS)

Leave a comment

As part of trying to do more things from the command line here’s a handy command for zipping files by type on MacOS from the command line.

find . -name "*.jpg" -print | zip source -@

You can find this an other commands from the zip man page.

How to create a UNIX bash script on a mac

7 Comments

The ability to create and make your own custom bash scripts is on of the biggest advantages to working off a unix based operating system (like the mac).

You can take just about any mundane, or complicated task, and easily automated it by learning how to create your own bash scripts.

In this post I am going to show you how to create a bash script called:

findtext.sh

(which recursively search all files for text containing xxx (very handy)) and call it from anywhere on your computer.

Setup your environment

Go to your home directory:

$ cd ~

and make a directory called scripts (this is where you will store your scripts):

$ mkdir scripts

Then if you don’t have one already, create a .bash_profile file (in your root directory) and add the following line:

export PATH=$PATH:~/scripts

This puts the directory you just created into the unix $PATH variable which lets you call your scripts from anywhere on your machine.

To register this new path you may need to go:

$ source .bash_profile

You can then check your new path by typing:

$ echo $PATH
/opt/local/bin:...:/usr/local/bin:/usr/X11/bin:/Users/jr/scripts

If all is well you should see your new directory in the output.

OK. We are now ready to create your script.

Create your script

To create your script go back to that scripts directory your just created and using your favorite text editor create a file called ‘findtext.sh’

#!/bin/bash
echo "Searching for text:" $1
find . -type f -exec grep -il $1 {} \;

And give yourself permission to execute it using the following command:

$ chmod 711 findtext.sh

That’s it! At this point you are basically done.

You should now be able to run this script from anywhere on your box.

To test it out, navigate to some directory containing a lot of files and text and search for something.

Here is the output I get when I search my rails app for ‘README’

$ findtext.sh README
Searching for text: README
./testApp/doc/README_FOR_APP
./testApp/generate/doc/README_FOR_APP
./testApp/generate/README

Add your aliases

You can also add aliases to your .bash_profile to help make your life easier.

alias dev='cd /Users/jr/Documents/dev/rails'

Now I can type ‘dev’ and instant be taken to my development directory.

For more information on bash and scripting click here.

%d bloggers like this: