The File-system Hierarchy
All files on a Linux system are stored on file systems, which are organized into a single inverted tree of directories, known as a file-system hierarchy. This tree is inverted because the root of the tree is said to be at the top of the hierarchy, and the branches of directories and subdirectories stretch below the root.
The / directory is the root directory at the top of the file-system hierarchy. The / character is also used as a directory separator in file names. For example, if etc is a subdirectory of the / directory, you could refer to that directory as /etc. Likewise, if the /etc directory contained a file named issue, you could refer to that file as /etc/issue.
Subdirectories of / are used for standardized purposes to organize files by type and purpose. This makes it easier to find files. For example, in the root directory, the subdirectory /boot is used for storing files needed to boot the system.
Important Red Hat Enterprise Linux Directories
/usr pourpose to Installed software, shared libraries, include files, and read-only program data. Important subdirectories include:
- /usr/bin: User commands.
- /usr/sbin: System administration commands.
- /usr/local: Locally customized software.
/etc pourpose to configuration files specific to this system.
/var pourpose to Variable data specific to this system that should persist between boots. Files that dynamically change, such as databases, cache directories, log files, printer-spooled documents, and website content may be found under /var.
/run pourpose to runtime data for processes started since the last boot. This includes process ID files and lock files, among other things. The contents of this directory are recreated on reboot. This directory consolidates /var/run and /var/lock from earlier versions of Red Hat Enterprise Linux.
/home pourpose to Home directories are where regular users store their personal data and configuration files.
/root pourpose to Home directory for the administrative superuser, root.
/tmp pourpose to A world-writable space for temporary files. Files which have not been accessed, changed, or modified for 10 days are deleted from this directory automatically. Another temporary directory exists, /var/tmp, in which files that have not been accessed, changed, or modified in more than 30 days are deleted automatically.
/boot pourpose to Files needed in order to start the boot process.
/dev pourpose to Contains special device files that are used by the system to access hardware.
Absolute Paths and Relative Paths
The path of a file or directory specifies its unique file system location. Following a file path traverses one or more named subdirectories, delimited by a forward slash (/), until the destination is reached. Directories, also called folders, contain other files and other subdirectories. They can be referenced in the same manner as files.
-Absolute Paths
An absolute path is a fully qualified name, specifying the files exact location in the file system hierarchy. It begins at the root (/) directory and specifies each subdirectory that must be traversed to reach the specific file. Every file in a file system has a unique absolute path name, recognized with a simple rule: A path name with a forward slash (/) as the first character is an absolute path name. For example, the absolute path name for the system message log file is /var/log/messages. Absolute path names can be long to type, so files may also be located relative to the current working directory for your shell prompt.
The Current Working Directory and Relative Paths
When a user logs in and opens a command window, the initial location is normally the user's home directory. System processes also have an initial directory. Users and processes navigate to other directories as needed; the terms working directory or current working directory refer to their current location.
Like an absolute path, a relative path identifies a unique file, specifying only the path necessary to reach the file from the working directory. Recognizing relative path names follows a simple rule: A path name with anything other than a forward slash as the first character is a relative path name. A user in the /var directory could refer to the message log file relatively as log/messages.
Linux file systems, including, but not limited to, ext4, XFS, GFS2, and GlusterFS, are case-sensitive. Creating FileCase.txt and filecase.txt in the same directory results in two unique files.
Non-Linux file systems might work differently. For example, VFAT, Microsoft's NTFS, and Apple's HFS+ have case preserving behavior. Although these file systems are not case-sensitive, they do display file names with the original capitalization used when the file was created. Therefore, if you tried to make the files in the preceding example on a VFAT file system, both names would be treated as pointing to the same file instead of two different files.
Navigating Paths
The pwd command displays the full path name of the current working directory for that shell. This can help you determine the syntax to reach files using relative path names. The ls command lists directory contents for the specified directory or, if no directory is given, for the current working directory.
Use the cd command to change your shell's current working directory. If you do not specify any arguments to the command, it will change to your home directory.
The touch command normally updates a file's timestamp to the current date and time without otherwise modifying it. This is useful for creating empty files, which can be used for practice, because "touching" a file name that does not exist causes the file to be created. In the following example, the touch command creates practice files in the Documents and Videos subdirectories.
The ls command has multiple options for displaying attributes on files. The most common and useful are -l (long listing format), -a (all files, including hidden files), and -R (recursive, to include the contents of all subdirectories).
The two special directories at the top of the listing refer to the current directory (.) and the parent directory (..). These special directories exist in every directory on the system. You will discover their usefulness when you start using file management commands.
The cd command has many options. A few are so useful as to be worth practicing early and using often. The command cd - changes to the previous directory; where the user was previously to the current directory.
The cd .. command uses the .. hidden directory to move up one level to the parent directory, without needing to know the exact parent name. The other hidden directory (.) specifies the current directory on commands in which the current location is either the source or destination argument, avoiding the need to type out the directory's absolute path name.
Managing Files Using Command-line Tools
Objectives
NOTE
Create a directory -> mkdir directory
Copy a file -> cp file new-file
Copy a directory and its contents -> cp -r directory new-directory
Move or rename a file or directory -> mv file new-file
Remove a file -> rm file
Remove a directory containing files -> rm -r directory
Remove an empty directory -> rmdir directory
Creating Directories
The mkdir command creates one or more directories or subdirectories. It takes as arguments a list of paths to the directories you want to create.
The mkdir command will fail with an error if the directory already exists, or if you are trying to create a subdirectory in a directory that does not exist. The -p (parent) option creates missing parent directories for the requested destination. Use the mkdir -p command with caution, because spelling mistakes can create unintended directories without generating error messages.
Use the mkdir -p command and space-delimited relative paths for each of the subdirectory names to create multiple parent directories with subdirectories.
The last mkdir command created three ChapterN subdirectories with one command. The -p option created the missing parent directory Thesis.
Copying Files
The cp command copies a file, creating a new file either in the current directory or in a specified directory. It can also copy multiple files to a directory.
When copying multiple files with one command, the last argument must be a directory. Copied files retain their original names in the new directory. If a file with the same name exists in the target directory, the existing file is overwritten. By default, the cp does not copy directories; it ignores them.
Moving Files
The mv command moves files from one location to another. If you think of the absolute path to a file as its full name, moving a file is effectively the same as renaming a file. File contents remain unchanged.
Use the mv command to rename a file.
Use the mv command to move a file to a different directory.
Removing Files and Directories
The rm command removes files. By default, rm will not remove directories that contain files, unless you add the -r or --recursive option.
It is a good idea to verify your current working directory before removing a file or directory.
Use the rm command to remove a single file from your working directory.
Use the rm -r command to remove a subdirectory and its contents.
The rm -r command traverses each subdirectory first, individually removing their files before removing each directory. You can use the rm -ri command to interactively prompt for confirmation before deleting. This is essentially the opposite of using the -f option, which forces the removal without prompting the user for confirmation.
Managing Links Between Files
Hard Links and Soft Links
It is possible to create multiple names that point to the same file. There are two ways to do this: by creating a hard link to the file, or by creating a soft link (sometimes called a symbolic link) to the file. Each has its advantages and disadvantages.
Creating Hard Links
Every file starts with a single hard link, from its initial name to the data on the file system. When you create a new hard link to a file, you create another name that points to that same data. The new hard link acts exactly like the original file name. Once created, you cannot tell the difference between the new hard link and the original name of the file.
You can find out if a file has multiple hard links with the ls -l command. One of the things it reports is each file's link count, the number of hard links the file has.
Limitations of Hard Links
Hard links have some limitations. Firstly, hard links can only be used with regular files. You cannot use ln to create a hard link to a directory or special file.
Secondly, hard links can only be used if both files are on the same file system. The file-system hierarchy can be made up of multiple storage devices. Depending on the configuration of your system, when you change into a new directory, that directory and its contents may be stored on a different file system.
You can use the df command to list the directories that are on different file systems.
[user@host ~]$ df
Creating Soft Links
The ln -s command creates a soft link, which is also called a "symbolic link." A soft link is not a regular file, but a special type of file that points to an existing file or directory.
Soft links have some advantages over hard links:
•They can link two files on different file systems.
•They can point to a directory or special file, not just a regular file.
[user@host ~]$ ln -s /home/user/newfile-link2.txt /tmp/newfile-symlink.txt
[user@host ~]$ ls -l newfile-link2.txt /tmp/newfile-symlink.txt
[user@host ~]$ cat /tmp/newfile-symlink.txt
A soft link can point to a directory. The soft link then acts like a directory. Changing to the soft link with cd will make the current working directory the linked directory. Some tools may keep track of the fact that you followed a soft link to get there. For example, by default cd will update your current working directory using the name of the soft link rather than the name of the actual directory. (There is an option, -P, that will update it with the name of the actual directory instead.)
Command-line Expansions
The Bash shell has multiple ways of expanding a command line including pattern matching, home directory expansion, string expansion, and variable substitution. Perhaps the most powerful of these is the path name-matching capability, historically called globbing. The Bash globbing feature, sometimes called “wildcards”, makes managing large numbers of files easier. Using metacharacters that “expand” to match file and path names being sought, commands perform on a focused set of files at once.
Pattern Matching
Globbing is a shell command-parsing operation that expands a wildcard pattern into a list of matching path names. Command-line metacharacters are replaced by the match list prior to command execution. Patterns that do not return matches display the original pattern request as literal text. The following are common metacharacters and pattern classes.
***** -> Any string of zero or more characters.
? -> Any single character.
[abc...] -> Any one character in the enclosed class (between the square brackets).
[!abc...] -> Any one character not in the enclosed class.
[^abc...] -> Any one character not in the enclosed class.
[[:alpha:]] -> Any alphabetic character.
[[:lower:]] -> Any lowercase character.
[[:upper:]] -> Any uppercase character.
[[:alnum:]] -> Any alphabetic character or digit.
[[:punct:]] -> Any printable character not a space or alphanumeric.
[[:digit:]] -> Any single digit from 0 to 9.
[[:space:]] -> Any single white space character. This may include tabs, newlines, carriage returns, form feeds, or spaces.
Tilde Expansion
The tilde character (~), matches the current user's home directory. If it starts a string of characters other than a slash (/), the shell will interpret the string up to that slash as a user name, if one matches, and replace the string with the absolute path to that user's home directory. If no user name matches, then an actual tilde followed by the string of characters will be used instead.
the echo command is used to display the value of the tilde character.
[user@host glob]$ echo ~root
[user@host glob]$ echo ~user
[user@host glob]$ echo ~/glob
[user@host glob]$
Brace Expansion
Brace expansion is used to generate discretionary strings of characters. Braces contain a comma-separated list of strings, or a sequence expression. The result includes the text preceding or following the brace definition. Brace expansions may be nested, one inside another. Also double-dot syntax (..) expands to a sequence such that {m..p} will expand to m n o p.
A practical use of brace expansion is to quickly create a number of files or directories.
[user@host glob]$ mkdir ../RHEL{6,7,8}
[user@host glob]$ ls ../RHEL*
[user@host glob]$
Variable Expansion
A variable acts like a named container that can store a value in memory. Variables make it easy to access and modify the stored data either from the command line or within a shell script.
You can assign data as a value to a variable using the following syntax:
[user@host ~]$ VARIABLENAME=value
Command Substitution
Command substitution allows the output of a command to replace the command itself on the command line. Command substitution occurs when a command is enclosed in parentheses, and preceded by a dollar sign ($). The $(command) form can nest multiple command expansions inside each other.
[user@host glob]$ echo Today is $(date +%A).
[user@host glob]$ echo The time is $(date +%M) minutes past $(date +%l%p).
[user@host glob]$
Protecting Arguments from Expansion
Many characters have special meaning in the Bash shell. To keep the shell from performing shell expansions on parts of your command line, you can quote and escape characters and strings.
The backslash () is an escape character in the Bash shell. It will protect the character immediately following it from expansion.
To protect longer character strings, single quotes (') or double quotes (") are used to enclose strings. They have slightly different effects. Single quotes stop all shell expansion. Double quotes stop most shell expansion.
Use double quotation marks to suppress globbing and shell expansion, but still allow command and variable substitution.
[user@host glob]$ myhost=$(hostname -s); echo $myhost
[user@host glob]$ echo "***** hostname is ${myhost} *****"
[user@host glob]$
Use single quotation marks to interpret all text literally.
[user@host glob]$ echo "Will variable $myhost evaluate to $(hostname -s)?"
[user@host glob]$ echo 'Will variable $myhost evaluate to $(hostname -s)?'
[user@host glob]$
Top comments (0)