Posts in the “linux-unix” category

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.
for i in `cat myfiles`
    fname=`basename $i`
    cp $i tmpdir/${count}-${fname}
    count=`expr $count + 1`

Linux alias command: How to create and use Linux aliases


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.

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 ]
  # do something here

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

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

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:

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.