Chapter 3: Working with Files and Directories


The UNIX file system provides a powerful and flexible way to organize and manage your information. This chapter introduces the basic concepts of the file system and explains the most important commands for manipulating files and directories. In particular, the commands that provide the basic file manipulation operations-viewing files, changing directories, deleting and moving files-are among the UNIX commands you will use most often.

By the end of this chapter, you will know how to display the contents of files and directories, and how to create, delete, and manage them. You will also be able to search for specific files, control user access to files by using permissions, and use the UNIX commands for printing files.

Files

A file is the basic structure that stores information on the UNIX System (and on Windows systems, as well). Conceptually, a computer file is similar to a paper document. Technically a file is a sequence of bytes that is stored somewhere on a storage device, such as a hard drive. A file can contain any kind of information that can be represented as a sequence of bytes. Word processing documents, bitmap images, and computer programs are all examples of files.

Filenames

Every file has a title, called a filename. A filename can be almost any sequence of characters, and up to 255 characters long. (On some older versions of UNIX, two filenames are considered the same if the first 14 characters are identical, so be careful if you use long filenames on these systems.) You can use any ASCII character in a filename except for the null character (ASCII NUL) or the slash (/), which has a special meaning in the UNIX file system. The slash acts as a separator between directories and files.

Even though UNIX allows you to choose filenames with special characters, it is a good idea to stick with alphanumeric characters (letters and numbers) when naming files. You may encounter problems when you use or display the names of files containing nonalphanumeric characters. In particular, although the following characters can be used in filenames, it is better to avoid them. Many of these characters have special meanings in the command shell, which makes them difficult to work with in filenames.

! (exclamation point)

* (asterisk)

{,} (brackets)

# (pound sign)

? (question mark)

; (semicolon)

& (ampersand)

\ (backslash)

^ (caret)

| (pipe)

(,) (parentheses)

tab

@ (at sign)

‘,” (single or double quotes)

space

$ (dollar sign)

< , > (left or right arrow)

backspace

Capitalization

Windows does not distinguish between uppercase and lowercase letters in filenames. You could save a file with the name Notes.DOC and find it by searching for notes.doc. The UNIX file system, however, is case-sensitive, meaning that uppercase and lowercase letters are distinct. In UNIX, NOTES, Notes, and notes would be three different files. If you save a file with the name Music, you will not find it by searching for music. This also applies to commands in UNIX. If you are trying to log out with the exit command, typing EXIT will not work. By the way, this explains why URLs (web addresses) can be case-sensitive, since the first web server was created on a UNIX-based platform, and many web servers still run UNIX.

Filename Extensions

In Windows, filenames typically consist of a basename, followed by a period and a short filename extension. Many Windows programs depend on the extension to determine how to use the file. For example, a file named solitaire.exe is considered to be a file named solitaire with the extension .exe, where the .exe extension tells Windows that it is an executable program. If the file extension is altered or deleted, it will be more difficult to work with the file in Windows.

In UNIX, file extensions are conveniences, rather than a necessary part of the filename. They can help you remember the content of files, or help you organize your files, but they are usually optional. In fact, many UNIX filenames do not have an extension. For example, an executable program would typically have a name like solitaire rather than solitaire.exe. In addition, filename extensions in UNIX can be longer than three characters. For example, some people use .backup to indicate a backup copy of a file, so notes.backup would be an extra copy of the file notes.

Some programs either produce or expect a file with a particular filename extension. For example, files that contain C source code have the extension .c, so sorting.c would be a C language file. Similarly, web browsers expect that HTML files will have the extension .html, such as index.html. Table 3–1 displays some of the most commonly used filename extensions.

Table 3–1: Common File Extensions

Extension

File Type

Extension

File Type

.au

Audio

.mpg, .mpeg

MPEG video

.c

C language source code

.o

Object file (compiled and assembled code)

.cc

C++ source code

.pl

Perl script

.class

Compiled Java file

.ps

PostScript file

.conf

Configuration file

.py

Python script

.d

Directory

.sh

Bourne shell script

.gif

GIF image

.tar

tar archive

.gz

Compressed with gzip

.tar.Z, .tar.gz

Files that have been archived with tar and then compressed

.h

Header file for a C program

.tex

Text formatted with Tex/LaTeX

.html

Webpage

.txt

ASCII text

.jar

Java archive

.uu, .uue

Uuencoded file

.java

Java source code

.wav

Wave audio

.jpg, .jpeg

JPEG image

.z

Compressed with pack

.log

Log file

.Z

Compressed with compress

UNIX files can have more than one extension. For example, the file book.tar.Z is a file that has first been archived using the tar command (which adds the extension .tar) and then compressed using the compress command (which adds the .Z). This enables a single script to both decompress the file and untar it, using the filename as input and parsing each of the extensions to perform the appropriate task.

The flexibility of filename conventions in UNIX allow for some variation in filenames. A program written in Perl could have the filename program.perl, the more frequently used program.pl, or even just the name program. You can even create your open file extensions. A text file containing research notes might be called res_nov, ResearchNovember, or research.notes.nov. In the last case, the extension .nov is just to remind you that the notes are from November. It will not change the way you work with the file.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net