alvin's blog

Perl reverse - Reverse file contents with Perl

Working on a Unix system, I just needed to reverse the contents of a file, and thought I'd show how I ended up doing it.

My file-reversal needs

For my situation I needed to (a) get 10 lines from the end of a file, (b) reverse those ten lines, and (c) save them to another file. If my Unix system supported the -r option of the tail command this would have been a no-brainer, but it didn't, so I had to work a little harder.

Some Linux tar command examples

The Linux tar command is used to created and extract archives. An archive is one file that contains one or (usually) many other files. The name "tar" itself comes from the phrase "tape archive", but that's just an old name. I mostly just create archives and then send them over the wire these days.

Creating Unix/Linux tar archives

To create an archive of all files in your current directory, and all subdirectories, use this tar command:

The Linux more command

Linux/Unix more command FAQ: Can you share some Linux more command examples?

The Unix/Linux more command lets you scroll through large files, or large amounts of output from other commands.

Linux more command examples

To scroll through the contents of a large file named "large_file" you can use the Unix more command like this:

more large_file

As a quick aside, I see a lot of people use the Linux cat command and more commands this way:

Exploring the Linux locate command

The Linux locate command lets you easily find files in the filesystem. It works by maintaining a system-wide database of "all files which are publicly accessible". The database itself is updated periodically by a background process. Because of this approach it returns results much faster than the find command, which only looks for files when you tell it to. Depending on your system, the locate command may need to be configured initially, or it may be pre-configured to work out of the box.

Linux mkdir command examples

As its name implies, the Linux mkdir ("make directory") command lets you create new directories.

Creating a new directory in your current directory is very simple. In our first mkdir example, we'll show how to create a new directory named "dir1":

mkdir dir1

If you want to create several directories at one time you can use a mkdir command like this:

The Linux head and tail commands

Linux head/tail FAQ: Can you share some examples of the Linux head and tail commands?

Sure. The Linux head and tail commands are very similar, so I've included them here together. The head command command prints lines from the beginning of a file (the head), and the tail command prints lines from the end of files. There's one very cool extra thing you can do with the tail command, and I'll show that in the tail example commands below.

Syndicate content