7. MARCO TEÓRICO Y CONCEPTUAL
7.3. FAMILIA Y ETAPAS DE AFRONTAMIENTO
Without options, the ls command offers relatively little information. Questions you might still have about your directory include: How big are the files? Which are files, and which are directories? How old are they? What hidden files do you have?
JUSTA MINUTE
1. Start by entering ls -s to indicate file sizes: % ls -s
total 403
4
2. To ascertain the size of each file or directory listed, you can use the -s flag to ls. The size indicated is the number of kilobytes, rounded upward, for each file. The first line of the listing also indicates the total amount of disk space used, in kilo- bytes, for the contents of this directory. The summary number does not, however, include the contents of any subdirectories, so it’s deceptively small.
A kilobyte is 1,024 bytes of information, a byte being a single character. The preceding paragraph, for example, contains slightly more than 400 characters. UNIX works in units of a block of information, which, depend- ing on which version of UNIX you’re using, is either 1 kilobyte or 512 bytes. Most UNIX systems now work with a 1-kilobyte block.
3. Here is a further definition of what occurs when you use the -s flag: ls indicates the number of blocks each file or directory occupies. You then can use simple calculations to convert blocks into bytes. For example, the ls command indicates that the LISTS file in my home directory occupies 108 blocks. A quick calculation of block size multiplied by the number of blocks reveals the actual file size, in bytes, of LISTS, as shown here:
% bc 1024 * 108
110592
quit
%
Based on these results of the bc command, you can see that the file is 110,592 bytes in size. You can estimate size by multiplying the number of blocks by 1,000. Be aware, however, that in large files, the difference between 1,000 and 1,024 is significant enough to introduce an error into your calculation. As an example, I have a file that’s more than three megabytes in size (a megabyte is 1,024 kilobytes, which is 1,024 bytes, so a megabyte is 1,024 × 1,024, or 1,048,576 bytes): % ls -s bigfile
3648 bigfile
4. The file actually occupies 3,727,360 bytes. If I estimated its size by multiplying the number of blocks by 1,000 (which equals 3,648,000 bytes), I’d have underesti- mated its size by 79,360 bytes. (Remember, blocks × 1,000 is an easy estimate!)
The last example reveals something else about the ls command. You can specify individual files or directories you’re interested in viewing and avoid having to see all files and directories in your current location.
JUSTA MINUTE
4
5. You can specify as many files or directories as you like, and separate them by spaces:
% ls -s LISTS iecc.list newels
108 LISTS 4 iecc.list 2 newels
In the previous hour, you learned that UNIX identifies each file that begins with a dot (.) as a hidden file. Your home directory is probably littered with dot files, which retain preferences, status information, and other data. To list these hidden files, use the -a flag to ls:
% ls -a
. .gopherrc .oldnewsrc .sig RUMORS.18Sept .. .history .plan Archives bin
.Agenda .info .pnewsexpert InfoWorld iecc.list .aconfigrc .letter .report LISTS mailing.lists .article .login .rm-timestamp Mail newels .cshrc .mailrc .rnlast News src .elm .newsrc .rnsoft OWL
You can see that this directory contains more dot files than regular files and directories. That’s not uncommon in a UNIX home directory. However, it’s rare to find any dot files other than the standard dot and dot-dot directories (those are in every directory in the entire file system) in directories other than your home directory.
6. You used another flag to the ls command—the -F flag—in the previous hour. Do you remember what it does?
% ls -F
Archives/ Mail/ RUMORS.18Sept mailing.lists InfoWorld@ News/ bin/ newels LISTS OWL/ iecc.list src/
Adding the -F flag to ls appends suffixes to certain filenames so that you can ascertain more easily what types of files they are. Three different suffixes can be added, as shown in Table 4.1.
Table 4.1. Filename suffixes appended by ls -F.
Suffix Example Meaning
/ Mail/ Mail is a directory.
* prog* prog is an executable program.
@ bin@ bin is a symbolic link to another file or directory.
7. If you’re familiar with the Macintosh and have used either System 7.0 or 7.1, you may recall the new feature that enables the user to create and use an alias. An alias
4
UNIX has offered a similar feature for many years, which in UNIX jargon is called a symbolic link. A symbolic link, such as bin in Table 4.1, contains the name of another file or directory rather than any contents of its own. If you could peek inside, it might look like bin = @/usr/bin. Every time someone tries to look at bin, the system shows the contents of /usr/bin instead.
You’ll learn more about symbolic links and how they help you organize your files a bit later in the book. For now, just remember that if you see an @ after a filename, it’s a link to another spot in the file system.
8. A useful flag for ls (one that might not be available in your version of UNIX) is the -m flag. This flag outputs the files as a comma-separated list. If there are many files, -m can be a quick and easy way to see what’s available.
% ls -m
Archives, InfoWorld, LISTS, Mail, News, OWL, RUMORS.18Sept, bin, iecc.list, mailing.lists, newels, src
Sometime you might want to list each of your files on a separate line, perhaps for a printout you want to annotate. You’ve seen that the -C flag forces recalcitrant versions of ls to output in multiple columns. Unfortunately, the opposite behavior isn’t obtained using a lowercase c. (UNIX should be so consistent!) Instead, use the -1 flag to indicate that you want one column of output. Try it.