Posts in the “linux-unix” category

vi/vim video tutorials

Woo-hoo, I've always wanted to create a vim video tutorial series, and now that I have the software to do it, I'm finally embarking on this adventure.

My vi/vim editor video tutorial - Lesson
1, Introduction

Dozens of Unix/Linux 'grep' command examples

[toc]

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

Dozens of Unix/Linux 'find' command examples

[toc]

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.

Installing Wiki.js on Ubuntu 20.04, with Postgresql

This probably won’t make sense to anyone else, but these are my notes related to installing Wiki.js and Postgresql on an Ubuntu 20.04 system. Everything here is related to setting up a new Ubuntu system and then running Wiki.js:

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:

A Linux crontab mail command example

Linux crontab mail FAQ: Can you share an example of a Linux crontab entry you use to send email on a regular basis?

Solution: Here’s the source code for a really simple Linux mail script that I used to send an email message to one of my co-workers every month. This script used the Unix or Linux mail command to email a file to her that showed a list of all the websites on our server that she needed to bill our customers for.

An example Linux crontab file

Linux crontab format FAQ: Do you have an example of a Unix/Linux crontab file format?

I have a hard time remembering the crontab file format, so I thought I’d share an example crontab file here today. The following file is the root crontab file from a CentOS Linux server I use in a test environment.

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 alias command: How to create and use Linux aliases

[toc]

Unix/Linux aliases FAQ: 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.

A sed command to display non-visible characters in a text file

I just ran into a need to see what non-printable (non-visible?) characters were embedded in a text file in a Unix system, when I remembered this old sed command:

sed -n 'l' myfile.txt

Note that the character in that sed command is a lower-case letter "L", and not the number one ("1").

This command shows the contents of your file, and displays some of the nonprintable characters with the octal values. On some systems tab characters may also be shown as ">" characters.

Shell script error: bad interpreter - No such file or directory

Some times when you take a file from a DOS/Windows system and move it to a Linux or Unix system you'll have problems with the dreaded ^M character. This happened recently when I moved an Ant script from a Windows system to my Mac OS X system. When I tried to run the shell script under the Mac Terminal I got this "bad interpreter" error message:

How to remove extended ASCII characters from Unix files with the 'tr' command

When working with text files on a Unix/Linux system, you'll occasionally run into a situation where a file will contain extended ASCII characters. These extended characters will generally appear to begin with ^ or [characters in your text files. For instance, the vi/vim editor will show ^M characters in DOS text files when they are transferred to Unix systems, such as when using the ftp command in binary transfer mode. Oftentimes, you'll want to easily delete these characters from your files.

Unix/Linux: Find all files that contain multiple strings/patterns

When using Unix or Linux, if you ever need to find all files that contain multiple strings/patterns, — such as finding all Scala files that contain 'try', 'catch', and 'finally' — this find/awk command seems to do the trick:

find . -type f -name *scala -exec awk 'BEGIN {RS=""; FS="\n"} /try/ && /catch/ && /finally/ {print FILENAME}' {} \;

As shown in the image below, all of the matching filenames are printed out. As Monk says, you’ll thank me later. :)

(I should mention that I got part of the solution from this gnu.org page.)

Update: My File Find utility

For a potentially better solution, see my File Find utility, which lets you search for multiple regex patterns in files.

How to show hidden/invisible characters in vi/vim

I just learned that you can show hidden characters like tabs and end-of-line/newline characters in vi/vim with its set list command. Just go into last-line mode with the : character, then use that command:

:set list

When I do that in my current file, the $ is used to show newline characters, and TAB characters show up as ^I:

#!/bin/sh$
exec scala "$0" "$@"$
!#$
$
println("Hello, world")$
$
^I// tab test$
$
$

If you ever need to show hidden/invisible characters in vi/vim, I hope this is helpful.

An awk script to extract source code blocks from Markdown files

I just wrote this awk script to extract all of the Scala source code examples out of a Markdown file. It can easily be converted to extract all of the source code examples out of an Asciidoc file, which is something else I will do with it eventually.

Here’s the awk script:

BEGIN {
    # awk doesn’t have true/false variables, so
    # create our own, and initialize our variable.
    true = 1
    false = 0
    printLine = false
}

{
    # look for ```scala to start a block and ``` to stop a block.
    # `[:space:]*` that is used below means “zero or more spaces”.
    if ($0 ~ /^```scala/) {
        printLine = true
        print ""
    } else if ($0 ~ /^```[:space:]*$/) {
        # if printLine was true, we were in a ```scala block,
        # so print the end matter, then make printLine false
        # so printing will stop
        if (printLine == true) {
            print "```"
        }
        printLine = false
    }
 
    if (printLine) print $0
}

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

Mac batch image resizing FAQ: Is there a built-in Mac OS X command I can use to batch resize images and photos on my Mac OS X  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.

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.

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

[toc]

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.