File Features

A few features are unique to Linux and you’ll need to be aware of them when working with files. This section walks through these features.

Using Wildcards

The ls, cp, mv, and rm commands are handy, but specifying single file or directory names in the commands makes them somewhat clunky to work with on the Linux command line. If you want to work with more than one file or directory, you need to use a technique the Linux world calls globbing.

Globbing is basically the use of wildcard characters to represent one or more characters in a file or directory name. That feature allows us to specify a pattern for Linux to match multiple files or directories against. There are two basic globbing characters that you can use:

The question mark is a stand-in character to represent any single character to match in the filename. For example, you can specify the filename file?.txt in a rm command to remove any file that starts with “file”, then has one character, then ends in “.txt”. Here’s an example:

An output shows information about several files using the r m and l s commands.
Description

An output shows information about several files using the r m and l s commands.
Description

The rm command uses the glob “file?.txt” as the parameter. Linux looks for any file in the directory that matches the pattern to remove. Two files, file1.txt and file2.txt, match the pattern. However, the file10.txt file doesn’t match the pattern because there are two characters between the “file” and “.txt” parts of the filename, and the file.txt file doesn’t match the pattern because there aren’t any characters between the “file” and “.txt” parts of the filename.

You use the asterisk glob character to match zero or more characters in the filename:

An output shows details about four files using r m command with file asterisk dot t x t parameter.
Description

By using the asterisk, Linux matched all of the files that started with “file” and ended with “.txt”, even the file.txt file! You can use the asterisk in any list, copy, move, or delete operation in the command line.

Quoting

Another issue you may run into with Linux is files or directories that contain spaces in their names. This is perfectly legal in Linux but can cause headaches when working from the command line.

If you try to reference a file or directory that contains a space in the filename, you’ll get an error message:

An output shows the usage of l s and r m commands.
Description

An output shows the usage of l s and r m commands.
Description

The problem is that by default, the rm command uses a space to indicate the end of a filename, so it thinks you’re trying to remove three separate files – “long”, “file”, and “name.txt”!

There are two ways to get around that. One way is to use quoting, which places quotes around any filenames that contain spaces:

Line 1: dollar space r m space double quote long space file space name dot t x t double quote. Line 2: dollar space l s space hyphen l. Line 3: total space 0. Line 4: dollar.

You can use either single or double quotes around the filename, as long as you use the same type on both ends of the filename.

The other method is to use escaping, which places a backslash character in front of the character you want the Linux shell to interpret as a data character and not a shell character:

Dollar space r m space long backslash space file backslash space name dot t x t.

Either method works just fine when working with spaces in file names.

Case Sensitivity

One last thing to watch out for when using the Linux file handling command line commands is the case of any file or directory names that you’re working with. Linux is a case-sensitive operating system, so files and directories can have both upper and lower case letters in the names. Likewise, as you’re working with the files, make sure you reference the correct format of the files or directory names:

An output shows the usage of r m command against files with similar names but with different cases.
Description

This is a good example of when using filename globbing can come in handy. If you’re not sure of the case of a character, you can use the question mark to represent any character of any case:

Line 1: dollar space r m space question mark i l e 1 dot t x t. Line 2: dollar.

This command will remove both the file1.txt and File1.txt files.

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

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