Posts in the “linux-unix” category

Teleport: The Unix/Linux ‘cd’ command, improved

Summary: By keeping a history of the directories you've visited, the Teleport command is an improvement on the Unix/Linux cd command. By having a memory, Teleport lets you jump from one directory to any previously visited directory, easily.

January, 2015 Update: The Teleport command now supports Bash completion. For more details on this, see the Github INSTALL.md file.

Ubuntu ‘apt-get’ list of commands (list, update, upgrade, cheatsheet)

I have a couple of Ubuntu Linux systems, including Raspberry Pi systems, test servers, and production servers. It seems like every time I have to use an apt-get or other apt command, I always have to search for the command I need. To put an end to that, I’m creating this “apt-get reference page.” It’s very terse, as I’ve just written it for myself, but I hope it’s also helpful for others.

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.

Dozens of Unix/Linux 'grep' command examples

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

Sure. The name grep means “general regular expression parser,” but you can think of the grep command as a search or find command for Unix and Linux systems: It’s used to search for text strings and regular expressions within one or more files.

I think it’s easiest to learn how to use the grep command by showing examples, so let’s dive right in.

.

.

.

.

.

(this section left blank for the long Table Of Contents over on the right)

.

.

.

.

.

Unix/Linux: Changing many files with the same multiline string pattern

Unix/Linux tip: How to change/edit the content on thousands of files using awk and a Unix shell script.

As a note to self, today I had to delete the same lines (or line patterns) from over 10,000 files on this Unix/Linux server. Basically I was looking in each file for a specific pattern, and if that pattern was found, a ceertain number of lines should be deleted, starting at that point.

How to use the Linux sed command to delete a range of lines

In a previous blog post I demonstrated how to use sed to insert text before or after a line in many files, and in this example I'd like to demonstrate how to delete a range of lines using sed.

sed delete - How to delete a range of lines using sed

The problem I had today was that I just re-generated 99 HTML files for my Introduction to Unix/Linux tutorial using Latex2HTML, and it generates a bunch of "junk" in my HTML files that looks like this:

MacOS: How to batch-resize images with the ImageMagick mogrify command

Mac batch image resizing FAQ: Is there a built-in macOS command I can use to batch resize images and photos on my macOS computer?

This article shows a “Mac batch image resize” approach you can use from the Mac Terminal command line, and in the link I share below I also show to how to batch resize images using a Mac GUI tool.

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

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

#!/bin/sh

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

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

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.

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

my-shell-script.sh > /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).

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.

Problem

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.

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:

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:

2010-07-11

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

Linux: How to find multiple filenames with the ‘find’ command

Unix/Linux find command FAQ: How can I write one Unix find command to find multiple filenames (or filename patterns)? For example, I want to find all the files beneath the current directory that end with the file extensions ".class" and ".sh".

You can use the Linux find command to find multiple filename patterns at one time, but for most of us the syntax isn't very common. In short, the solution is to use the find command's "or" option, with a little shell escape magic. Let's take a look at several examples.