Читать книгу Linux Command Line and Shell Scripting Bible - Christine Bresnahan - Страница 19

Part I
The Linux Command Line
Chapter 3
Basic bash Shell Commands
Listing Files and Directories

Оглавление

To see what files are available on the system, use the list command (ls). This section describes the ls command and options available to format the information it can display.

Displaying a basic listing

The ls command at its most basic form displays the files and directories located in your current directory:


Notice that the ls command produces the listing in alphabetical order (in columns rather than rows). If you're using a terminal emulator that supports color, the ls command may also show different types of entries in different colors. The LS_COLORS environment variable controls this feature. (Environment variables are covered in Chapter 6). Different Linux distributions set this environment variable depending on the capabilities of the terminal emulator.

If you don't have a color terminal emulator, you can use the – F parameter with the ls command to easily distinguish files from directories. Using the – F parameter produces the following output:


The – F parameter flags the directories with a forward slash (/), to help identify them in the listing. Similarly, it flags executable files (like the my_script file in the preceding code) with an asterisk (*), to help you more easily find files that can be run on the system.

The basic ls command can be somewhat misleading. It shows the files and directories contained in the current directory, but not necessarily all of them. Linux often uses hidden files to store configuration information. In Linux, hidden files are files with filenames starting with a period (.). These files don't appear in the default ls listing. Thus, they are called hidden files.

To display hidden files along with normal files and directories, use the – a parameter. Here is an example of using the – a parameter with the ls command.


All the files beginning with a period, hidden files, are now shown. Notice that three files begin with .bash. These are hidden files that are used by the bash shell environment. These features are covered in detail in Chapter 6.

The – R parameter is another option the ls command can use. Called the recursive option, it shows files that are contained within subdirectories in the current directory. If you have lots of subdirectories, this can be quite a long listing. Here's a simple example of what the – R parameter produces. The – F option was tacked on to help you see the file types:


Notice that the – R parameter shows the contents of the current directory, which are the files from a user's home directory shown in earlier examples. It also shows each subdirectory in the user's home directory and their contents. The only subdirectory containing a file is the Music subdirectory, and it contains the executable file, ILoveLinux.mp3.

Tip

Option parameters don't have to be entered separately as shown in the nearby example: ls – F – R. They can often be combined as follows: ls – FR.

In the previous example, there were no subdirectories within subdirectories. If there had been further subdirectories, the – R parameter would have continued to traverse those as well. As you can see, for large directory structures, this can become quite a large output listing.

Displaying a long listing

In the basic listings, the ls command doesn't produce much information about each file. For listing additional information, another popular parameter is – l. The – l parameter produces a long listing format, providing more information about each file in the directory:


The long listing format lists each file and subdirectory on a single line. In addition to the filename, the listing shows additional useful information. The first line in the output shows the total number of blocks contained within the directory. After that, each line contains the following information about each file (or directory):

● The file type – such as directory (d), file (-), linked file (l), character device (c), or block device (b)

● The file permissions (see Chapter 6)

● The number of file hard links (See the section “Linking Files” in Chapter 7.)

● The file owner username

● The file primary group name

● The file byte size

● The last time the file was modified

● The filename or directory name

The – l parameter is a powerful tool to have. Armed with this parameter, you can see most of the information you need for any file or directory.

The ls command has lots of parameters that can come in handy as you do file management. If you type at the shell prompt man ls, you see several pages of available parameters for you to use to modify the ls command output.

Don't forget that you can also combine many of the parameters. You can often find a parameter combination that not only displays the desired output, but also is easy to remember, such as ls – alF.


Конец ознакомительного фрагмента. Купить книгу
Linux Command Line and Shell Scripting Bible

Подняться наверх