Mastering the Linux Shell : Files and Directories

Spread the love

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:

fish
duck
program_2.01
a.out
letter.to.mom.who.I.dont.write.often.enough.as.it.is
.bash_profile

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
/etc/thunderbird

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 CookingWithLinux.com where it’s all Linux, all the time. Except for the occasional wine review.

A votre santé! Bon appétit!

Liked it? Take a second to support Cooking With Linux on Patreon!
Become a patron at Patreon!