Using the command line effectively is a vital part of a Linux systems administrator’s skills. In this tip we’re providing five more cool and quirky Linux command line tricks that can help you efficiently work on your Linux systems.
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
Reading files on Linux usually involves the more or less commands. The more command reads forward in a file, and therefore loads the whole file when starting up. The less command allows movement forwards and backwards in a file, and only reads the next portion of the file it’s loading. To save time, a handy command trick is to alias the less command to more:
$ alias more=less
Add this to your .bashrc or .bash_profile file to make it a permanent alias.
The less command is easy to use like more scrolling forwards can be done with the space bar or the f key. You can scroll backwards a screen at a time with the b key (there are other commands you can see in the less command’s man page.
Another way to output the content of a file is the cat command, which dumps the content of the file to the screen from top to bottom. What many don’t know is that cat has a sister command called tac. As you’d imagine the tac command also dumps the contents of a file but backwards, from bottom to top. This is useful when reading from a log file or anything else where the output to care about is at the bottom of the file.
Less useful but interesting to know about is the shuf command, which shuffles the input it receives. Run:
$ shuf filename
And it will return the contents of the file in a random order.
No one can hear you screen
Got multiple SSH sessions to the same server? You’re doing things wrong. Opening multiple SSH sessions is both a waste of compute resources and cumbersome to manage. Especially if you’re using a Window Manager or a tool like Putty where multiple terminals quickly take up substantive screen real estate. A much better alternative is to use tools like screen or tmux to allow you to have multiple terminals inside a single SSH session.
The screen command is very easy to use. First, you need to ensure you’ve got screen installed. On both Red Hat and Ubuntu hosts this means installing the screen package, for example:
$ sudo yum install screen
Then from a terminal session type:
Looks like nothing happened right? Well you’d be wrong. Type a command like top and you should see your processes displayed. Now type Ctrl-a-c (Hold down Ctrl and then a and then c) and a new terminal window will pop up. Your old window is still available though. In fact you can go back to it by typing Ctrl-a-p and you can cycle through available windows with Ctrl-a-a. Or you can select a window by number, in order of their creation, the first window with Ctrl-a-0 and Ctrl-a-1 and so on. More windows can be created using additional Ctrl-a-c commands and windows can be closed by typing exit on the command line. When the last window is closed screen will exit.
The really clever thing about screen is that you can detach your screen and return to it. Type Ctrl-a-d to detach the screen and you’ll be returned to the original terminal window from which you ran screen. You can then exit the host. When you log back in you can run:
$ screen -r
And your screen session will be restarted and all your terminals will be available (assuming no one has killed the process in the meantime or restarted the host). You can find lots of other useful information at the Screen manual page.
Another alternative to screen is Tmux, which is more fully featured and more complex tool that is useful for multi-user screen sharing. A common use might be remote pair programming.
HTTP server: When you need a Web server now!
When you need to quickly deliver files, one of the easiest ways is through a HTTP server. I do this by quickly creating a Ruby Web server from the command line:
$ ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => 3125, :DocumentRoot => Dir.pwd).start'
This will serve out the current directory on port 3125:
You can also do the same thing with Python:
$ python -m SimpleHTTPServer
This serves out the current directory on port 8000. (You’ll need to open either port through your firewall.)
It’s important to remember anyone can download the files you serve out so make sure you know what files you are providing, and not offer up anything that you don’t want to share!
A better process: pgrep
You may frequently find yourself typing something like:
$ ps -A | grep ‘ruby’
1680 ? 00:05:45 ruby
30250 ? 00:06:39 ruby
But there’s a much simpler approach you can use to display processes without needing to pipe the ps output: pgrep.
$ pgrep -l ruby
You can also query all process being run by a particular user, like so:
$ pgrep -u root
This will return all processes running as root.
Lastly, you can also do some nifty grep-like tricks:
$ pgrep -lv `whoami`
This will list the process name and IDs for all processes not being run by your user as returned by the whoami command (as in grep, the -v option negates).
You may be aware of the aspell (and ispell) libraries that allow you to spell check on Linux. But you can run the aspell command alone or integrated with other applications. For example, to spell check a file via the command line you can run aspell like so:
$ aspell -c filename.txt
This will launch an interactive spell checking application that you can use to go through and update any spelling errors in your file.
But sometimes you just want to know how to spell a word, which is when look, a command line spell checker can be handy. Type look and part of the word you’d like to spell, for example:
$ look vendo
And it’ll return a full list of the words starting with vendo.
ABOUT THE AUTHOR: James Turnbull is a former IT executive in the banking industry and author of five technology books, including Hardening Linux, Pulling Strings with Puppet and Pro Linux System Administration. He has been involved in IT Operations for nearly twenty years and is an advocate of open source technology. He currently works at Puppet Labs as head of operations. James manages operations, documentation, testing, support and release management for Puppet Labs.