Command-line Basics: Working with Files


Gone are the days of carrying a stack of punch cards to the computer lab to see if it will run. Gone are the days of owning designer cases to store your varying sizes of floppy disks. Pretty good chance your daily driver workstation doesn’t even have a way to read media outside of a USB drive. While the mediums have changed, the ability to work with files from the command-line has stayed virtually the same.

In this article, we’re going to discuss how to work with files. Moving and copying files. Throwing them away without the safety net of the “recycle bin”. Even reading the contents of a file can all be accomplished without ever taking your hands off of the keyboard.

Getting Started

To get started, we’re going to need some files. I’m going to assume you already have some files on your computer but for the sake of demonstration we can create a few new files to play with. You’ll thank me when we start deleting things.

I prefer to mess around in /tmp since clean up is done automatically when you reboot:

$ cd /tmp
$ mkdir reptiles
$ cd reptiles
$ touch {alligator,crocodile,snake}.txt
$ ls >> toc.txt

This is just the quick and dirty to create a few files one of which is populated with some text. Want to get deeper into creating both files and directories? Check out this article: Command-line Basics: Creating Files and Directories

Safety First

You see my friend, the command-line can be a very scary place. Things like moving and deleting files are fairly instant and very permanent.

GUI applications with all of their faults tend to do one thing right, and that’s attempting to protect you, the user. Dialog upon dialog making sure you know what’s about to happen and giving you one last opportunity to cancel.

Nearly everybody’s had at least one freak out moment after a file is deleted, only to find and restore it from the ever faithful trash can icon.

What I’m trying to say is, files don’t just magically delete themselves, but throwing around commands can definitely result in data loss if you’re not careful.

Between you and me, I’m not very careful. What I am is resourceful. Resourceful enough to check the man pages for the commands I run and try to set myself up for success instead of failure.

With that, each of the destructive commands we’re about to use accepts an -i flag to help keep you from grieving file loss.

When using -i or --interactive with cp and mv you will be prompted before any overwrites occur. When using -i with rm you will be prompted before each removal.

As mentioned, I’m not very careful, so I added the following to my shell’s profile file:

# Safety first
$ alias cp='cp -i'
$ alias mv='mv -i'
$ alias rm='rm -i'

With these aliases in place, I don’t have to remember to tack on the additional flag and can go around all willy nilly as I normally would.

Sure, the extra prompts can slow me down and tick me off sometimes, but it’s a small price to pay to keep from making a silly mistake.

Copying Files

Okay, now that my lecture is over, let’s get to working with some files. First thing we can do is copy a file to another location:

$ cp alligator.txt chinese-alligator.txt

Running ls will reveal that we now have a new file named chinese-alligator.txt that is a copy of alligator.txt. If the source file actually had any contents, the new file would have the same contents.

Moving / Renaming Files

If you know anything about alligators, you know that there is an American alligator and a Chinese alligator. Currently our directory contains a file for the Chinese alligator and a generic alligator file.

Instead of copying the generic file a second time, we can move it so we don’t end up with three alligator files:

$ mv alligator.txt american-alligator.txt

Moving a file is the equivalent of doing a copy cp followed by a remove rm.

Removing Files

Speaking of removing files, let’s get that pesky snake out of here:

$ rm snake.txt

Not much to it, the file is gone! If you happened to setup the aforementioned aliases, you would have been prompted as such:

rm: remove regular empty file 'snake.txt'?

A simple y or n will suffice when interacting with these prompts.

Reading Files

Reading is fundamental, as they say. Reading files comes in a few different shapes and sizes depending on what you’re trying to accomplish.

If you would like to read the entire file, you can use the cat command:

$ cat toc.txt

The file is meant to concatenate files together and then display them to the standard output so you can actually list multiple files if you’d like.

Reading the entire contents of a file is great but falls apart the moment you need to take a look at a 46 megabyte nginx access log file.

You can ease the burden a few different ways, depending on what you’re trying to get out of the file.

If you’d like to casually peruse the entire file, without opening up a text editor, you can use less to read the file and then navigate the file with your arrow keys (or vi-keys if you’re into that):

$ less some-huge.access.log

When you’re done, you can press q to exit less.

Now if you’re only interested in the top or bottom part of the file, you can use head and tail respectively to see the first or last 10 lines of the file:

$ head some-huge.access.log
$ tail some-huge.access.log

Need more than 10 lines? Both commands accept -n or --lines to specify however many lines you’d like.

The tail command also accepts the -f or --follow flag that will dump the last lines of the file but then also wait for new lines and report those too. This is infinitely valuable when monitoring a file for changes.

Done tailing? Just hit CTRL+c to stop.

  Tweet It

🕵 Search Results

🔎 Searching...