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.