Chapter 3: Manage Files from the Command Line PDF

Summary

This document provides an overview of Linux file system hierarchy concepts. It explains how files are organized, the purposes of various directories, and the significance of the root directory (/). It also uses simple examples to show how files, directories and paths work.

Full Transcript

Chapter 3. Manage Files from the Command Line Describe Linux File System Hierarchy Concepts Objectives Describe how Linux organizes files, and the purposes of various directories in the file-system hierarchy. The File-system Hierarchy The Linux system stores all files on file...

Chapter 3. Manage Files from the Command Line Describe Linux File System Hierarchy Concepts Objectives Describe how Linux organizes files, and the purposes of various directories in the file-system hierarchy. The File-system Hierarchy The Linux system stores all files on file systems, which are organized into a single inverted tree known as a file-system hierarchy. This hierarchy is an inverted tree because the tree root is at the top, and the branches of directories and subdirectories stretch below the root. Figure 3.1: Significant file-system directories in Red Hat Enterprise Linux 9 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, then refer to that directory as /etc. Likewise, if the /etc directory contains a file that is named issue, then refer to that file as /etc/issue. Subdirectories of / are used for standardized purposes to organize files by type and purpose to make it easier to find files. For example, in the root directory, the /boot subdirectory is used for storing files to boot the system. NOTE The following terms help to describe file-system directory contents: Static content remains unchanged until explicitly edited or reconfigured. Dynamic or variable content might be modified or appended by active processes. Persistent content remains after a reboot, such as configuration settings. Runtime content from a process or from the system is deleted on reboot. The following table lists some of the significant directories on the system by name and purpose. Table 3.1. Significant Red Hat Enterprise Linux Directories Location Purpose /boot Files to start the boot process. /dev Special device files that the system uses to access hardware. /etc System-specific configuration files. /home Home directory, where regular users store their data and configuration files. /root Home directory for the administrative superuser, root. /run Runtime data for processes that started since the last boot. This data includes process ID files and lock files. The contents of this directory are re-created on reboot. This directory Location Purpose consolidates the /var/run and /var/lock directories from earlier versions of Red Hat Enterprise Linux. /tmp A world-writable space for temporary files. Files that are not accessed, changed, or modified for 10 days are deleted from this directory automatically. The /var/tmp directory is also a temporary directory, in which files that are not accessed, changed, or modified in more than 30 days are deleted automatically. /usr Installed software, shared libraries, including files, and read-only program data. Significant subdirectories in the /usr directory include the following commands: /usr/bin: User commands /usr/sbin: System administration commands /usr/local: Locally customized software /var System-specific variable data should persist between boots. Files that dynamically change, such as databases, cache directories, log files, printer-spooled documents, and website content, might be found under /var. IMPORTANT In Red Hat Enterprise Linux 7 and later, four older directories in / have identical contents to their counterparts in /usr: /bin and /usr/bin /sbin and /usr/sbin /lib and /usr/lib /lib64 and /usr/lib64 Earlier versions of Red Hat Enterprise Linux had distinct directories with different sets of files. In Red Hat Enterprise Linux 7 and later, the directories in / are symbolic links to the matching directories in /usr. Specify Files by Name Objectives Specify the absolute location and relative location of files to the current working directory, determine and change the working directory, and list the contents of directories. 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, which are delimited by a forward slash (/), until the destination is reached. Directories, also called folders, can contain other files and other subdirectories. Directories are referenced in the same manner as files. IMPORTANT A space character is acceptable in a Linux file name. The shell also uses spaces to distinguish options and arguments on the command line. If a command includes a file with a space in its name, then the shell can misinterpret the command and assume that the file name is multiple arguments. To avoid this mistake, surround such file names in quotation marks so that the shell interprets the name as a single argument. Red Hat recommends avoiding spaces at all in file names. Absolute Paths An absolute path is a fully qualified name that specifies the exact location of the file in the file-system hierarchy. The absolute path begins at the root (/) directory and includes each subdirectory that must be traversed to reach the specific file. Every file in a file system has a unique absolute path name, which is 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 can also be located relative to the current working directory of your shell prompt. The Current Working Directory and Relative Paths When a user logs in and opens a command window, the initial location is typically the user's home directory. System processes also have an initial directory. Users and processes change to other directories as needed. The working directory and current working directory terms refer to their current location. Similar to an absolute path, a relative path identifies a unique location, and specifies only the necessary path to reach the location from the working directory. Relative path names follow this rule: a path name with anything other than a forward slash as the first character is a relative path name. For example, relative to the /var directory, the message log file is log/messages. Linux file systems, including ext4, XFS, GFS2, and GlusterFS, are case-sensitive. Creating the FileCase.txt and filecase.txt files in the same directory results in two unique files. Non-Linux file systems might work differently. For example, VFAT, Microsoft NTFS, and Apple HFS+ have case-preserving behavior. Although these file systems are not case-sensitive, they do display file names with the file's original capitalization. By creating the files in the preceding example on a VFAT file system, both names would point to the same file instead of to two different files. Navigate Paths in the File System The pwd command displays the full path name of the current working directory for that shell. This command helps you to determine the syntax to reach files by 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. [user@host ~]$ pwd /home/user [user@host ~]$ ls Desktop Documents Downloads Music Pictures Public Templates Videos [user@host ~]$ Use the cd command to change your shell's current working directory. If you do not specify any arguments to the command, then it changes to your home directory. In the following example, a mixture of absolute and relative paths are used with the cd command to change the current working directory for the shell. [user@host ~]$ pwd /home/user [user@host ~]$ cd Videos [user@host Videos]$ pwd /home/user/Videos [user@host Videos]$ cd /home/user/Documents [user@host Documents]$ pwd /home/user/Documents [user@host Documents]$ cd [user@host ~]$ pwd /home/user [user@host ~]$ In the preceding example, the default shell prompt also displays the last component of the absolute path to the current working directory. For example, for the /home/user/Videos directory, only the Videos directory is displayed. The prompt displays the tilde character (~) when your current working directory is your home directory. The touch command updates the time stamp of a file to the current date and time without otherwise modifying it. This command is useful for creating empty files, and can be used for practice, because when you use the touch command with a file name that does not exist, the file is created. In the following example, the touch command creates practice files in the Documents and Videos subdirectories. [user@host ~]$ touch Videos/blockbuster1.ogg [user@host ~]$ touch Videos/blockbuster2.ogg [user@host ~]$ touch Documents/thesis_chapter1.txt [user@host ~]$ touch Documents/thesis_chapter2.txt [user@host ~]$ The ls command has multiple options for displaying attributes on files. The most common options are -l (long listing format), -a (all files, including hidden files), and -R (recursive, to include the contents of all subdirectories). [user@host ~]$ ls -l total 0 drwxr-xr-x. 2 user user 6 Mar 2 02:45 Desktop drwxr-xr-x. 2 user user 6 Mar 2 02:45 Documents drwxr-xr-x. 2 user user 6 Mar 2 02:45 Downloads drwxr-xr-x. 2 user user 6 Mar 2 02:45 Music drwxr-xr-x. 2 user user 6 Mar 2 02:45 Pictures drwxr-xr-x. 2 user user 6 Mar 2 02:45 Public drwxr-xr-x. 2 user user 6 Mar 2 02:45 Templates drwxr-xr-x. 2 user user 6 Mar 2 02:45 Videos [user@host ~]$ ls -la total 40 drwx------. 17 user user 4096 Mar 2 03:07. drwxr-xr-x. 4 root root 35 Feb 10 10:48.. drwxr-xr-x. 4 user user 27 Mar 2 03:01.ansible -rw-------. 1 user user 444 Mar 2 04:32.bash_history -rw-r--r--. 1 user user 18 Aug 9 2021.bash_logout -rw-r--r--. 1 user user 141 Aug 9 2021.bash_profile -rw-r--r--. 1 user user 492 Aug 9 2021.bashrc drwxr-xr-x. 9 user user 4096 Mar 2 02:45.cache drwxr-xr-x. 9 user user 4096 Mar 2 04:32.config drwxr-xr-x. 2 user user 6 Mar 2 02:45 Desktop drwxr-xr-x. 2 user user 6 Mar 2 02:45 Documents...output omitted... At the top of the listing are two special directories. One dot (.) refers to the current directory, and two dots (..) refer to the parent directory. These special directories exist in every directory on the system, and they are useful when using file management commands. IMPORTANT File names that begin with a dot (.) indicate hidden files; you cannot see them in the normal view with ls and other commands. This behavior is not a security feature. Hidden files keep necessary user configuration files from cluttering home directories. Many commands process hidden files only with specific command-line options, and prevent one user's configuration from being accidentally copied to other directories or users. To protect file contents from improper viewing requires the use of file permissions. You can also use the tilde (~) special character in combination with other commands for better interaction with the home directory. [user@host ~]$ cd /var/log/ [user@host log]$ ls -l ~ total 0 drwxr-xr-x. 2 user user 6 Mar 2 02:45 Desktop drwxr-xr-x. 2 user user 6 Mar 2 02:45 Documents drwxr-xr-x. 2 user user 6 Mar 2 02:45 Downloads drwxr-xr-x. 2 user user 6 Mar 2 02:45 Music drwxr-xr-x. 2 user user 6 Mar 2 02:45 Pictures drwxr-xr-x. 2 user user 6 Mar 2 02:45 Public drwxr-xr-x. 2 user user 6 Mar 2 02:45 Templates drwxr-xr-x. 2 user user 6 Mar 2 02:45 Videos [user@host ~]$ The cd command has many options. Some options are useful to practice early and to use often. The cd - command changes to the previous directory, where the user was previously to the current directory. The following example illustrates this behavior, and alternates between two directories, which is useful when processing a series of similar tasks. [user@host ~]$ cd Videos [user@host Videos]$ pwd /home/user/Videos [user@host Videos]$ cd /home/user/Documents [user@host Documents]$ pwd /home/user/Documents [user@host Documents]$ cd - [user@host Videos]$ pwd /home/user/Videos [user@host Videos]$ cd - [user@host Documents]$ pwd /home/user/Documents [user@host Documents]$ cd - [user@host Videos]$ pwd /home/user/Videos [user@host Videos]$ cd [user@host ~]$ 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 where the current location is either the source or destination argument, and avoids the need to type the directory's absolute path name. [user@host Videos]$ pwd /home/user/Videos [user@host Videos]$ cd. [user@host Videos]$ pwd /home/user/Videos [user@host Videos]$ cd.. [user@host ~]$ pwd /home/user [user@host ~]$ cd.. [user@host home]$ pwd /home [user@host home]$ cd.. [user@host /]$ pwd / [user@host /]$ cd [user@host ~]$ pwd /home/user [user@host ~]$ Manage Files with Command-line Tools Objectives Create, copy, move, and remove files and directories. Command-line File Management Creating, copying, moving, and, removing files and directories are common operations for a system administrator. Without options, some commands are used to interact with files, or they can manipulate directories with the appropriate set of options. Be aware of the options that are used when running a command. The meaning of some options might differ between commands. Create Directories The mkdir command creates one or more directories or subdirectories. It takes as an argument a list of paths to the directories that you want to create. In the following example, files and directories are organized beneath the /home/user/Documents directory. Use the mkdir command and a space-delimited list of the directory names to create multiple directories. [user@host ~]$ cd Documents [user@host Documents]$ mkdir ProjectX ProjectY ProjectZ [user@host Documents]$ ls ProjectX ProjectY ProjectZ If the directory exists, or a parent directory of the directory that you are trying to create does not exist, then the mkdir command fails and it displays an error. The mkdir command -p (parent) option creates any missing parent directories for the requested destination. In the following example, the mkdir command creates three ChapterN subdirectories with one command. The -p option creates the missing Thesis parent directory. [user@host Documents]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3 [user@host Documents]$ ls -R Thesis/ Thesis/: Chapter1 Chapter2 Chapter3 Thesis/Chapter1: Thesis/Chapter2: Thesis/Chapter3: Use the mkdir command -p option with caution, because spelling mistakes can create unintended directories without generating error messages. In the following example, imagine that you are trying to create a Watched subdirectory in the Videos directory, but you accidentally omitted the letter "s" in Videos in your mkdir command. [user@host ~]$ mkdir Video/Watched mkdir: cannot create directory Video/Watched: No such file or directory The mkdir command fails because the Video directory does not exist. If you had used the mkdir command with the -p option, then the Video directory would be created unintentionally. The Watched subdirectory would be created in that incorrect directory. Copy Files and Directories The cp command copies a file, and creates a file either in the current directory or in a different specified directory. [user@host ~]$ cd Videos [user@host Videos]$ cp blockbuster1.ogg blockbuster3.ogg [user@host Videos]$ ls -l total 0 -rw-rw-r--. 1 user user 0 Feb 8 16:23 blockbuster1.ogg -rw-rw-r--. 1 user user 0 Feb 8 16:24 blockbuster2.ogg -rw-rw-r--. 1 user user 0 Feb 8 16:34 blockbuster3.ogg You can also use the cp command to copy multiple files to a directory. In this scenario, the last argument must be a directory. The copied files retain their original names in the new directory. If a file with the same name exists in the target directory, then the existing file is overwritten. NOTE By default, the cp command does not copy directories; it ignores them. In the following example, two directories are listed as arguments, the Thesis and ProjectX directories. The last argument, the ProjectX directory, is the target and is valid as a destination. The Thesis argument is ignored by the cp command, because it is intended to be copied and it is a directory. [user@host Documents]$ cp thesis_chapter1.txt thesis_chapter2.txt Thesis ProjectX cp: -r not specified; omitting directory 'Thesis' [user@host Documents]$ ls Thesis ProjectX ProjectX: thesis_chapter1.txt thesis_chapter2.txt Thesis: Chapter1 Chapter2 Chapter3 The Thesis directory failed to copy, but the copying of the thesis_chapter1.txt and thesis_chapter2.txt files succeeded. You can copy directories and their contents by using the cp command -r option. Keep in mind that you can use the. and.. special directories in command combinations. In the following example, the Thesis directory and its contents are copied to the ProjectY directory. [user@host Documents]$ cd ProjectY [user@host ProjectY]$ cp -r../Thesis/. [user@host ProjectY]$ ls -lR.: total 0 drwxr-xr-x. 5 user user 54 Mar 7 15:08 Thesis./Thesis: total 0 drwxr-xr-x. 2 user user 6 Mar 7 15:08 Chapter1 drwxr-xr-x. 2 user user 6 Mar 7 15:08 Chapter2 drwxr-xr-x. 2 user user 6 Mar 7 15:08 Chapter3./Thesis/Chapter1: total 0./Thesis/Chapter2: total 0./Thesis/Chapter3: total 0 Move Files and Directories The mv command moves files from one location to another. If you think of the absolute path to a file as its full name, then moving a file is effectively the same as renaming a file. The contents of the files that are moved remain unchanged. Use the mv command to rename a file. In the following example, the mv thesis_chapter2.txt command renames the thesis_chapter2.txt file to thesis_chapter2_reviewed.txt in the same directory. [user@host Documents]$ ls -l -rw-r--r--. 1 user user 7100 Mar 7 14:37 thesis_chapter1.txt -rw-r--r--. 1 user user 11431 Mar 7 14:39 thesis_chapter2.txt...output omitted... [user@host Documents]$ mv thesis_chapter2.txt thesis_chapter2_reviewed.txt [user@host Documents]$ ls -l -rw-r--r--. 1 user user 7100 Mar 7 14:37 thesis_chapter1.txt -rw-r--r--. 1 user user 11431 Mar 7 14:39 thesis_chapter2_reviewed.txt...output omitted... Use the mv command to move a file to a different directory. In the next example, the thesis_chapter1.txt file is moved from the ~/Documents directory to the ~/Documents/Thesis/Chapter1 directory. You can use the mv command -v option to display a detailed output of the command operations. [user@host Documents]$ ls Thesis/Chapter1 [user@host Documents]$ [user@host Documents]$ mv -v thesis_chapter1.txt Thesis/Chapter1 renamed 'thesis_chapter1.txt' -> 'Thesis/Chapter1/thesis_chapter1.txt' [user@host Documents]$ ls Thesis/Chapter1 thesis_chapter1.txt [user@host Documents]$ ls -l -rw-r--r--. 1 user user 11431 Mar 7 14:39 thesis_chapter2_reviewed.txt...output omitted... Remove Files and Directories The rm command removes files. By default, rm does not remove directories. You can use the rm command -r or the --recursive option to enable the rm command to remove directories and their contents. The rm -r command traverses each subdirectory first, and individually removes their files before removing each directory. In the following example, the rm command removes the thesis_chapter1.txt file without options, but to remove the Thesis/Chapter1 directory, you must add the - r option. [user@host Documents]$ ls -l Thesis/Chapter1 -rw-r--r--. 1 user user 7100 Mar 7 14:37 thesis_chapter1.txt [user@host Documents]$ rm Thesis/Chapter1/thesis_chapter1.txt [user@host Documents]$ rm Thesis/Chapter1 rm: cannot remove 'Thesis/Chapter1': Is a directory [user@host Documents]$ rm -r Thesis/Chapter1 [user@host Documents]$ ls -l Thesis drwxr-xr-x. 2 user user 6 Mar 7 12:37 Chapter2 drwxr-xr-x. 2 user user 6 Mar 7 12:37 Chapter3 IMPORTANT Red Hat Enterprise Linux does not provide a command-line undelete feature, nor a "trash bin" from which you can restore files held for deletion. A trash bin is a component of a desktop environment such as GNOME, but is not used by commands that are run from a shell. It is a good idea to verify your current working directory before you remove a file or directory by using relative paths. [user@host Documents]$ pwd /home/user/Documents [user@host Documents]$ ls -l thesis* -rw-r--r--. 1 user user 11431 Mar 7 14:39 thesis_chapter2_reviewed.txt [user@host Documents]$ rm thesis_chapter2_reviewed.txt [user@host Documents]$ ls -l thesis* ls: cannot access 'thesis*': No such file or directory You can use the rm command -i option to interactively prompt for confirmation before deleting. This option is essentially the opposite of using the rm command - f option, which forces the removal without prompting the user for confirmation. [user@host Documents]$ rm -ri Thesis rm: descend into directory 'Thesis'? y rm: descend into directory 'Thesis/Chapter2'? y rm: remove regular empty file 'Thesis/Chapter2/thesis_chapter2.txt'? y rm: remove directory 'Thesis/Chapter2'? y rm: remove directory 'Thesis/Chapter3'? y rm: remove directory 'Thesis'? y WARNING If you specify both the -i and -f options, then the -f option takes priority and you are not prompted for confirmation before rm removes files. You can also use the rmdir command to remove empty directories. Use the rm command -r option to remove non-empty directories. [user@host Documents]$ pwd /home/user/Documents [user@host Documents]$ rmdir ProjectZ [user@host Documents]$ rmdir ProjectX rmdir: failed to remove 'ProjectX': Directory not empty [user@host Documents]$ rm -r ProjectX [user@host Documents]$ Make Links Between Files Objectives Make multiple file names reference the same file with hard links and symbolic (or "soft") links. Manage Links Between Files You can create multiple file names that point to the same file. These file names are called links. You can create two types of links: a hard link, or a symbolic link (sometimes called a soft link). Each way has its advantages and disadvantages. Create 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 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. After the link is created, you cannot tell the difference between the new hard link and the original name of the file. You can determine whether a file has multiple hard links by using the ls - l command. One item that it reports is each file's link count, the number of hard links that the file has. In the next example, the link count of the newfile.txt file is 1. It has exactly one absolute path, which is the /home/user/newfile.txt location. [user@host ~]$ pwd /home/user [user@host ~]$ ls -l newfile.txt -rw-r--r--. 1 user user 0 Mar 11 19:19 newfile.txt You can use the ln command to create a hard link (another file name) that points to an existing file. The command needs at least two arguments: a path to the existing file, and the path to the hard link that you want to create. The following example creates a hard link called newfile-hlink2.txt for the existing newfile.txt file in the /tmp directory. [user@host ~]$ ln newfile.txt /tmp/newfile-hlink2.txt [user@host ~]$ ls -l newfile.txt /tmp/newfile-hlink2.txt -rw-rw-r--. 2 user user 12 Mar 11 19:19 newfile.txt -rw-rw-r--. 2 user user 12 Mar 11 19:19 /tmp/newfile-hlink2.txt To determine whether two files are hard linked, use the ls command -i option to list each file's inode number. If the files are on the same file system and their inode numbers are the same, then the files are hard links that point to the same data file content. [user@host ~]$ ls -il newfile.txt /tmp/newfile-hlink2.txt 8924107 -rw-rw-r--. 2 user user 12 Mar 11 19:19 newfile.txt 8924107 -rw-rw-r--. 2 user user 12 Mar 11 19:19 /tmp/newfile-hlink2.txt IMPORTANT Hard links that reference the same file share the inode structure with the link count, access permissions, user and group ownership, time stamps, and file content. When that information is changed for one hard link, then the other hard links for the same file also show the new information. This behavior is because each hard link points to the same data on the storage device. Even if the original file is deleted, you can still access the contents of the file provided that at least one other hard link exists. Data is deleted from storage only when the last hard link is deleted, which makes the file contents unreferenced by any hard link. [user@host ~]$ rm -f newfile.txt [user@host ~]$ ls -l /tmp/newfile-hlink2.txt -rw-rw-r--. 1 user user 12 Mar 11 19:19 /tmp/newfile-hlink2.txt [user@host ~]$ cat /tmp/newfile-hlink2.txt Hello World Limitations of Hard Links Hard links have some limitations. First, you can use hard links only with regular files. You cannot use the ln command to create a hard link to a directory or special file. Second, you can use hard links only if both files are on the same file system. The file- system hierarchy can be composed of multiple storage devices. Depending on the configuration of your system, when you change into a new directory, that directory and its contents might be stored on a different file system. You can use the df command to list the directories that are on different file systems. For example, you might see the following output: [user@host ~]$ df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 886788 0 886788 0% /dev tmpfs 902108 0 902108 0% /dev/shm tmpfs 902108 8696 893412 1% /run tmpfs 902108 0 902108 0% /sys/fs/cgroup /dev/mapper/rhel_rhel9--root 10258432 1630460 8627972 16% / /dev/sda1 1038336 167128 871208 17% /boot tmpfs 180420 0 180420 0% /run/user/1000 Files in two different "Mounted on" directories and their subdirectories are on different file systems. So, in the system in this example, you can create a hard link between the /var/tmp/link1 and /home/user/file files, because they are both subdirectories of the / directory but not of any other directory on the list. However, you cannot create a hard link between the /boot/test/badlink and /home/user/file files. The first file is in a subdirectory of the /boot directory (on the "Mounted on" list) and it is in the /dev/sda1 file system. The second file is in the /dev/mapper/rhel_rhel9—root file system. Create Symbolic Links The ln command -s option creates a symbolic link, which is also called a "soft link". A symbolic link is not a regular file, but a special type of file that points to an existing file or directory. Symbolic links have some advantages over hard links: Symbolic links can link two files on different file systems. Symbolic links can point to a directory or special file, not just to a regular file. In the following example, the ln -s command creates a symbolic link for the /home/user/newfile-link2.txt file. The name for the symbolic link is /tmp/newfile-symlink.txt. [user@host ~]$ ln -s /home/user/newfile-link2.txt /tmp/newfile-symlink.txt [user@host ~]$ ls -l newfile-link2.txt /tmp/newfile-symlink.txt -rw-rw-r--. 1 user user 12 Mar 11 19:19 newfile-link2.txt lrwxrwxrwx. 1 user user 11 Mar 11 20:59 /tmp/newfile-symlink.txt -> /home/user/newfil e-link2.txt [user@host ~]$ cat /tmp/newfile-symlink.txt Symbolic Hello World In the preceding example, the first character of the long listing for the /tmp/newfile- symlink.txt file is l (letter l) instead of -. This character indicates that the file is a symbolic link and not a regular file. When the original regular file is deleted, the symbolic link still points to the file but the target is gone. A symbolic link that points to a missing file is called a "dangling symbolic link". [user@host ~]$ rm -f newfile-link2.txt [user@host ~]$ ls -l /tmp/newfile-symlink.txt lrwxrwxrwx. 1 user user 11 Mar 11 20:59 /tmp/newfile-symlink.txt -> /home/user/newfil e-link2.txt [user@host ~]$ cat /tmp/newfile-symlink.txt cat: /tmp/newfile-symlink.txt: No such file or directory IMPORTANT One side-effect of the dangling symbolic link in the preceding example is that if you create a file with the same name as the deleted file (/home/user/newfile-link2.txt), then the symbolic link is no longer "dangling" and points to the new file. Hard links do not work in this way. If you delete a hard link and then use normal tools (rather than ln) to create a file with the same name, then the new file is not linked to the old file. Consider the following way to compare hard links and symbolic links, to understand how they work: A hard link points a name to data on a storage device. A symbolic link points a name to another name, which points to data on a storage device. A symbolic link can point to a directory. The symbolic link then acts like the directory. If you use cd to change to the symbolic link, then the current working directory becomes the linked directory. Some tools might track that you followed a symbolic link to get there. For example, by default, cd updates your current working directory by using the name of the symbolic link rather than the name of the actual directory. If you want to update the current working directory by using the name of the actual directory, then you can use the -P option. The following example creates a symbolic link called /home/user/configfiles that points to the /etc directory. [user@host ~]$ ln -s /etc /home/user/configfiles [user@host ~]$ cd /home/user/configfiles [user@host configfiles]$ pwd /home/user/configfiles [user@host configfiles]$ cd -P /home/user/configfiles [user@host etc]$ pwd /etc REFERENCES Match File Names with Shell Expansions Objectives Efficiently run commands that affect many files by using pattern matching features of the Bash shell. Command-line Expansions When you type a command at the Bash shell prompt, the shell processes that command line through multiple expansions before running it. You can use these shell expansions to perform complex tasks that would otherwise be difficult or impossible. Following are the main expansions that Bash shell performs: Brace expansion, which can generate multiple strings of characters Tilde expansion, which expand to a path to a user home directory Variable expansion, which replaces text with the value that is stored in a shell variable Command substitution, which replaces text with the output of a command Pathname expansion, which helps to select one or more files by pattern matching Pathname expansion, historically called globbing, is one of the most useful features of Bash. With this feature, it is easier to manage many files. By using metacharacters that "expand" to match the file and path names that you are looking for, commands can act on a focused set of files at once. Pathname Expansion and Pattern Matching Pathname expansion expands a pattern of special characters that represent wild cards or classes of characters into a list of file names that match the pattern. Before the shell runs your command, it replaces the pattern with the list of file names that matched. If the pattern does not match anything, then the shell tries to use the pattern as a literal argument for the command that it runs. The following table lists common metacharacters and pattern classes that are used for pattern matching. Table 3.2. Table of Metacharacters and Matches Pattern Matches * 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 that is not a space or alphanumeric [[:digit:]] Any single digit from 0 to 9 [[:space:]] Any single white space character, which might include tabs, newlines, carriage returns, form feeds, or spaces For the next example, imagine that you ran the following commands to create some sample files: [user@host ~]$ mkdir glob; cd glob [user@host glob]$ touch alfa bravo charlie delta echo able baker cast dog easy [user@host glob]$ ls able alfa baker bravo cast charlie delta dog easy echo [user@host glob]$ In the next example, the first two commands use simple pattern matches with the asterisk (*) to match all the file names that start with "a" and all the file names that contain an "a", respectively. The third command uses the asterisk and square brackets to match all the file names that start with "a" or "c". [user@host glob]$ ls a* able alfa [user@host glob]$ ls *a* able alfa baker bravo cast charlie delta easy [user@host glob]$ ls [ac]* able alfa cast charlie The next example also uses question mark (?) characters to match some of those file names. The two commands match only file names with four and five characters in length, respectively. [user@host glob]$ ls ???? able cast easy echo [user@host glob]$ ls ????? baker bravo delta 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 that precedes or follows the brace definition. Brace expansions might be nested, one inside another. You can also use double-dot syntax (..), which expands to a sequence. For example, the {m..p} double-dot syntax inside braces expands to m n o p. [user@host glob]$ echo {Sunday,Monday,Tuesday,Wednesday}.log Sunday.log Monday.log Tuesday.log Wednesday.log [user@host glob]$ echo file{1..3}.txt file1.txt file2.txt file3.txt [user@host glob]$ echo file{a..c}.txt filea.txt fileb.txt filec.txt [user@host glob]$ echo file{a,b}{1,2}.txt filea1.txt filea2.txt fileb1.txt fileb2.txt [user@host glob]$ echo file{a{1,2},b,c}.txt filea1.txt filea2.txt fileb.txt filec.txt A practical use of brace expansion is to create multiple files or directories. [user@host glob]$ mkdir../RHEL{7,8,9} [user@host glob]$ ls../RHEL* RHEL7 RHEL8 RHEL9 Tilde Expansion The tilde character (~), matches the current user's home directory. If it starts with a string of characters other than a slash (/), then the shell interprets the string up to that slash as a username, if one matches, and replaces the string with the absolute path to that user's home directory. If no username matches, then the shell uses an actual tilde followed by the string of characters. In the following example, the echo command displays the value of the tilde (~). [user@host glob]$ echo ~root /root [user@host glob]$ echo ~user /home/user [user@host glob]$ echo ~/glob /home/user/glob [user@host glob]$ echo ~nonexistinguser ~nonexistinguser Variable Expansion A variable acts like a named container that stores a value in memory. Variables simplify accessing and modifying the stored data either from the command line or within a shell script. You can assign data as a value to a variable with the following syntax: [user@host ~]$ VARIABLENAME=value You can use variable expansion to convert the variable name to its value on the command line. If a string starts with a dollar sign ($), then the shell tries to use the rest of that string as a variable name and to replace it with the variable value. [user@host ~]$ USERNAME=operator [user@host ~]$ echo $USERNAME operator To prevent mistakes due to other shell expansions, you can put the name of the variable in curly braces, for example ${VARIABLENAME}. [user@host ~]$ USERNAME=operator [user@host ~]$ echo ${USERNAME} operator Variable names can contain only letters (uppercase and lowercase), numbers, and underscores. Variable names are case-sensitive and cannot start with a number. Command Substitution Command substitution enables the output of a command to replace the command itself on the command line. Command substitution occurs when you enclose a command in parentheses and precede it by a dollar sign ($). The $(command) form can nest multiple command expansions inside each other. [user@host glob]$ echo Today is $(date +%A). Today is Wednesday. [user@host glob]$ echo The time is $(date +%M) minutes past $(date +%l%p). The time is 26 minutes past 11AM. NOTE An earlier form of command substitution uses backticks: `command`. Although the Bash shell still accepts this format, try to avoid it because it is easy to visually confuse backticks with single quotation marks, and backticks cannot be nested. Protecting Arguments from Expansion Many characters have a special meaning in the Bash shell. To prevent 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 protects the following character from expansion. [user@host glob]$ echo The value of $HOME is your home directory. The value of /home/user is your home directory. [user@host glob]$ echo The value of \$HOME is your home directory. The value of $HOME is your home directory. In the preceding example, with the dollar sign protected from expansion, Bash treats it as a regular character, without variable expansion on $HOME. To protect longer character strings, you can use single quotation marks (') or double quotation marks (") to enclose strings. They have slightly different effects. Single quotation marks stop all shell expansion. Double quotation marks stop most shell expansion. Double quotation marks suppress special characters other than the dollar sign ($), backslash (\), backtick (`), and exclamation point (!) from operating inside the quoted text. Double quotation marks block pathname expansion, but still allow command substitution and variable expansion to occur. [user@host glob]$ myhost=$(hostname -s); echo $myhost host [user@host glob]$ echo "***** hostname is ${myhost} *****" ***** hostname is host ***** Use single quotation marks to interpret all text between the quotes literally. [user@host glob]$ echo "Will variable $myhost evaluate to $(hostname -s)?" Will variable host evaluate to host? [user@host glob]$ echo 'Will variable $myhost evaluate to $(hostname -s)?' Will variable $myhost evaluate to $(hostname -s)? IMPORTANT It is easy to confuse the single quotation mark ( ' ) and the command substitution backtick ( ` ), on both the screen and the keyboard. The use of one when you mean to use the other leads to unexpected shell behavior. Summary Files on a Linux system are organized into a single inverted tree of directories, a file-system hierarchy. Absolute paths start with a forward slash character (/) and specify the location of a file in the file-system hierarchy. Relative paths do not start with a forward slash character. Relative paths specify a file location in relation to the current working directory. You can use commands in combination with the dot (.), double dot (..), and tilde (~) special characters to refer to a file location in the file system. The mkdir, rmdir, cp, mv, and rm commands are key commands to manage files in Linux. Hard links and soft links are different ways for multiple file names to point to the same data. The Bash shell provides pattern matching, expansion, and substitution features to help you to run commands efficiently.

Use Quizgecko on...
Browser
Browser