Posts in the “linux-unix” category

Linux ‘find’ command error: missing argument to -exec

Problem: I just issued a Linux find command, and got the following error message:

find: missing argument to -exec

Solution: Amazingly, it turns out that the computer is right, and I messed up my command. I entered my find command like this:

grep reverse: how to reverse the meaning of a `grep` search

Problem

You need to reverse the meaning of a search you're performing with the grep command. For instance, you've been searching for pepperoni pizza orders like this:

grep pepperoni pizza-orders.txt

and now you need to find all orders that don't have pepperoni.

Solution

Just add the -v switch to your grep search command, like this:

Linux Mint: How to change the disk encryption password

If you ever need to change the password you used to encrypt your Linux Mint hard drive — the full disk encryption of the entire hard disk you used when you installed Mint — I just found that the commands at this linuxmint.com page worked as desired.

In short, I used this command to see how my hard drive was encrypted:

sudo lsblk -o name,size,fstype,label,mountpoint

I’ll show that output when I get back on that Mint system, but it looked similar to the output shown on that linuxmint.com page, except my crypto_LUKS setting showed up on /dev/sda5.

Once I knew that was the partition I needed to work with, I used this command to see that my initial password was indeed in “Key Slot 0”:

sudo cryptsetup luksDump /dev/sda5

After that I added a new disk encryption password to Key Slot 1 using this command:

sudo cryptsetup luksAddKey /dev/sda5 -S 1

It prompted me for the password in Key Slot 0, and then asked me to enter a new password for Key Slot 1. (It also prompted me to confirm this new password.) I then rebooted the system and was able to use this new password when I was prompted to do so immediately after the computer booted up. For good measure I added another password to Key Slot 2 using this command:

sudo cryptsetup luksAddKey /dev/sda5 -S 2

I also verified that it worked by rebooting the system.

In my case I hadn’t lost the password I had in Key Slot 0, I just could never remember it, so I decided to create a new password that was plenty long, but that I could also easily remember.

(I’ll try to remember to update this article with more details the next time I’m back on that Linux Mint system.)

The Vim goto line number command

Vim FAQ: What is the Vim go to line number command? (Or, how do I go to a specific line number in vim?)

In short, you use the capital letter 'G' in Vim command mode to move to a specific line number. Here are several Vim goto line number example commands:

gvim colors: How to set the default gvim colorscheme

I just finished downloading and installing vim (gvim) on my Mac (Mac OS X 10.5.x to be precise), and then struggling for a while to set the default colorscheme, I thought I'd make a brief note here about how to set the default colorscheme properly.

Setting a default gvim color scheme (colorscheme)

As a friend of mine once, said "It's easy to do, once you know how to do it." For me, that applies to setting a default colorscheme for gvim on Mac OS X.

To set your vim default colorscheme to a theme named "slate", just follow these simple steps:

How to prompt and read user input in a Bash shell script

Unix/Linux bash shell script FAQ: How do I prompt a user for input from a shell script (Bash shell script), and then read the input the user provides?

Answer: I usually use the shell script "read" function to read input from a shell script. Here are two slightly different versions of the same shell script. This first version prompts the user for input only once, and then dies if the user doesn't give a correst Y/N answer:

Free Unix/Linux and vi/vim cheat sheets

Way back in the 1990s I created some “cheat sheets” for Unix training classes that I taught. Somewhere in the 2000s I updated them to make sure they worked with Linux as well, Here then are two Unix/Linux cheat sheets I created (way back when) that you can print out if you’re just learning Linux and the vi/vim editor:

A vim “delete blank lines” command

vim FAQ: How do I delete blank lines in vim?

To delete blank lines in vim (empty lines), use this vim last line mode command:

:g/^$/d

Here's a brief explanation of how this "vim delete blank lines" command works:

Common vi/vim commands I'm trying to remember

I've created a list of some common vim commands that I need to remember. The most important of these vim commands are related to vim syntax highlighting, auto-indent, and showing line numbers.

Here's a short list of these vi commands, all of which can be issued while you are in vi "command mode":

The vim “delete all lines” command

vim FAQ: How do I delete all lines in the current text file in vim?

To delete all lines in vim, use this command:

:1,$d

This "vim delete all lines" command can be read like this:

  • Beginning at line 1, and
  • Ending at the last line in the file (represented by the '$'),
  • Delete each line.

I hope this vim "delete all lines" command example has been helpful. If you need any more details/discussion just leave a command below.

Linux find command: How to find files not matching a pattern

Unix/Linux find "patterns" FAQ: How do I find files or directories that don't match a specific pattern (files not matching a regex pattern, or filename pattern)?

In my case I just ran into a situation where I needed to find all files below the current subdirectory that are NOT named with the filename pattern *.html . Fortunately with the newer Unix/Linux find syntax this solution is pretty easy, you just include the -not argument, like this:

A Bash ‘for’ loop to iterate over a file that has blank spaces in its lines

I just had to write a Linux bash shell script that has a for loop that reads a file, and that file contains lines with blank spaces in it. This sounds simple, but blank spaces cause major problems in a Bash for loop.

Fortunately there’s a simple solution: Before the for loop, declare the input field separator to be a newline character, as shown in this for loop code:

LaTeX: How to set the Table of Contents (TOC) depth

LaTeX question: For a technical document I'm creating with LaTeX, there are a lot of sections that have a repeated/consistent format. This is exactly what I want, but for a 160 page document, I ended up with a 15-page table of contents (TOC). Is there are way to control the "depth" of the table of contents?

After a little research ... yes, I can. Here's some sample code that I put in the LaTeX preamble, just before the \tablofcontents tag:

Apache RedirectMatch wildcard examples

Apache Redirect 301 FAQ: How can I redirect many old web pages using the Apache Redirect or RedirectMatch syntax and wildcard patterns (regex patterns)?

I'm currently trying to fix a lot of URLs that I more or less intentionally broke when I deleted the old "directory" portion of this website. In short, after removing the directory, no URL at "/Dir" work any more, so I have thousands of broken URLs (technically "URIs") that look like this:

vi/vim search and replace: how to replace all tabs with spaces

vi/vim question: How do I convert all occurrences of ABC to XYZ in a file using the vi/vim editor?

Answer: This is actually easy to do with the vi or vim editor. Just go into "last line mode" by typing the ":" character, then enter a command like this to replace all occurrences of the string ABC with the string XYZ:

How to gracefully restart Nginx

Nginx FAQ: How do I gracefully restart Nginx?

This used to be harder, but these days all you have to do is run this command:

$ sudo service nginx restart

Note that you will probably want to make sure your configuration is correct with this command before running that command:

$ nginx -t

Configuring an Nginx default website

I’m configuring Nginx as my primary web server, with other Apache servers behind it, and I wanted to configure Nginx to serve up a blank page whenever someone tried to hit my server’s IP address, instead of one of the websites that’s hosted on the server. That is, I wanted to serve up a blank page rather than the default Nginx page.

In short, I added this setup information to my nginx.conf configuration file: