Posts in the “linux-unix” category

vi/vim: How to control/configure editor colors (color settings)

[toc]

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:

Linux crontab “every X minutes or hours” examples

[toc]

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 — such as How to edit your Linux crontab file and Example Linux crontab file format — but I’ve never included a tutorial that covers the crontab “every” options, so here are some examples to demonstrate how to run crontab commands at time intervals, like every minute, every five minutes, every ten minutes, every hour, and so on.

How to use ‘awk’ to print columns from a text file (in any order)

Unix/Linux FAQ: How can I print columns of data from text files on Unix/Linux systems?

Background

One of my favorite ways to use the Unix awk command is to print columns of data from text files, including printing columns in a different order than they are in in the text file. Here are some examples of how awk works in this use case.

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:

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 Linux shell script to rename files with a counter and copy them

As a brief note today, I was recently looking for all Messages/iMessage files that are stored on my Mac, and I used this shell script to copy all of those files — many of which have the same name — into a directory named tmpdir, giving them all new names during the copy process:

# WARNING: back up your files before running this script.
#          if something is wrong, you may lose them all.
count=1
for i in `cat myfiles`
do
    fname=`basename $i`
    cp $i tmpdir/${count}-${fname}
    count=`expr $count + 1`
done

More Linux find command examples

The Linux find command is used to locate files and directories based on a wide variety of criteria. I'll try to cover the most common find examples here.

Basic find command examples

To find a file or directory named "foo" somewhere below your current directory use a find command like this:

find . -name foo

If the filename begins with "foo" but you can't remember the rest of it, you can use a wildcard character like this:

[toc hidden:1]

Unix/Linux shell script reference page (shell cheat sheet)

Linux shell script test syntax

All of the shell script tests that follow should be performed between the bracket characters [ and ], like this:

if [ true ]
then
  # do something here
fi

Very important: Make sure you leave spaces around the bracket characters.

I'll show more detailed tests as we go along.

Linux shell file-related tests

To perform tests on files use the following comparison operators:

[toc hidden:1]

vim search commands

vim search FAQ: How do I search in vim?

There are a few commands you can use to search in the vi or vim editors. The main thing to remember is that the '/' key lets you search forward in a file, and the '?' key lets you search backwards in a file for whatever text you are looking for.

Remove control-m characters in vi/vim's binary mode

vi/vim FAQ: How can I remove control-m ^M characters in a text file using vi or vim?

“DOS format” message in vim

If you’ve ever opened a text file with vi (or vim) and saw a message on the bottom of the screen that says “dos” or “dos format”, there’s a reason for this. The file was probably created on a DOS or Windows computer, and it contains extra binary characters that are not normally found in a file created on a Unix or Linux system.

[toc hidden:1]

vim copy and paste commands

vim copy and paste FAQ: How do I copy and paste in vim (the vi or vim editor)?

In this short vi/vim tutorial I thought I'd share the most common vim copy and paste commands I use on a daily basis. I've been using vi (and vim) for almost 20 years now (wow), and these are the most common copy/paste commands I use.

[toc hidden:1]

vi quit and exit tutorial

vim quit/save/exit FAQ: How do I quit/exit vim?

Answer: This depends by what you mean by the word exit. Here's a short list of the different ways I normally quit or exit a vi/vim editor session.

vi exit - no changes made to your file (vim quit command)

If you haven't made any changes to your file you can just quit your vi (or vim) editing session like this:

[toc hidden:1]

vim modes - the three modes of vi/vim

The vi editor can be a little difficult to learn, so I've been writing some vi tutorials here recently. One of the first things to know about vi is that it typically functions in three different modes:

  1. Command mode
  2. Insert mode
  3. Last line mode

Here's a quick description of each vi mode.

[toc hidden:1]

vim editor: How do I enable and disable vim syntax highlighting?

vim syntax faq: How do I turn on (enable) or turn off (disable) vim syntax highlighting?

Turning on syntax highlighting in your vim editor is usually pretty simple; you just need to issue a syntax on command, either in your current editor session, or in your vimrc configuration file. Here are a couple of quick examples.

Turn vim syntax highlighting on

To enable syntax highlighting in your current vim editor session just issue this command:

[toc hidden:1]

Unix `sed` examples: how to insert text before and after existing lines

If you ever need to use the Unix/Linux sed command to insert text before or after a line of text in an existing file, here's how I just ran several sed commands to update my old Function Point Analysis tutorial to have a format that doesn't look like it was created in the 1990s.

This tutorial consists of over 40 files, and I had eight changes I wanted to make each file. So I had two choices: modify each file by hand over the next six hours, or run a series of sed commands and be done in 30 minutes. (I chose the sed commands.)

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]