Posts in the “linux-unix” category

Unix: How to redirect STDOUT and STDERR to /dev/null

Linux/Unix FAQ: On a Unix system, how do I redirect both STDOUT (standard output) and STDERR (standard error) to /dev/null, aka, “the bit bucket”?


To redirect both STDOUT and STDERR to /dev/null, use this syntax:

$ my_command > /dev/null 2>&1

With that syntax, my_command represents whatever command you want to run, and when it’s run, its STDOUT output is sent to /dev/null (the “bit bucket”), and then the 2>&1 syntax tells STDERR to go to the same place as STDOUT.

Note that this syntax can be used to redirect command output to any location, but we commonly send it to /dev/null when we don’t care about either type of output, and at some point in Unix history the /dev/null file became known as the bit bucket because of this.

So if you needed to know how to redirect both STDOUT and STDERR to the bit bucket, I hope this is helpful.

Linux: How to get CPU and memory information

Linux FAQ: How can I find Linux processor and memory information? (Also written as, How can I find Linux CPU information?, How can I find Linux RAM information?)

To see what type of processor/CPU your computer system has, use this Linux processor command:

cat /proc/cpuinfo

As you can see, all you have to do is use the Linux cat command on a special file on your Linux system. (See below for sample processor output.)

To see your Linux memory information and memory stats use this command:

Linux free memory: How to show the free memory on a Linux system

How do I show the free memory on a Linux system?

You can show free memory on a Linux system with the free command, like this:


That command returns results like this:

            total       used       free     shared    buffers     cached
Mem:       8145044    8097552      47492          0      74252    1189464
-/+ buffers/cache:    6833836    1311208
Swap:     12578884    6205424    6373460

If you prefer to see information in MB you can use the -m parameter, like this:

Linux process memory usage: How to sort ‘ps’ command output

Linux ps sort FAQ: Can you share some examples of how to sort the ps command?

Sure. In this article we'll take a look at how to sort the Linux ps command output -- without using the Linux sort command.

Before we get started, the important thing to know is that the Linux ps command supports a --sort argument, and that argument takes a number of key values, and those keys indicate how you want to support the ps output.

Here's a quick look at the --sort information from the ps command man page:

Linux gzip and gunzip: How to work with compressed files

If you work much with Unix and Linux systems you'll eventually run into the terrific file compression utilities, gzip and gunzip. As their names imply, the first command creates compressed files (by gzip'ing them), and the second command unzip's those files.

In this tutorial I take a quick look at the gzip and gunzip file compression utilities, along with their companion tools you may not have known about: zcat, zgrep, and zmore.

Use zgrep to grep a gzip (gz) file

Linux zgrep FAQ: How do I use the Linux zgrep command? (Or, How do I grep a GZ file?)

Linux users quickly learn how to use the Linux grep command on plain text files, but it takes a little longer to really you can grep gzip (gz) files as well. Here's how.

How to display the contents of a gzip/gz file ("cat a gz file")

Problem: You have a plain text file that has been compressed with the gzip command, and you'd like to display the file contents with the Unix/Linux cat or more commands.

Solution: Instead of using the cat or more commands, use their equivalents for working with gz files, the zcat and zmore commands.

Linux alias command: How to create and use Linux aliases

Unix/Linux aliases FAQ: Can you share some examples of the Linux alias command?

Using Linux aliases

Aliases in Unix and Linux operating systems are cool. They let you define your own commands — or command shortcuts — so you can customize the command line and make it work the way you want it to work. In this tutorial I'll share several Linux aliases that I use on a daily basis.

How to use the Linux ‘scp’ command without a password to make remote backups

Summary: How to create a public and private key pair to use ssh and scp without using a password, which lets you automate a remote server backup process.

Over the last two years I've ended up creating a large collection of websites and web applications on a variety of Linux servers that are hosted with different companies like GoDaddy and A2 Hosting. I recently embarked on a mission to automate the backup processes for all these sites, and as a result of this effort, I thought I'd share what I've learned here.

How to create a symbolic link in Linux

Linux FAQ: How do I create a symbolic link in Linux?

Answer: To create a symbolic link in Linux, just use the Linux ln command, like this:

ln -s OriginalFile NewSymbolicFile

As you can see from my filenames, when using the Linux ln command, you specify the name of the file you're linking to first, and then supply the name of the link second.

vi/vim editor: How to show hidden/invisible characters

vi/vim FAQ: How do I show hidden characters in the vi/vim editor?


I just learned that you can show hidden characters like tabs and end-of-line/newline characters in vi/vim with its set list command. Just go into last-line mode with the : character, then use that command:

:set list

When I do that in my current file, the $ is used to show newline characters, and TAB characters show up as ^I:

exec scala "$0" "$@"$
println("Hello, world")$
^I// tab test$

If you ever need to show hidden/invisible characters in vi/vim, I hope this is helpful.

How to view HTTP headers from the command line using curl

I’ve been working a lot on the next generation web server for the website lately, and as I’m looking at different HTTP accelerators, I keep wanting/needing to look at the headers returned by my web pages. A simple way to look at the HTTP headers from the command line is with the curl command, like this:

curl -I

Running this command against the main Google website, I see output like this:

vi quit and exit tutorial

vim quit/save/exit FAQ: How do I quit/exit vim?

Answer: This depends by what you mean by the word exit. Here's a short list of the different ways I normally quit or exit a vi/vim editor session.

vi exit - no changes made to your file (vim quit command)

If you haven't made any changes to your file you can just quit your vi (or vim) editing session like this:

[toc hidden:1]

vim editor: How do I enable and disable vim syntax highlighting?

vim editor syntax faq: How do I turn on (enable) or turn off (disable) vim syntax highlighting?

Short answer

Turning on syntax highlighting in your vim editor is usually fairly simple; you just need to issue a syntax on command, either in your current editor session, or in your vimrc configuration file. Here are a couple of quick examples.

vi end of line command

vi (vim) line navigation FAQ: What is the vi command to move to the end of the current line? (How do I move to the end of the current line in vim?)

Short answer: When in vi/vim command mode, use the "$" character to move to the end of the current line.

Other vi/vim line related commands

While I'm in the vi line neighborhood, here's a longer answer, with a list of "vi/vim go to line" commands:

How to undo and redo changes in vi/vim

vi/vim editor FAQ: How do I undo and redo changes in the vi and vim editors?


The solutions are:

  • Undo changes in vim with the u command in command mode
  • redo changes using the [Ctrl][r] keystroke

See below for more details.

vim undo (how to undo a change in vi/vim)

You “undo” changes in vi and vim with the undo command, which is the u key when you are in vim command mode. For instance, if you start with this text in your editor:

vi/vim line wrap command

Some times when you're editing a file with really long lines it's easier to work with the file if the lines don't wrap on screen. That's usually when I use the "no wrap" feature of the vi (or vim) editor.

If you don't want the lines to wrap in vi just type this command:

:set nowrap

That command tells vi not to wrap the lines. The result is that any line that was wrapped before will now scroll off the screen to the right. If you want to return it to the default wrapping mode just type this command: