Posts in the “linux-unix” category

Linux shell script: while loop and sleep example

Linux shell script FAQ: Can you share a Linux shell script while loop example? While you're at it, can you show how to use the sleep command in the shell script while loop?

Sure. As a little background, I've written a program I call an Email Agent that periodically scans my email inbox, and does a lot of things to the inbox, including deleting the over 6,000 spams that I receive in a typical day. A recent problem with the Agent is that it runs too fast, apparently overwhelming the sendmail process on the machine that it runs on.

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.

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:

Wanting a Unix alias that takes a command-line argument (shell script, actually)

I wanted to create a Unix alias that took an argument (command-line argument), but from what I saw, that wasn’t going to be easy, so I created this little shell script to do what I want. It fails gracefully if you don’t supply a command-line argument, and runs the desired command if you do supply it:


# NAME:    scw
# VERSION: 0.1
# PURPOSE: a script that works like a Unix alias
#          that requires a command-line argument

if [ $1 ]
    echo "PURPOSE: Run 'scala-cli <filename> --watch'"
    echo "USAGE:   scw <filename>"
    exit 1

scala-cli $filename --watch

I keep a bin directory in my home directory, and it’s in my PATH, so I just drop this shell script in there and then I can use it like a Unix alias.

You can also use it as an example for any similar script you want to run, i.e., a simple shell script that handles a command-line argument.

How to redirect Unix/Linux STDOUT and STDERR to the same file/location

Unix/Linux redirection FAQ: How do I redirect Unix STDOUT and STDERR to the same file with one command?

To redirect both STDOUT and STDERR to the same file with one Unix/Linux command, use this syntax: > /dev/null 2>&1

As you can see in this command, I'm redirecting STDOUT to /dev/null as normal, and then the special 2>&1 syntax tells your Bash shell to redirect STDERR to STDOUT (which is already pointing to /dev/null).

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 (How to edit your Linux crontab file, Example Linux crontab file format), but I’ve never included a tutorial that covers the “every” options, so here are some examples to demonstrate this crontab syntax.

How to control/configure vim colors


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.

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:

[toc hidden:1]

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.

A Unix shell script to rename many files at one time

Summary: In this post I share a Unix/Linux shell script that can be used to rename multiple files (many files) with one shell script command.


You're on a macOS, Unix, or Linux system, and you'd like to be able to rename a large number of files at once. In particular, you'd like to be able to change the extensions of a large number of files, such as from *.JPG to *.jpg (changing the case of each file extension from upper case to lower case), or something similar.

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

Solution: Undo changes in vim with the u command in command mode, and 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:

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:

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.

A Unix/Linux shell script to make a quick backup of a directory

As a brief note today, I just created this little Unix/Linux shell script that I named tarquick, and it lets me quickly create a tar/gz backup of one directory. It does a lot of the tar work for you, and all you have to do is specify an optional directory name:

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

Linux/Unix FAQ: How do I redirect STDOUT and STDERR to /dev/null?

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

$ my_command > /dev/null 2>&1

With that syntax, when my_command is run, its STDOUT output is sent to /dev/null (the “bit bucket”), and then STDERR is told to go to the same place as STDOUT. 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.

How do I sort a Unix directory listing by file size?

To sort a Unix / Linux directory listing by file size, you just need to add one or more options to the base ls. On Mac OS X (which runs a form of Unix) this command works for me:

ls -alS

That lists the files in order, from largest to smallest. To reverse the listing so it shows smallest to largest, just add the 'r' option to that command:

ls -alSr

For another article related to finding large files, see my article, How to find the largest files under a directory on MacOS.

How to sort Linux ls command file output

A couple of days ago I was asked how to sort the output from the Unix and Linux ls command. Off the top of my head I knew how to sort the ls output by file modification time, and also knew how to sort ls with the Linux sort command, but I didn't realize there were other cool file sorting options available until I looked them up.

In this short tutorial I'll demonstrate the Unix/Linux ls command file sorting options I just learned.

Sorting Unix 'ls' command output by filesize

I just noticed that some of the MySQL files on this website had grown very large, so I wanted to be able to list all of the files in the MySQL data directory and sort them by filesize, with the largest files shown at the end of the listing. This ls command did the trick, resulting in the output shown in the image:

ls -Slhr

The -S option is the key, telling the ls command to sort the file listing by size. The -h option tells ls to make the output human readable, and -r tells it to reverse the output, so in this case the largest files are shown at the end of the output.

Linux shell script date formatting

Unix/Linux date FAQ: How do I create a formatted date in Linux? (Or, “How do I create a formatted date I can use in a Linux shell script?”)

I just ran into a case where I needed to create a formatted date in a Linux shell script, where the desired date format looks like this:


To create this formatted date string, I use the Linux date command, adding the + symbol to specify that I want to use the date formatting option, like this:

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