-->

Listing Files and Directories On Linux

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.

Listing Files and Directories On Linux
Listing Files and Directories On Linux


Displaying a basic listing

The ls command at its most basic form displays the files and directories located in your current directory:
$ lsDesktop Downloads Music Pictures Templates VideosDocuments examples.desktop my_script Public test_file$
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. (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:
$ ls -F
Desktop/ Downloads/ Music/ Pictures/ Templates/ Videos/
Documents/ examples.desktop my_script* Public/ test_file
$
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.
$ ls -a
. .compiz examples.desktop Music test_file
.. .config .gconf my_script Videos
.bash_history Desktop .gstreamer-0.10 Pictures .Xauthority
.bash_logout .dmrc .ICEauthority .profile .xsession-errors
.bashrc Documents .local Public .xsession-errors.old
.cache Downloads .mozilla Templates
$
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. (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:
$ ls -F -R
.:
Desktop/ Downloads/ Music/ Pictures/ Templates/ Videos/
Documents/ examples.desktop my_script* Public/ test_file
./Desktop:
./Documents:
./Downloads:
./Music:
ILoveLinux.mp3*
./Pictures:
./Public:
./Templates:
./Videos:
$
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:
$ ls -ltotal 48drwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Desktopdrwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Documentsdrwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Downloads-rw-r—r— 1 christine christine 8980 Apr 22 13:36 examples.desktop-rw-rw-r— 1 christine christine 0 May 21 13:44 fall-rw-rw-r— 1 christine christine 0 May 21 13:44 fell-rw-rw-r— 1 christine christine 0 May 21 13:44 fill-rw-rw-r— 1 christine christine 0 May 21 13:44 fulldrwxr-xr-x 2 christine christine 4096 May 21 11:39 Music-rw-rw-r— 1 christine christine 0 May 21 13:25 my_file-rw-rw-r— 1 christine christine 0 May 21 13:25 my_scrapt-rwxrw-r— 1 christine christine 54 May 21 11:26 my_script-rw-rw-r— 1 christine christine 0 May 21 13:42 new_filedrwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Picturesdrwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Publicdrwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Templates-rw-rw-r— 1 christine christine 0 May 21 11:28 test_filedrwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Videos$
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.

Filtering listing output

As you’ve seen in the examples, by default the ls command lists all the non-hidden directory files. Sometimes, this can be overkill, especially when you’re just looking for information on a few files.

Fortunately, the ls command also provides a way for you to define a filter on the command line. It uses the filter to determine which files or directories it should display in the output.

The filter works as a simple text-matching string. Include the filter after any command line parameters you want to use:
$ ls -l my_script-rwxrw-r— 1 christine christine 54 May 21 11:26 my_script$
When you specify the name of a specific file as the filter, the ls command only shows that file’s information. Sometimes, you might not know the exact filename you’re looking for. The ls command also recognizes standard wildcard characters and uses them to match patterns within the filter:

  • A question mark (?) to represent one character
  • An asterisk (*) to represent any number of characters

The question mark can be used to replace exactly one character anywhere in the filter string. For example:
$ ls -l my_scr?pt-rw-rw-r— 1 christine christine 0 May 21 13:25 my_scrapt-rwxrw-r— 1 christine christine 54 May 21 11:26 my_script$
The filter my_scr?pt matched two files in the directory. Similarly, the asterisk can be used to match zero or more characters:
$ ls -l my*-rw-rw-r— 1 christine christine 0 May 21 13:25 my_file-rw-rw-r— 1 christine christine 0 May 21 13:25 my_scrapt-rwxrw-r— 1 christine christine 54 May 21 11:26 my_script$
Using the asterisk finds three different files, starting with the name my. As with the question mark, you can place the asterisks anywhere in the filter:
$ ls -l my_s*t-rw-rw-r— 1 christine christine 0 May 21 13:25 my_scrapt-rwxrw-r— 1 christine christine 54 May 21 11:26 my_script$
Using the asterisk and question mark in the filter is called file globbing. File globbing is the processing of pattern matching using wildcards. The wildcards are officially called metacharacter wildcards. You can use more metacharacter wildcards for file globbing than just the asterisk and question mark. You can also use brackets:
$ ls -l my_scr[ai]pt-rw-rw-r— 1 christine christine 0 May 21 13:25 my_scrapt-rwxrw-r— 1 christine christine 54 May 21 11:26 my_script$
In this example, we used the brackets along with two potential choices for a single character in that position, a or i. The brackets represent a single character position and give you multiple options for file globbing. You can list choices of characters, as shown in the preceding example, and you can specify a range of characters, such as an alphabetic range [a - i]:
$ ls -l f[a-i]ll-rw-rw-r— 1 christine christine 0 May 21 13:44 fall-rw-rw-r— 1 christine christine 0 May 21 13:44 fell-rw-rw-r— 1 christine christine 0 May 21 13:44 fill$
Also, you can specify what should not be included in the pattern match by using the exclamation point (!):
$ ls -l f[!a]ll-rw-rw-r— 1 christine christine 0 May 21 13:44 fell-rw-rw-r— 1 christine christine 0 May 21 13:44 fill-rw-rw-r— 1 christine christine 0 May 21 13:44 full$
File globbing is a powerful feature when searching for files. It can also be used with other shell commands besides ls. You find out more about this later in the chapter.

Handling Files

The shell provides many file manipulation commands on the Linux filesystem. This section walks you through the basic shell commands you need to handle files.

Creating files

Every once in a while you run into a situation where you need to create an empty file. For example, sometimes applications expect a log file to be present before they can write to it. In these situations, you can use the touch command to easily create an empty file:
$ touch test_one$ ls -l test_one-rw-rw-r— 1 christine christine 0 May 21 14:17 test_one$
The touch command creates the new file you specify and assigns your username as the file owner. Notice in the preceding example that the file size is zero because the touch command just created an empty file.

The touch command can also be used to change the modification time. This is done without changing the file contents:
$ ls -l test_one-rw-rw-r— 1 christine christine 0 May 21 14:17 test_one$ touch test_one$ ls -l test_one-rw-rw-r— 1 christine christine 0 May 21 14:35 test_one$The modification time of test_one is now updated to 14:35 from the original time, 14:17.To change only the access time, use the -a parameter with the touch command:$ ls -l test_one-rw-rw-r— 1 christine christine 0 May 21 14:35 test_one$ touch -a test_one$ ls -l test_one-rw-rw-r— 1 christine christine 0 May 21 14:35 test_one$ ls -l —time=atime test_one-rw-rw-r— 1 christine christine 0 May 21 14:55 test_one$
In the preceding example, notice that by using only the ls -l command, the access time does not display. This is because the modification time is shown by default. To see a file’s access time, you need to add an additional parameter, —time=atime. After we add that parameter in the preceding example, the file’s altered access time is displayed.

Creating empty files and altering file timestamps is not something you will do on a Linux system daily. However, copying files is an action you will do often while using the shell.

Copying files

Copying files and directories from one location in the filesystem to another is a common practice for system administrators. The cp command provides this feature. In its most basic form, the cp command uses two parameters — the source object and the destination object: cp source destination.

When both the source and destination parameters are filenames, the cp command copies the source file to a new destination file. The new file acts like a brand new file, with an updated modification time:
$ cp test_one test_two
$ ls -l test_*
-rw-rw-r— 1 christine christine 0 May 21 14:35 test_one
-rw-rw-r— 1 christine christine 0 May 21 15:15 test_two
$
The new file test_two shows a different modification time than the test_one file. If the destination file already exists, the cp command may not prompt you to this fact. It is best to add the -i option to force the shell to ask whether you want to overwrite a file:
$ ls -l test_*-rw-rw-r— 1 christine christine 0 May 21 14:35 test_one-rw-rw-r— 1 christine christine 0 May 21 15:15 test_two$$cp -i test_one test_twocp: overwrite ‘test_two’? n$
If you don’t answer y, the file copy does not proceed. You can also copy a file into a preexisting directory:
$ cp -i test_one /home/christine/Documents/$$
ls -l /home/christine/Documents
total 0
-rw-rw-r— 1 christine christine 0 May 21 15:25 test_one
$
The new file is now under the Documents subdirectory, using the same filename as the original.

Note
The preceding example uses a trailing forward slash (/) on the destination directory name. Using the slash indicates Documents is a directory and not a file. This is helpful for clarity purposes and is important when copying single files. If the forward slash is not used and the subdirectory /home/christine/Documents does not exist, problems can occur. In this case, attempting to copy a single file to the Documents subdirectory creates a file named Documents instead, and no error messages display!
This last example used an absolute directory reference, but you can just as easily use a relative directory reference:
$ cp -i test_one Documents/cp: overwrite ‘Documents/test_one’? y$$ls -l Documentstotal 0-rw-rw-r— 1 christine christine 0 May 21 15:28 test_one$
Earlier in this chapter, you read about the special symbols that can be used in relative directory references. One of them, the single dot (.), is great to use with the cp command. Remember that the single dot represents your present working directory. If you need to copy a file with a long source object name to your present working directory, the single dot can simplify the task:
$ cp -i /etc/NetworkManager/NetworkManager.conf .
$$
ls -l NetworkManager.conf
-rw-r—r— 1 christine christine 76 May 21 15:55 NetworkManager.conf
$
It’s hard to see that single dot! If you look closely, you’ll see it at the end of the first example code line. Using the single dot symbol is much easier than typing a full destination object name, when you have long source object names.

Tip
There are many more cp command parameters than those described here. Remember
that you can see all the different available parameters available for the cp command,
by typing man cp.
The -R parameter is a powerful cp command option. It allows you to recursively copy the contents of an entire directory in one command:
$ ls -Fd *Scripts
Scripts/
$ ls -l Scripts/
total 25
-rwxrw-r— 1 christine christine 929 Apr 2 08:23 file_mod.sh
-rwxrw-r— 1 christine christine 254 Jan 2 14:18 SGID_search.sh
-rwxrw-r— 1 christine christine 243 Jan 2 13:42 SUID_search.sh
$$
cp -R Scripts/ Mod_Scripts
$ ls -Fd *Scripts
Mod_Scripts/ Scripts/
$ ls -l Mod_Scripts
total 25
-rwxrw-r— 1 christine christine 929 May 21 16:16 file_mod.sh
-rwxrw-r— 1 christine christine 254 May 21 16:16 SGID_search.sh
-rwxrw-r— 1 christine christine 243 May 21 16:16 SUID_search.sh
$
The directory Mod_Scripts did not exist prior to the cp -R command. It was created with the cp -R command, and the entire Scripts directory’s contents were copied into it. Notice that all the files in the new Mod_Scripts directory have new dates associated with them. Now Mod_Scripts is a complete copy of the Scripts directory.

Note
In the preceding example, the options -Fd were added to the ls command. You read about the -F option earlier in this chapter. However, the -d option may be new to you. The -d option lists a directory’s information but not its contents.

You can also use wildcard metacharacters in your cp commands:
$ cp *script Mod_Scripts/$ ls -l Mod_Scriptstotal 26-rwxrw-r— 1 christine christine 929 May 21 16:16 file_mod.sh-rwxrw-r— 1 christine christine 54 May 21 16:27 my_script-rwxrw-r— 1 christine christine 254 May 21 16:16 SGID_search.sh-rwxrw-r— 1 christine christine 243 May 21 16:16 SUID_search.sh$
This command copied any files that ended with script to Mod_Scripts. In this case, only one file needed to be copied: my_script.

When copying files, another shell feature can help you besides the single dot and wildcard metacharacters. It is called tab auto-complete.

Using tab auto-complete

When working at the command line, you can easily mistype a command, directory name, or filename. In fact, the longer a directory reference or filename, the greater the chance that you will mistype it.

This is where tab auto-complete can be a lifesaver. Tab auto-complete allows you to start typing a filename or directory name and then press the tab key to have the shell complete it for you:

$ ls really*really_ridiculously_long_file_name$$cp really_ridiculously_long_file_name Mod_Scripts/ls -l Mod_Scriptstotal 26-rwxrw-r— 1 christine christine 929 May 21 16:16 file_mod.sh-rwxrw-r— 1 christine christine 54 May 21 16:27 my_script-rw-rw-r— 1 christine christine 0 May 21 17:08really_ridiculously_long_file_name-rwxrw-r— 1 christine christine 254 May 21 16:16 SGID_search.sh-rwxrw-r— 1 christine christine 243 May 21 16:16 SUID_search.sh$
In the preceding example, we typed the command cp really and pressed the tab key, and the shell auto-completed the rest of the filename! Of course, the destination directory had to be typed, but still tab auto-complete saved the command from several potential typographical errors.

The trick to using tab auto-complete is to give the shell enough filename characters so it can distinguish the desired file from other files. For example, if another filename started with really, pressing the tab key would not auto-complete the filename. Instead, you would hear a beep. If this happens, you can press the tab key again, and the shell shows you all the filenames starting with really. This feature allows you to see what needs to be typed for tab auto-complete to work properly.

Linking files

Linking files is a great option available in the Linux filesystem. If you need to maintain two (or more) copies of the same file on the system, instead of having separate physical copies, you can use one physical copy and multiple virtual copies, called links. A link is a placeholder in a directory that points to the real location of the file. Two types of file links are available in Linux:

  • A symbolic link
  • A hard link
  • A symbolic link is simply a physical file that points to another file somewhere in the virtual directory structure. The two symbolically linked together files do not share the same contents.

To create a symbolic link to a file, the original file must pre-exist. We can then use the ln command with the -s option to create the symbolic link:
$ ls -l data_file
-rw-rw-r— 1 christine christine 1092 May 21 17:27 data_file
$$
ln -s data_file sl_data_file
$$
ls -l *data_file
-rw-rw-r— 1 christine christine 1092 May 21 17:27 data_file
lrwxrwxrwx 1 christine christine 9 May 21 17:29 sl_data_file ->
data_file
$
In the preceding example, notice that the name of the symbolic link, sl_data_file, is listed second in the ln command. The —> symbol displayed after the symbolic link file’s long listing shows that it is symbolically linked to the file data_file.

Also note the symbolic link’s file size versus the data file’s file size. The symbolic link, sl_data_file, is only 9 bytes, whereas the data_file is 1092 bytes. This is because sl_data_file is only pointing to data_file. They do not share contents and are two physically separate files.

Another way to tell that these linked files are separate physical files is by viewing their inode number. The inode number of a file or directory is a unique identification number that the kernel assigns to each object in the filesystem. To view a file or directory’s inode number, add the -i parameter to the ls command:
$ ls -i *data_file296890 data_file 296891 sl_data_file$
The example shows that the data file’s inode number is 296890, while the sl_data_file inode number is different. It is 296891. Thus, they are different files.

A hard link creates a separate virtual file that contains information about the original file and where to locate it. However, they are physically the same file. When you reference the hard link file, it’s just as if you’re referencing the original file. To create a hard link, again the original file must pre-exist, except that this time no parameter is needed on the ln command:
$ ls -l code_file-rw-rw-r— 1 christine christine 189 May 21 17:56 code_file$$ln code_file hl_code_file$$ls -li *code_file296892 -rw-rw-r— 2 christine christine 189 May 21 17:56code_file296892 -rw-rw-r— 2 christine christine 189 May 21 17:56hl_code_file$
In the preceding example, we used the ls -li command to show both the inode numbers and a long listing for the *code_files. Notice that both files, which are hard linked together, share the name inode number. This is because they are physically the same file. Also notice that the link count (the third item in the listing) now shows that both files have two links. In addition, their file size is exactly the same size as well.

NoteYou can only create a hard link between files on the same physical medium. To create
a link between files under separate physical mediums, you must use a symbolic link.
Be careful when copying linked files. If you use the cp command to copy a file that’s
linked to another source file, all you’re doing is making another copy of the source file.
This can quickly get confusing. Instead of copying the linked file, you can create another link to the original file. You can have many links to the same file with no problems. However, you also don’t want to create soft links to other soft-linked files. This creates a chain of links that can be confusing — and easily broken — causing all sorts of problems. You may find symbolic and hard links difficult concepts. Fortunately, renaming files in the next section is a great deal easier to understand.

Renaming files

In the Linux world, renaming files is called moving files. The mv command is available to move both files and directories to another location or a new name:
$ ls -li f?ll
296730 -rw-rw-r— 1 christine christine 0 May 21 13:44 fall
296717 -rw-rw-r— 1 christine christine 0 May 21 13:44 fell
294561 -rw-rw-r— 1 christine christine 0 May 21 13:44 fill
296742 -rw-rw-r— 1 christine christine 0 May 21 13:44 full
$$
mv fall fzll
$$
ls -li f?ll
296717 -rw-rw-r— 1 christine christine 0 May 21 13:44 fell
294561 -rw-rw-r— 1 christine christine 0 May 21 13:44 fill
296742 -rw-rw-r— 1 christine christine 0 May 21 13:44 full
296730 -rw-rw-r— 1 christine christine 0 May 21 13:44 fzll
$
Notice that moving the file changed the name from fall to fzll, but it kept the same
inode number and timestamp value. This is because mv affects only a file’s name.
You can also use mv to change a file’s location:
$ ls -li /home/christine/fzll
296730 -rw-rw-r— 1 christine christine 0 May 21 13:44
/home/christine/fzll
$$
ls -li /home/christine/Pictures/
total 0
$ mv fzll Pictures/
$$
ls -li /home/christine/Pictures/
total 0
296730 -rw-rw-r— 1 christine christine 0 May 21 13:44 fzll
$$
ls -li /home/christine/fzll
ls: cannot access /home/christine/fzll: No such file or directory
$
In the preceding example, we moved the file fzll from /home/christine to /home/christine/Pictures using the mv command. Again, there were no changes to the file’s inode number or timestamp value.

Tip
Like the cp command, you can use the -i option on the mv command. Thus, you are
asked before the command attempts to overwrite any pre-existing files.
The only change was to the file’s location. The fzll file no longer exists in /home/christine, because a copy of it was not left in its original location, as the cp command would have done.

You can use the mv command to move a file’s location and rename it, all in one easy step:

$ ls -li Pictures/fzll296730 -rw-rw-r— 1 christine christine 0 May 21 13:44Pictures/fzll$$mv /home/christine/Pictures/fzll /home/christine/fall$$ls -li /home/christine/fall296730 -rw-rw-r— 1 christine christine 0 May 21 13:44/home/christine/fall$$
ls -li /home/christine/Pictures/fzll
ls: cannot access /home/christine/Pictures/fzll:
No such file or directory

For this example, we moved the file fzll from a subdirectory, Pictures, to the home directory, /home/christine, and renamed it to fall. Neither the timestamp value nor the inode number changed. Only the location and name were altered.

You can also use the mv command to move entire directories and their contents:
$ ls -li Mod_Scripts
total 26
296886 -rwxrw-r— 1 christine christine 929 May 21 16:16
file_mod.sh
296887 -rwxrw-r— 1 christine christine 54 May 21 16:27
my_script
296885 -rwxrw-r— 1 christine christine 254 May 21 16:16
SGID_search.sh
296884 -rwxrw-r— 1 christine christine 243 May 21 16:16
SUID_search.sh
$$
mv Mod_Scripts Old_Scripts
$$
ls -li Mod_Scripts
ls: cannot access Mod_Scripts: No such file or directory
$$
ls -li Old_Scripts
total 26
296886 -rwxrw-r— 1 christine christine 929 May 21 16:16
file_mod.sh
296887 -rwxrw-r— 1 christine christine 54 May 21 16:27
my_script
296885 -rwxrw-r— 1 christine christine 254 May 21 16:16
SGID_search.sh
296884 -rwxrw-r— 1 christine christine 243 May 21 16:16
SUID_search.sh
$
The directory’s entire contents are unchanged. The only thing that changes is the name of the directory.

After you know how to rename…err…move files with the mv command, you realize how simple it is to accomplish. Another easy, but potentially dangerous, task is deleting files.

Deleting files

Most likely at some point you’ll want to be able to delete existing files. Whether it’s to clean up a filesystem or to remove a software package, you always have opportunities to delete files.

In the Linux world, deleting is called removing. The command to remove files in the bash
shell is rm. The basic form of the rm command is simple:
$ rm -i fallrm: remove regular empty file ‘fall’? y$$ls -l fallls: cannot access fall: No such file or directory$
Notice that the -i command parameter prompts you to make sure that you’re serious about removing the file. The shell has no recycle bin or trashcan. After you remove a file, it’s gone forever. Therefore, a good habit is to always tack on the -i parameter to the rm command.

You can also use wildcard metacharacters to remove groups of files. However, again, use that -i option to protect yourself:
$ rm -i f?llrm: remove regular empty file ‘fell’? yrm: remove regular empty file ‘fill’? yrm: remove regular empty file ‘full’? y$$ls -l f?llls: cannot access f?ll: No such file or directory$
One other feature of the rm command, if you’re removing lots of files and don’t want to be bothered with the prompt, is to use the -f parameter to force the removal. Just be careful!

0 Response to "Listing Files and Directories On Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel