Working with Files

Let me tell you the secret of computers, of operating systems, and of the whole industry that surrounds these things: Everything is data. Information is the be-all and end-all of everything we do with computers. Files are the storehouses for that information and learning how to manipulate them, use and abuse them, and otherwise play with them will still be the point of computers 20 years from now.

You might remember a few of these commands from chapter 5.

A few file related commands:


LiSt files


conCATenate files

Try cat /etc/profile


SORT the contents of a file (or any output for that matter)

Try sort /etc/passwd


Return only the UNIQue lines?you do this after sorting


Word Count (returns a count of words, characters, and lines)


CoPy files


MoVe, or rename, a file


ReMove, or delete, a file


Easy paging of large text files


Like the more command but with serious attitude

File Naming Conventions

Valid file names may contain almost any character. You do have to pay some attention to the names you come up with. Your Linux system will allow file names up to 255 characters in length. How you define file names can save you a lot of hassle, as I will soon demonstrate.

Some valid file name examples include the following:


Notice the last name in particular. It starts with a period. Normally, this type of file is invisible with a default listing. To see these so-called dotfiles, use the ls command with a -a ( ls -a ).

Listing Files with Emotion!

The ls command seems so simple, yet it has a number of options that can give you tons of information. Change to something such as the /etc directory and try these options if you never have:

cd /etc
ls ?color
ls -b
ls -lS
ls -lt

The first listing will show different types of files and directories in color. The second (-b) will show octal representations for files that might have been created with control characters. Depending on the terminal you are using, the default is to show question marks or simply blanks. If you need to access (or delete) the file, it helps to know what it is really called. The third and fourth options control sorting. The -lS option gives you a long listing (lots of information) sorted by file size. The last option (-lt) sorts by time, with the newest files at the top of the list and the oldest at the bottom.