Scala, Java, Unix, MacOS tutorials (page 393)

Perl FAQ: "How can I process every file in a directory that matches a certain filename pattern?"

There are several ways to do this, but I normally use the glob function, because I can remember the syntax pretty easily.

Let's look at a couple of glob examples.

Using the glob operator

You can use the glob operator to get a list of all files in the current directory like this:

Perl file copying FAQ: How do I copy a file in Perl?

You can copy a file in Perl using the File::Copy module. Here's a quick example:

A common Perl FAQ is "How do I do (something) for every file in a directory?"

Here's some sample code that will show you how to loop through each file in a directory:

Perl file writing FAQ: How can I test to see if I can write to a file in Perl?

In Perl it's very simple to determine whether you can write to a file. Just use the Perl -w file operator, as shown in this example:

Perl file test FAQ: How can I run a Perl test to see if I have read access on a file?

Using Perl it's very simple to determine whether you can read a file. Just use the -r file operator, as shown in this example:

Perl has a couple of convenient file operators that let you determine when a file was last accessed or modified in units of days. These operators are:

-M  The modification age of the file, in days
-A  The access age of the file, in days

A quick Perl file test example

You can use these Perl file test operators in your scripts to determine when files are getting "old" (where the definition of "old" is up to you).

This document provides a list of the most commonly-used Perl file test operators.

Operators to determine whether a file or directory exists

Here are the most common Perl file test operators that can be used to determine whether a file or directory exists:

-e  File or directory name exists
-z  File exists and has zero size
-s  File or directory exists and has non-zero size

All of those tests can be executed as shown in the following source code example:

Perl FAQ: How do I determine the size of a file from within a Perl program/script?

Solution -  the Perl stat function

Just use the Perl stat function to get the file size. Here is a quick example.

Example - get the file size with Perl stat

To determine size of a file named foo.txt, just use a little Perl code like this:

How to use the Perl stat function.

Problem: Using Perl, you need to determine the last time a file was accessed (read) or updated (modified).

Solution: Use the Perl stat function to get this information. Let's look at a couple of short examples to see how this works.

Perl stat - getting the file access time

To determine the last access (read) time of a file named foo.txt, use this sample Perl code:

Perl FAQ: How do I delete a list of files in a Perl script?

This is very similar to deleting one file in a Perl program. You just use Perl's unlink function, but this time pass it a list of filenames. Let's take a look at a simple example.

A simple example

First, create some sample files in the current directory:

You delete a file in Perl programs using the Perl unlink function. Let's take a look at a simple example.

A simple Perl delete (Perl unlink) example

First, we need a test file we can delete from our Perl script. Let's create a sample file in the current directory using the Unix touch command, like this:

touch delete-me.txt

(This creates an empty file named delete-me.txt.)

Many times in Perl applications you need to be able to create a temporary file (a Perl temp file). In this scenario you don't want to get into the system-specific details of temporary directories and things like that; you just want to create and use a temporary file.

Here are some Perl code snippets that should help get you started down this road.

vi/vim editor FAQ: How do I undo and redo changes in the vi and vim editors?

Solution

The solutions are:

  • Undo changes in vim with the u command in command mode
  • redo changes using the [Ctrl][r] keystroke

See below for more details.

vim undo (how to undo a change in vi/vim)

You “undo” changes in vi and vim with the undo command, which is the u key when you are in vim command mode. For instance, if you start with this text in your editor:

Vim FAQ: How do I configure Vim to wrap lines (or not wrap lines) of text?

Answer: To get Vim to not wrap text, issue the "vim set nowrap" command, like this:

:set nowrap

By default vim will wrap long lines in the editor, but this command changes that display, so long lines will now go off-screen to your right.

vim set wrap - how to get back to wrapping mode

Getting back to the vim editor's normal line wrap mode is also simple. Just issue a very similar "vim set wrap" command:

A vi/vim FAQ I often get is, "Does vi have any sort of auto-complete feature?"

Answer: Yes, just use the [Ctrl]p keystroke after you've entered some text.

Auto-complete example

Here's a quick example. Assume you have this text in a vi/vim editor:

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 FAQ: How do I show line numbers in the vi/vim editor?

I’m frequently asked if there’s a way to show line numbers in vi (or vim). That is, can you take a normal vim display like this:

aaa
bbb
ccc

and then get vim to show line numbers before each line in the editor, like this:

1 aaa
2 bbb
3 ccc

The answer is a resounding yes.

How to show vim line numbers (vim set number)

You show vim line numbers by issuing this vim “set number” command:

vi/vim FAQ: Can you provide some examples of how to create vim macros?

Here's a quick example of how to define macros that you can use in the vim editor (or vi editor). As you can tell by looking at them, I use these particular vim macros when editing HTML files.

Define vim macros with the vim map command

Step 1 in the process or creating vim macros is to edit the vi/vim startup file in your home directory. On Unix, Linux, and Mac OS X systems, this file is named .vimrc.

So, move to your home directory like this:

Here's a quick list of the most common vi and vim editor navigation commands I use every day.

Moving on the current line

Here are the most common navigation commands for moving around the current line:

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.