Posts in the “linux-unix” category

The Linux 'rm' command (remove files and directories)

Linux FAQ: How do I delete files (remove files) on a Unix or Linux system?

The Linux rm command is used to remove files and directories. (As its name implies, this is a dangerous command, so be careful.)

Let's take a look at some rm command examples, starting from easy examples to more complicated examples.

Unix/Linux rm command examples - Deleting files

In its most basic use, the rm command can be used to remove one file, like this:

How to read Unix/Linux shell script command line arguments

Unix/Linux shell script args FAQ: How do I access Unix or Linux shell script command line arguments?

You can process Unix shell script command line arguments in at least two ways:

  • With getopts, when you want to handle options like -n 10
  • By number, such as $1, $2, etc.

I show both of these solutions below.

[toc hidden:1]

How to use the Linux sed command to edit many files in place (and make a backup copy)

Warning: The following Unix sed commands are very powerful, so you can modify a lot of files successfully — or really screw things up — all in one command. :)

Yesterday I ran into a situation where I had to edit over 250,000 files, and with that I also thought, “I need to remember how to use the Unix/Linux sed command.” I knew what editing commands I wanted to run — a series of simple find/replace commands — but my bigger problem was how to edit that many files in place.

A quick look at the sed man page showed that I needed to use the -i argument to edit the files in place:

How to type smart quotes on Ubuntu Linux

Note: I don’t know why, but all of the images for this article have been lost. I’ll replace them when I have some free time (but free time is scarce these days).

There seem to be a few different ways to type “smart quotes” on Ubuntu Linux, including using keys (keystrokes) like AltGr and Compose. In this tutorial I’ll document an approach that works best for me: creating macros I can assign to simple keystrokes rather than having to use more-complicated keystrokes.

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