Back to Top

Mastering the Linux Shell : Files and Directories

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. There's a saying in the Linux world that "everything is a file" (a comment attributed to Ken Thompson, the developer of UNIX). That includes directories. Directories are just files with lists of files inside them. All these files and directories are organized into a hierarchical file system, starting from the root directory and branching out.

For the record, and to make things easier, you can safely assume that folders and directories are the same thing. The terms can be used interchangeably, but I will usually refer to them directories. If you are more comfortable thinking of them as folders, don't worry. Depending on the application, you'll see both terms used.

The root directory (referred to as slash, or /) is actually aptly named. If you consider your file system as a tree's root system spreading out below the surface, you start to get an idea of just what things look like.

Under the root directory, you'll find folders called usr, bin, etc, tmp, and so on.  And then we have the three invisible, often overlooked, but completely indispensible files on your system: standard in, standard out, and standard error. I'll tell you more about those three later. Suffice it to say that understanding and knowing how to work with all these “files” will provide you with amazing flexibility when it comes to doing your work. 

File Naming Conventions

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

Some valid filename 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. Starting a file name with a period is a way to make a file somewhat invisible.  This is good to know if you don't want to burden file listings with a lot of noise.  It is also the way that a cracker (or hacker, if you prefer) might hide his or her tracks should they break into your system -- by creating a directory that starts with a period.  To see they so-called dot-files, use the ls command with a -a flag (ls -a).  Two particularly interesting directories are:

.     (dot) Your current directory
..    (dot dot) The parent directory

As you can see, they are, by default, invisible because they start with a period. To see them, you need to use "ls -a". And now that I've brought up listing files, let's examine the ls command.

Listing Files with Emotion!

The ls command seems so simple, and yet it has a number of options that can give you tons of information. Change to something like the /etc directory and try these options. The cd command is how you change directory.

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.

We started this by changing directory to /etc.  If you ever want to know what directory you are in, one way is to look at your bash command line prompt as it sometimes displays where you are.  But that's not always true. A sure fire way is the pwd command.

$ pwd

Here's a cool trick. To go back to the last directory (or folder) you were in, try the following.

cd -

The hyphen is a special character that references your last position in the file system. Here's another cool one.

cd ~

That will take you home, to your own personal starting directory. The tilde means "home". More on this later in the series. Just typing 'cd' will do the same thing, but the tilde is an important substitution character. Speaking of which . . .

A Peek at Metacharacters

Metacharacters are special characters that have particular meaning to your shell, that dollar sign or hash mark prompt where you do your work. The two I want to look at are the asterisk and the question mark. The following is what they mean to the shell.

*      Match any number of characters
?      Match a single character

Extending our talk of listing files, you could list all files containing “ackle” by using this command:

$ ls *ackle*
hackle hackles tackles

Similarly, you could find all the words that start with an “h” like this:

$ ls h*
hackle hackles

Now, if you want to see all the seven-letter words in your directory, use this command:

$ ls ???????
hackles tackles

Each question mark represents a single letter position.

And that is where I will leave this discussion for today. Join me next Monday for part three in this series. Remember that you can also follow the action on where it's all Linux, all the time. Except for the occasional wine review. 

A votre santé! Bon appétit!



The Original Cooking with Linux
Appearing monthly, 3rd Thursday, on Computer America
Appearing every 3rd Thurday of every month on the Computer America radio show.
  • RT : gives us as David Bowie scolding 's Technical Boy, & we're so happy:… 14 hours 17 min ago
  • My 9 year old is creating a MEPtionary, which basically consists of replacing random MEPter seMEPces with MEP. Says he'll be a MEPionnaire. 14 hours 19 min ago
  • I laughed. I guffawed. I smirked. I giggled like a little kid. 16 hours 4 min ago
  • Curse me for a fool! I overcooked the egg yolk for my Hollandaise sauce and wound up with liquid butter mixed with scrambled eggs. 1 day 12 hours ago
  • RT : RT - Today, Jesus & Mo ponder the origin of hatred: 1 day 13 hours ago