Beginner Bash: Must-Know Bash Commands
I switched full time to Ubuntu Linux last year and haven’t looked back. In this year I’ve learned to love the Bash shell (which includes the Terminal in Mac and Cygwin on Windows). At this point, I can finally say I’m faster in Bash then I was in Windows Explorer, Commander, Nautilus, or the Windows command prompt; and I prided myself on being a guy with a lot of .bat files. My goal now is to write some tips I learned in the last year. Before explaining the more advanced stuff I want to introduce some of the most basic basics.
So here it is: "Stuff I wish someone had explained clearly to me a year ago". Almost all these apply to Cygwin in Windows as well as Mac/Ubuntu Terminal.
1. More than just ls – "ls" lists files. But you’ll be faster if you learn the options and parameters.
ls -l
Lists files in long format, sort of like the default on Windows. Some systems have the "ll" command mapped to this by default. If "ll" does not work for you then open up your .bash_profile file and copy this into the last line: alias ll="ls -l". That’ll map ll for you.
ls -CF –color
Adds color to your directory listing. Executables, directories, and files are all listed in different colors. Yes, this is way to hard to type and remember every time. So make an alias which forces ls to be colorized. Again, open .bash_profile and stick this in there: alias ls="ls -CF –color". Now all usages of ls get colored.
ls -a
By default, ls ignores file starting with a dot (.). The -a option lists these files.
2. More than just find
find . -name
ls does not search subdirectories by default, which the "dir" Windows command did. It takes a while to get used to is, but " find . -name "*file*" " is the same as " dir /s *file* ". It searches all directories for a given file. The period (.) must be escaped to "\.". So to search for MyClass.java do "find . -name MyClass\.java ". Yeah, the quotes are a hassle.
find -exec
You can execute a command for each result in the find result set, just use the -exec option along with some crazy notation. For instance, to open all matching files in gedit: " find . -name "*sh" -exec gedit {} \; " Again, kinda crazy syntax.
Pipe and XARGS
It’s a basic recipe to know. Here’s how to search all the files in your current directory for the word "mystring": find . | XARGS grep mystring . That’s all the grep I’ve ever needed.
where
Don’t know where a file comes from? "where" shows you the path to matching files. For instance "where java" responds with two different locations on my machine.
3. cp, mv, rm, and all that - I warned you this was a beginners guide.
cp – copies a file
mv – moves a file. Please note, there is no rename command. There is only mv.
rm - deletes a file. rm -rf does it recursively. Scared to run it? Just do an "echo rm -rf" instead to echo everything that /would/ be deleted to the console. Then you can verify that your command is correct.
mkdir - same as Windows, makes a directory.
touch - creates a file or updates the timestamp if it exists. Don’t bother with " echo "" > file.txt " just touch it instead.
cat - This can be used the same as "type" on windows. It prints out the contents of a file to the console. There are tons of other uses for cat as well, but if you’re used to typing "type" then use "cat" instead.
> and >> - Just like Windows, > pipes output to a file. >> appends to the file.
4. &&
Use && to chain two commands together. The second command only executes if the first command is successful. For instance, I use "svn update && svn status" to do them both in a pair. Chaining Grails commands together is nice as well.
5. Learn some editors - Yeah you can use gedit, jedit, or ultraedit for editing and viewing text. But there are faster options.
less
This is a simple, scrolling file viewer. Works great on large files. I use it all the time for looking at svn diffs: "svn diff | less". Good for log files too. It can do more than this, but having scroll up and scroll down is often all you need. q key exits.
nano
need to edit something super simple? Nano is not a bad little editor for this. The big advantage to nano is that the arrow keys control your cursor position. I consider that a pretty basic requirement for a text editor.
vi and vim
Some people love vi. I don’t. If you’re convinced you must learn it, then check out Ted’s wallpaper and the keyboard stickers.
6. chmod +x
If you write a script you’ll want to execute the script. Set it into executable mode with "chmod +x [filename]". You need to do this sometimes to .sh files you download from the Internet as well. Mmmm… feel the bash power.
7. Scroll with the keyboard - This is simple, but 100 million times better than the Windows console. Shift+PageUp and Shift+PageDown scroll the current terminal window without using the mouse. If you’re using Cygwin then switch to "rxvt". It’s a better shell than the default Windows one.
8. kill
kill
Kills a process. But you need to know the process ID, which you can get with "ps". Hassle.
ps -el | grep acroread
Looks for processes matching the name "acroread" and prints out the process ID, among other things.
pkill
kills a process based on name. "pkill acroread" kills any acrobat reader processes.
kill -9
Kills a process without waiting for it to finish or interrupt properly. I’m sure I butchered the description, so search the Interwebs yourself if you want exact documentation.
9. know your history - yeah up and down arrow keys still work to scroll through recent commands. No you should never have to resort to up and down arrows.
history
Lists out all your recent commands. By default, it lists 500 commands, but you can open up your .bashrc file and edit the HISTSIZE setting to change that.
history | grep java
Lists out all the commands you’ve used that have the word Java in it. Nice way to see a list of your recent commands pertaining to a particular program.
!123
This executes a recent command. For instance, of the "history" command reveals that "ps -el" is entry number 123, then "!123! will re-execute that command.
Ctrl+R
This is reverse search and way faster than history. Type Ctrl+R and then starting typing. Reverse search looks up any matching commands from your history, and you can hit Ctrl+R repeatedly to scroll through matches. For example, my USB stick is mounted somewhere and I always forget the path. So I Ctrl+R, start typing "media" and up comes "cd /mount/dev8/media" or something like that. I can’t remember the exact location because I never need to know it.
Alt+.
The Alt+period key combination repeats the last parameter from the last command. If you create a directory with "mkdir mydirectory" then you can type "cd ", press Alt+. and the "mydirectory" part will be filled in. Play with the period, using it is a big help.
That’s It for Now
9 tips seems like a good place to stop. I hope this helps someone. I realize I simplified the explanations of many of these commands; I wanted to make a short, useful description for beginners rather than a long, accurate description. If you have other tips or corrections then please leave a comment.
Next up… an intermediate Bash blog post… stay tuned.











Frank Pavageau said,
December 22, 2010 @ 3:33 pm
Usually, you’ll want to use “rm -r” to delete recursively, as always doing “rm -rf” is an anti-pattern (f is for force, as in “remove read-only files unconditionally”).
Hamlet said,
December 22, 2010 @ 3:35 pm
Thanks, I did not know that!
Andrey Paramonov said,
December 22, 2010 @ 6:50 pm
Take a look at ACK (http://betterthangrep.com) it’s a powerful replacement of find-grep.
BernGP said,
December 22, 2010 @ 9:27 pm
Some goodies you might find helpful in your .bashrc
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
eval “`dircolors -b`”
alias ls=’ls –color=auto’
alias dir=’dir –color=auto’
alias vdir=’vdir –color=auto’
alias grep=’grep –color=auto’
alias fgrep=’fgrep –color=auto’
alias egrep=’egrep –color=auto’
fi
alias ll=’ls -l’
alias la=’ls -A’
alias l=’ls -CF’
alias kp=’ps auxwww’ #e.g: kp | grep -P “(PID|java)”
#gradle
alias gd=’gradle –daemon ‘
alias gt=’gradle -t’
alias gij=’gradle idea’
alias gfull=’gradle clean javadoc install’
Besides this don’t forget awk which can be combined with pipes e.g
ps -ef | grep java | awk ‘{print $2}’
ls -ltr | awk ‘{print $1″\t”$8}’
Cheers and enjoy Ubuntu!
BernGP said,
December 22, 2010 @ 9:41 pm
Here is a sample of my .bashrc template
https://github.com/berngp/random-stuff/blob/master/usr/home/template/bashrc.bashrc
I also find awk useful when I need to extract some information from previous in-lline commands.
e.g.
ps -ef | grep evolution | awk ‘{print $2}’ | xargs kill -9
Alex Miller said,
December 22, 2010 @ 9:46 pm
One of my favorite little tips:
Use !$ to get the last arg from the last command, which you can use surprisingly frequently. As in:
$ vi blah/foo/goat/cheese.txt
$ git ci !$
Alex Miller said,
December 22, 2010 @ 9:47 pm
I also find something like this helpful to have in your standard setup:
alias 2u=’cd ../..’
alias 3u=’cd ../../..’
alias 4u=’cd ../../../..’
alias 5u=’cd ../../../../..’
alias 6u=’cd ../../../../../..’
alias 7u=’cd ../../../../../../..’
alias 8u=’cd ../../../../../../../..’
Alex Miller said,
December 22, 2010 @ 9:51 pm
If you happen to be on a Mac, ls with color is
ls -G
The dark blue is hard to read, so this can help:
export LSCOLORS=dxfxcxdxbxegedabagacad
Ted Naleid said,
December 22, 2010 @ 9:55 pm
Nice tips Hamlet. Bash is a good default shell for people getting used to the unix commandline and I think this is a good summary of the basics (with a few more advanced things thrown in there).
One comment on the 9th tip for the period key. Maybe it’s different in cygwin, but on all the unix system’s I’ve used, you need to hit “alt-.” not just “.” to get it to repeat the last argument of the previous command.
I also really like ctrl-a and ctrl-e for moving to the beginning and end of a command line, alt-b and alt-f to move backwards and forwards a word at a time, and ctrl-w to delete an entire word (there are more of these, but these are the 5 I use most).
Dierk König said,
December 22, 2010 @ 10:59 pm
I also like ‘ls -latr’ to list in long format, no dirs, sorted by time, reversed. It’s easy to remember as “later”.
You see the last recently used files at the end of the list, which is close to your command prompt.
Thanks for sharing
Dierk
Hamlet said,
December 23, 2010 @ 6:48 am
Alex, I believe typing the Alt+. key does the same thing as !$ except that it displays the text on the console as well. The Alt+. key has replaced !$ for me because it is easier to type and you see the results.
Hamlet said,
December 23, 2010 @ 6:49 am
Yes the key is Alt+. not just period! Oops. Muscle memory.
Tomek said,
December 29, 2010 @ 8:40 pm
links to vim’s wallpapers and stickers are broken
–
Cheers,
Tomek Kaczanowski
Robert Fischer said,
December 31, 2010 @ 10:23 pm
Instead of aliasing ls, you can set the following to get colors:
export CLICOLOR=xterm-color
export LSCOLORS=dxfxcxdxbxegedabagacad
The LSCOLORS here are the same as Alex’s above.
Robert Fischer said,
December 31, 2010 @ 10:25 pm
Dierk’s tip is vital for figuring out what Grails keeps recompiling, which tells you where you had a file name that no longer matches a class name (or similar confusion).
Robert Fischer said,
December 31, 2010 @ 10:40 pm
Two more things…
You can save yourself some headaches by being sure to use single quotes around the argument to “name” for “find”.
find . -name ‘Foo.java’
Your find may also provide “iname”, which looks for names case-insensitively.
Also, here’s a nifty function to find a file and pop it up in VI, ignoring class files and anything in SVN. (note that it assumes an iname command to find is available).
function vif {
if [ -x $1 ]; then
vi $1
else
FIND_ME=”$1*”
vi `find . -iname $FIND_ME -and -not -iname ‘*.svn*’ -and -not -ipath ‘*/.svn/*’ -and -not -iname ‘*.class’`
fi
}
Adriene Shrigley said,
January 2, 2011 @ 12:24 am
The most difficult thing is to find a blog with unique and fresh content but you definitely add value. Keep it like this.
Craig said,
January 2, 2011 @ 8:51 pm
The links in item 5 are broken.
thanks for the helpful Hamlet.
darmowe filmy erotyczne said,
January 3, 2011 @ 9:57 pm
Well I really liked reading it. This tip provided by you is very effective for proper planning.
darmowe filmy erotyczne said,
January 4, 2011 @ 10:50 am
Thank you for your valuable information.