Linux Command Line Tips

Most Linux users spend quite a bit of time using the command line. It’s fast, efficient, and gives you bonus “cool points” with every command. If you’re anything like me, you’ve thought from time to time, “It sure would be nice to be able to do X” and then later stumbled across that exact feature. Here’s a short list of some useful tips and commands for Linux.

Previous commands
The majority of you probably know about pressing up to go to previous commands or pressing tab to auto-complete filenames and the like. You probably even know about (but perhaps rarely use) the history command. One additional feature you might not know about is the ability to quickly and easily search for a previous command. Try CTRL+R and then start typing the first few letters of a command you previously ran.

Moving around
Obviously, you can use the arrow keys to move around on the command line, but there are several other shortcuts:

  • CTRL+A = move cursor to beginning of line
  • CTRL+E = move cursor to end of line
  • CTRL+U = deletes everything before the cursor
  • CTRL+K = deletes everything after the cursor
  • CTRL+W = deletes the “word” before the cursor (all text up to the previous space)
  • CTRL+Y = paste previously deleted text (similar to undo and can be repeated)
  • CTRL+I = similar to tab but for whatever text your cursor is on
  • CTRL+L = similar to the “clear” command

Background/Foreground processes
You can press CTRL+Z to put the current process into the background and enter fg to return that process to the foreground. You can also start a process in the background by appending an ampersand (‘&’) to your command (e.g., “cp backup/bigfile.gz . &”). You can view the current processes with the jobs command, and you can selectively bring them to the foreground by using fg X where ‘X’ is equal to the job number (e.g., “fg 3″).

Miscellaneous
If you haven’t started using Vim, you’re missing out. The learning curve is a little steep, but vimtutor can help. You may need to install the tutor (“sudo apt-get install vim-runtime” sets it up in Ubuntu), but it is well worth the little effort. What some users don’t realize is that you can very effectively use the shell through Vim. For instance, you can highlight text in Visual Mode, then run :!sort (“:!” is how you start any single shell command). That passes the highlight text to the Linux program, “sort,” and inserts the results. You can also go entirely into a shell with :sh so that you can accomplish more complex tasks (of course you can always CTRL+Z as well). Obviously, there is a lot more that Vim can do, but I thought this little bit was most appropriate for a post about the Linux command line.

I commonly search for strings in multiple files (particularly when editing PHP). I learned to do that using find . -name “*.php” -exec grep -il “string” {} \;, where “string” is the string you wish to search for. The “-il” keeps it case insensitive and simply lists the files the string occurs in. With Linux there are many different ways to do anything, so find what works best for you.

Sometimes you need to have a command run even if you log out of the server. For this, you use “nohup” plus the normal command. For example, you might type nohup ./autorun & to start a program in the background and allow it to continue running when you disconnect.

There are countless other tips out there. I suggest that any time you think, “It would be really helpful if I could do X,” stop what you are doing and find out how to do it. Sure, you can press the left arrow repeatedly to get to the start of a command line, but learning CTRL+A will save you time. Just like when learning Vim, if you force yourself to do things “the Vim way,” you will be much better off than if you try to use the arrow keys like in another program, so force yourself to do things “the Linux way,” and double your productivity (or at least impress a few friends).

If you have any additional tips, feel free to post them.


3 Responses to “Linux Command Line Tips”

  1. 1 linuxer

    Tips are useful. But newbie should first know the linux commands yeah ;)
    for command resources.

    http://www.plentyofcode.com/2007/07/most-frequently-used-linux-commands.html

  2. 2 Gordaen

    I came across a much more complete list of bash shortcuts today.

  3. 3 Ghassem Tofighi

    Very Good Tutorial, Thanks

Leave a Reply