Posts in the “linux-unix” category

Notes about setting up HTTPS on websites using LetEncrypt and certbot

As a note to self, I added SSL/TLS certificates to a couple of websites using LetEncrypt. Here are a couple of notes about the process:

  • Read the LetEncrypt docs
  • They suggest using certbot
  • Read those docs, and follow their instructions for installing the packages you’ll need
  • Make sure your server firewall rules allow port 443 (You may get an “Unable to connect to the server” error message if you forget this part, as I did)
  • After making some backups, run this command as root (or you may be able to use the sudo command):

Linux: Recursive file searching with `grep -r` (like grep + find)

Unix/Linux grep FAQ: How can I perform a recursive search with the grep command in Linux?

Two solutions are shown next, followed by some additional details which may be useful.

Solution 1: Combine 'find' and 'grep'

For years I always used variations of the following Linux find and grep commands to recursively search sub-directories for files that match a grep pattern:

find . -type f -exec grep -l 'alvin' {} \;

This command can be read as, “Search all files in all sub-directories of the current directory for the string ‘alvin’, and print the filenames that contain this pattern.” It’s an extremely powerful approach for recursively searching files in all sub-directories that match the pattern I specify.

Here’s an explanation of those command-line arguments:

  • The name of the command is find
  • The “.” means “current directory”
  • The -type f part means “only search for files (not directories)”
  • -exec means “execute the following command”
  • grep -l 'alvin' is the command that searches the files for the string “alvin
  • The {} \; part of the command is a little unusual ... I think it’s an old artifact of the find command, but you can think of that area as holding each file that the find command finds, and then grep operates on each one of those files, one file at a time

Solution 2: 'grep -r'

However, I was just reminded that a much easier way to perform the same recursive search is with the -r flag of the grep command:

grep -rl alvin .

As you can see, this is a much shorter command, and it performs the same recursive search as the longer command, specifically:

  • The -r option says “do a recursive search”
  • The -l option (lowercase letter L) says “list only filenames”
  • As you’ll see below, you can also add -i for case-insensitive searches

If you haven’t used commands like these before, to demonstrate the results of this search, in a PHP project directory I’m working in right now, this command returns a list of files like this:

./index.tpl
./js/jquery-1.6.2.min.js
./webservice/ws_get_table_names.php

More: How to search multiple sub-directories

Your recursive grep searches don’t have to be limited to just the current directory. This next example shows how to recursively search two unrelated directories for the case-insensitive string "alvin":

grep -ril alvin /home/cato /htdocs/zenf

In this example, the search is made case-insensitive by adding the -i argument to the grep command.

Using egrep recursively

You can also perform recursive searches with the egrep command, which lets you search for multiple patterns at one time. Since I tend to mark comments in my code with my initials ("aja") or my name ("alvin"), this recursive egrep command shows how to search for those two patterns, again in a case-insensitive manner:

egrep -ril 'aja|alvin' .

Note that in this case, quotes are required around my search pattern.

Summary: `grep -r` notes

A few notes about the grep -r command:

  • This particular use of the grep command doesn’t make much sense unless you use it with the -l (lowercase "L") argument as well. This flag tells grep to print the matching filenames.
  • Don’t forget to list one or more directories at the end of your grep command. If you forget to add any directories, grep will attempt to read from standard input (as usual).
  • As shown, you can use other normal grep flags as well, including -i to ignore case, -v to reverse the meaning of the search, etc.

Here’s the section of the Linux grep man page that discusses the -r flag:

-R, -r, --recursive
Read all files under each directory, recursively; this is
equivalent to the -d recurse option.

  --include=PATTERN
  Recurse in directories only searching file matching PATTERN.

  --exclude=PATTERN
  Recurse in directories skip file matching PATTERN.

As you’ve seen, the grep -r command makes it easy to recursively search directories for all files that match the search pattern you specify, and the syntax is much shorter than the equivalent find/grep command.

For more information on the find command, see my Linux find command examples, and for more information on the grep command, see my Linux grep command examples.

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:

Dozens of Unix/Linux 'find' command examples

Linux/Unix FAQ: Can you share some Linux find command examples?

Sure. The Linux find command is very powerful. It can search the entire filesystem to find files and directories according to the search criteria you specify. Besides using the find command to locate files, you can also use it to execute other Linux commands (grep, mv, rm, etc.) on the files and directories that are found, which makes find even more powerful.

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 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:

vi/vim delete commands and examples

vi/vim editor FAQ: Can you share some example vi/vim delete commands?

The vi editor can be just a little difficult to get started with, so I thought I’d share some more vi commands here today, specifically some commands about how to delete text in vi/vim.

vi/vim delete commands - reference

A lot of times all people need is a quick reference, so I’ll start with a quick reference of vi/vim delete commands:

vi/vim: How to control/configure editor colors (color settings)

vim colors FAQ: Can you provide details on how to control/configure colors in the vim editor (i.e., vim color settings)?

Sure. When using vim syntax highlighting, a common complaint is that the default color scheme is a little too bold. In this article I'll try to demonstrate how you can change the colors in vim to be a little more pleasing, or at least be more in your control.

The Vim “page up” and “page down” keystrokes

Vim FAQ: What are the Vim page up and page down keystrokes?

Short answer:

The Vim page up keystroke is [Control][b]. You can remember the 'b' by thinking "back".

The Vim page down keystroke is [Control][f]. You can remember the 'f' by thinking "forward".

More Vim page up and page down keys

I use those two Vim keystrokes most of the time, but you can use other keystrokes in Vim to move up and down, as shown here:

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”?

Solution

To redirect both STDOUT and STDERR to /dev/null on Unix/Linux systems, 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 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: How do I create an alias in Linux? 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.

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:

free

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 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 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.