File and Directory Listing

The most basic feature of the shell is the ability to see what files are available on the system. The list command (ls) is the tool that helps do that. This section describes the ls command and all of the options available to format the information it can provide.

Basic Listing

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

An output displays the files and directories located in the current directory using the l s command.
Description

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. 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:

An output distinguishes files from directories using the l s command with hyphen F parameter.
Description

The -F parameter now flags the directories with a forward slash, to help identify them in the listing. Similarly, it flags executable files (like the test,sh file above) with an asterisk, to help you find the files that can be run on the system easier.

The -R parameter is another command ls parameter to use. It shows files that are contained within directories in the current directory. If you have lots of directories, this can be quite a long listing. Here’s a simple example of what the -R parameter produces:

An output shows files that are contained within directories in the current directory using the l s command with hyphen F and hyphen R parameters.
Description

Notice that first, the -R parameter shows the contents of the current directory, which is a file (file1) and two directories (test1 and test2). Following that, it traverses each of the two directories, showing if any files are contained within each directory. The test1 directory shows two files (myprog1 and myprog2), while the test2 directory doesn’t contain any files. If there had been further subdirectories within the test1 or test2 directories, 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.

Modifying Listing Information

As you can see in the basic listings, the ls command doesn’t produce a whole lot of 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:

An output shows more information about each file in the directory using the l s command with hyphen l parameter.
Description

The long listing format lists each file and directory contained in the directory on a single line. Besides the file name, it shows additional useful information. The first line in the output shows the total number of blocks contained within the directory. Following that, each line contains the following information about each file (or directory):

  • The file type (such as directory (d), file (-), character device (c), or block device (b)

  • The permissions string for the file, indicting permissions for the user, group, and other users

  • The number of hard links to the file

  • The username of the owner of the file

  • The group name of the group the file belongs to

  • The size of the file in bytes

  • The time the file was modified last

  • The file or directory name

The -l parameter is a powerful tool to have. Armed with this information you can see just about any information you need to for any file or directory on the system.

The Complete Parameter List

There are lots of parameters for the ls command that can come in handy as you do file management. If you use the man command for ls, you’ll see several pages of available parameters for you to use to modify the output of the ls command.

The ls command uses two types of command line parameters:

  • Single-letter parameters

  • Full-word (long) parameters

The single-letter parameters are always preceded by a single dash. Full-word parameters are more descriptive and are preceded by a double dash. Many parameters have both a single-letter and full-word version, while some have only one type. TABLE 7-2 lists some of the more popular parameters that’ll help you out with using the bash ls command.

TABLE 7-2 Some popular ls command parameters.

Single LetterFull WordDescription
-a--allDon’t ignore entries starting with a period.
-A--almost-allDon’t list the . and .. files.
--authorPrint the author of each file.
-b--escapePrint octal values for nonprintable characters.
--block-size=size Calculate the block sizes using size-byte blocks.
-B~Don’t list entries with the tilde (~) symbol (used to denote backup copies).
-cSort by time of last modification.
-CList entries by columns.
--color=whenWhen to use colors (always, never, or auto).
-d--directoryList directory entries instead of contents and don’t dereference symbolic links.
-F--classifyAppend file-type indicator to entries.
--file-typeOnly append file-type indicators to some file types (not executable files).
--format=wordFormat output as either across, commas, horizontal, long, single-column, verbose, or vertical.
-gList full file information except for the file’s owner.
--group-directories-firstList all directories before files.
-G--no-groupIn long listing don’t display group names.
-h--human-readablePrint sizes using K for kilobytes, M for megabytes, and G for gigabytes.
--siSame as -h, but use powers of 1000 instead of 1024.
-i--inodeDisplay the index number (inode) of each file.
-lDisplay the long listing format.
-L--dereferenceShow information for the original file for a linked file.
-n--numeric-uid-gidShow numeric userid and groupid instead of names.
-oIn long listing don’t display owner names.
-r--reverseReverse the sorting order when displaying files and directories.
-R--recursiveList subdirectory contents recursively.
-s--sizePrint the block size of each file.
-S--sort=sizeSort the output by file size.
-t--sort=timeSort the output by file modification time.
-uDisplay file last access time instead of last modification time.
-U--sort=noneDon’t sort the output listing.
-v--sort=versionSort the output by file version.
-xList entries by line instead of columns.
-X--sort=extensionSort the output by file extension.

You can use more than one parameter at a time if you want to. The double dash parameters must be listed separately, but the single dash parameters can be combined together into a string behind the dash. A common combination to use is the -a parameter to list all files, the -i parameter to list the inode for each file, the -l parameter to produce a long listing, and the -s parameter to list the block size of the files. The inode of a file or directory is a unique identification number the kernel assigns to each object in the filesystem. Combining all of these parameters creates the easy-to-remember -sail parameter:

An output is shown using the l s command with hyphen sail parameter.
Description

Besides the normal -l parameter output information, you’ll see two additional numbers added to each line. The first number in the listing is the file or directory inode number. The second number is the block size of the file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.237.131