Linux Files, Users, and Shell Customization with Bash
When a friend of mine got a new Unix computer, the console display didn't look quite right. When we tried to view files, the operating system didn't know how big the screen was. It displayed the entire file instead of a screen at a time.
My Unix was a bit rusty at the time, but I remembered that there was a stty command to change attributes of the display. Looking at the help listing for stty, I noticed a person could set the rows and the line. Thinking that line must be the number of lines on the display, I typed stty line 24. The computer stopped responding, forcing us to reboot it.
We phoned up a Unix professional who had the same operating system. He said, "That should have worked. Let me try it." There was a brief pause. "I locked up my computer, too."
It turned out that stty line 24 set the serial port for the display, changing it to port 24 when there was no device connected to port 24. With the wide variety of options available to Unix-based operating systems like Linux, it can sometimes be difficult to predict what a command actually does. This chapter expands on the last chapter, covering more basic commands and their many, sometimes confusing, options.
The ls (list) command shows the contents of the current directory. Although ls is a familiar command available on all Unix-like operating system, Linux uses the ls command from the GNU fileutils project and has many special switches and features.
$ ls archive check-orders.sh orders.txt
ls has switches that affect how the files are listed, including the level of detail, the sorting order, and the number of columns. Most Linux distributions set up certain defaults for ls command. Red Hat, for example, has the -q and -F switches on by default. From the point of view of script writing, it's not safe to use the ls command in a script without specifying the appropriate switches because you can't be sure which defaults a particular distribution uses.
ls hides files that begin with a period. This is the Linux convention for configuration files, history files, and other files that a user isn't normally interested in. To see these files, use the -A (all) switch. Use -a (absolutely all) to show the implicit . and .. files as well.
$ ls -A .bash_history .bash_logout .bash_profile .bashrc archive check-orders.sh orders.txt
The filenames can be printed in color to show the kind of file they are. The colors are defined in a file /etc/DIR_COLORS. You can customize the colors using a .dir_colors file in your own directory. The format of the file is described in the /etc/DIR_COLORS file.
To display the files without color and with symbols instead, use the --color and --classify (or -F) switches. (On most Linux distributions, this feature is turned on using aliases.)
$ ls --color=never --classify archive/ check-orders.sh* orders.txt
The classify symbols are directories (/), programs (*), symbolic links (@), pipes (|), and Unix domain socket files (=). These symbols are not a part of the name: They are hints as to the type of file. In this example, archive is a directory and check-orders.sh is a program.
Another very important switch is --hide-control-chars (or -q). Linux filenames can contain any character, even control characters. It is possible to create a filename with hidden characters in the name. In these cases, you can't rename or delete the file unless you know what the hidden characters are. Contrary to what the name implies, the --hide-control-chars switch displays any unprintable characters in the filename as question marks, making their locations visible.
$ rm orders.txt rm: orders.txt non-existent $ ls --color=never --classify -hide-control-chars archive/ check-orders.sh* orde?rs.txt
A complete list of switches appears at the end of this chapter.