Posts in the “linux-unix” category

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?

Solution

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:

#!/bin/sh$
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.

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

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

Solution

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.

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 alvinalexander.com 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 http://example.com/

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?

Solution

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:

How to display the contents of a gzip/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.

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:

How to use the Linux 'lsof' command to list open files

Linux “open files” FAQ: Can you share some examples of how to show “open files” on a Linux system (i.e., how to use the lsof command)?

The Linux lsof command lists information about files that are open by processes running on the system. (The lsof command itself stands for “list of open files.”) In this brief article I’ll just share some lsof command examples. If you have any questions, just let me know.

Linux/Unix: How to edit your crontab file with “crontab -e”

Linux crontab FAQ: How do I edit my Unix/Linux crontab file?

I was working with an experienced Linux sysadmin a few days ago, and when we needed to make a change to the root user crontab file, I was really surprised to watch him cd to the root user’s cron folder, make changes to the file, then do a kill -HUP on the crontab process.

Thinking he knew something I didn’t know, I asked him why he did all of that work instead of just entering 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 crontab “every X minutes or hours” examples

Linux crontab FAQ: How do I schedule Unix/Linux crontab jobs to run at time intervals, like “Every five minutes,” “Every ten minutes,” “Every half hour,” and so on?

Solution

I’ve posted other Unix/Linux crontab tutorials here before — such as How to edit your Linux crontab file and Example Linux crontab file format — but I’ve never included a tutorial that covers the crontab “every” options, so here are some examples to demonstrate how to run crontab commands at time intervals, like every minute, every five minutes, every ten minutes, every hour, and so on.

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:

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.