DEV Community

NabilahSaputri555
NabilahSaputri555

Posted on

Managing Files From the Command Line

The File-system Hierarchy

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.

Subdirectories of / are used for standardized purposes to organize files by type and purpose. This makes it easier to find files.
Important Red Hat Enterprise Linux Directories

Location - Purpose

/usr - 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 - Configuration files specific to this system.
/var - Variable data specific to this system that should persist between boots files that dynamically change.
/run - Runtime data for processes started since the last boot. 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 - Home directories are where regular users store their personal data and configuration files.
/root - Home directory for the administrative superuser, root.
/tmp - 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.
/boot - Files needed in order to start the boot process.
/dev - 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 kotraverses one or more named subdirectories, delimited by a forward slash (/), until the destination is reached.

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

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

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

[user@host ~]$ pwd
/home/user
[user@host ~]$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[user@host ~]$
Enter fullscreen mode Exit fullscreen mode

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.

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 ~]$
Enter fullscreen mode Exit fullscreen mode

The default shell prompt also displays the last component of the absolute path to the current working 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.

[user@host ~]$ touch Videos/blockbuster1.ogg
[user@host ~]$ touch Videos/blockbuster2.ogg
[user@host ~]$ touch Documents/thesis_chapter1.odf
[user@host ~]$ touch Documents/thesis_chapter2.odf
[user@host ~]$
Enter fullscreen mode Exit fullscreen mode

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

[user@host ~]$ ls -l
total 15
drwxr-xr-x.  2 user user 4096 Feb  7 14:02 Desktop
drwxr-xr-x.  2 user user 4096 Jan  9 15:00 Documents
drwxr-xr-x.  3 user user 4096 Jan  9 15:00 Downloads
drwxr-xr-x.  2 user user 4096 Jan  9 15:00 Music
drwxr-xr-x.  2 user user 4096 Jan  9 15:00 Pictures
drwxr-xr-x.  2 user user 4096 Jan  9 15:00 Public
drwxr-xr-x.  2 user user 4096 Jan  9 15:00 Templates
drwxr-xr-x.  2 user user 4096 Jan  9 15:00 Videos
Enter fullscreen mode Exit fullscreen mode
[user@host ~]$ ls -la
total 15
drwx------. 16 user user   4096 Feb  8 16:15 .
drwxr-xr-x.  6 root root   4096 Feb  8 16:13 ..
-rw-------.  1 user user  22664 Feb  8 00:37 .bash_history
-rw-r--r--.  1 user user     18 Jul  9  2013 .bash_logout
-rw-r--r--.  1 user user    176 Jul  9  2013 .bash_profile
-rw-r--r--.  1 user user    124 Jul  9  2013 .bashrc
drwxr-xr-x.  4 user user   4096 Jan 20 14:02 .cache
drwxr-xr-x.  8 user user   4096 Feb  5 11:45 .config
drwxr-xr-x.  2 user user   4096 Feb  7 14:02 Desktop
drwxr-xr-x.  2 user user   4096 Jan  9 15:00 Documents
drwxr-xr-x.  3 user user   4096 Jan 25 20:48 Downloads
drwxr-xr-x. 11 user user   4096 Feb  6 13:07 .gnome2
drwx------.  2 user user   4096 Jan 20 14:02 
Enter fullscreen mode Exit fullscreen mode
.gnome2_private
-rw-------.  1 user user  15190 Feb  8 09:49 .ICEauthority
drwxr-xr-x.  3 user user   4096 Jan  9 15:00 .local
drwxr-xr-x.  2 user user   4096 Jan  9 15:00 Music
drwxr-xr-x.  2 user user   4096 Jan  9 15:00 Pictures
drwxr-xr-x.  2 user user   4096 Jan  9 15:00 Public
drwxr-xr-x.  2 user user   4096 Jan  9 15:00 Templates
drwxr-xr-x.  2 user user   4096 Jan  9 15:00 Videos
[user@host ~]$

Enter fullscreen mode Exit fullscreen mode

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.

[user@host ~]$ ls -R
.:
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

./Desktop:

./Documents:
thesis_chapter1.odf  thesis_chapter2.odf

Enter fullscreen mode Exit fullscreen mode
./Downloads:

./Music:

./Pictures:

./Public:

./Templates:

./Videos:
blockbuster1.ogg  blockbuster2.ogg
[user@host ~]$
Enter fullscreen mode Exit fullscreen mode

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 following example illustrates this behavior, alternating 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
Enter fullscreen mode Exit fullscreen mode
[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 ~]$
Enter fullscreen mode Exit fullscreen mode

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.

[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
Enter fullscreen mode Exit fullscreen mode
[user@host home]$ cd ..
[user@host /]$ pwd
/
[user@host /]$ cd
[user@host ~]$ pwd
/home/user
[user@host ~]$
Enter fullscreen mode Exit fullscreen mode

~ Command-line File Management
To manage files, you need to be able to create, remove, copy, and move them. You also need to organize them logically into directories, which you also need to be able to create, remove, copy, and move.
Common file management commands
Activity -> Command Syntax :
• Create a directory -> mkdir directory
• Copy a file -> cp file new-file
• Copy a directory and its contents -> cp -rdirectory new-directory
• Move or rename a file or directory -> mv file new-file
• Remove a file -> rm file
• Remove a directory containing files -> rm -rdirectory
• Remove an empty directory -> is rmdirectory

~ 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.
In the following example, pretend that you are trying to create a directory in the Videos directory named Watched, but you accidentally left off 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
Enter fullscreen mode Exit fullscreen mode

The mkdir command failed because Videos was misspelled and the directory Video does not exist. If you had used the mkdir command with the -p option, the directory Video would be created, which was not what you had intended, and the subdirectory Watched would be created in that incorrect directory.
After correctly spelling the Videos parent directory, creating the Watched subdirectory will succeed.

[user@host ~]$ mkdir Videos/Watched
[user@host ~]$ ls -R Videos
Videos/:
blockbuster1.ogg  blockbuster2.ogg  Watched

Videos/Watched:
Enter fullscreen mode Exit fullscreen mode

In the above 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
[user@host Documents]$ ls
ProjectX  ProjectY
Enter fullscreen mode Exit fullscreen mode

Use the mkdir -p command and space-delimited relative paths for each of the subdirectory names to create multiple parent directories with subdirectories.

[user@host Documents]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3
Enter fullscreen mode Exit fullscreen mode
[user@host Documents]$ cd
[user@host ~]$ ls -R Videos Documents
Documents:
ProjectX  ProjectY  Thesis

Documents/ProjectX:

Documents/ProjectY:
Enter fullscreen mode Exit fullscreen mode
Documents/Thesis:
Chapter1  Chapter2  Chapter3

Documents/Thesis/Chapter1:

Documents/Thesis/Chapter2:

Documents/Thesis/Chapter3:

Videos:
blockbuster1.ogg  blockbuster2.ogg  Watched

Videos/Watched:
Enter fullscreen mode Exit fullscreen mode

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.

[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
drwxrwxr-x. 2 user user 4096 Feb  8 16:05 Watched
Enter fullscreen mode Exit fullscreen mode
[user@host Videos]$
Enter fullscreen mode Exit fullscreen mode

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.
Use the copy command with the -r (recursive) option, to copy the directory and its contents to the directory.

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

[user@host Videos]$ cd ../Documents
[user@host Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter1.odf
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter2.odf
[user@host Documents]$ mv thesis_chapter2.odf thesis_chapter2_reviewed.odf
Enter fullscreen mode Exit fullscreen mode
[user@host Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter1.odf
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter2_reviewed.odf

Enter fullscreen mode Exit fullscreen mode

Use the mv command to move a file to a different directory.

[user@host Documents]$ ls Thesis/Chapter1
[user@host Documents]$
[user@host Documents]$ mv thesis_chapter1.odf Thesis/Chapter1
Enter fullscreen mode Exit fullscreen mode
[user@host Documents]$ ls Thesis/Chapter1
thesis_chapter1.odf
[user@host Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb  6 21:16 thesis_chapter2_reviewed.odf
Removing Files and Directories
Enter fullscreen mode Exit fullscreen mode

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.

[user@host Documents]$ pwd
/home/student/Documents
Use the rm command to remove a single file from your working directory.
[user@host Documents]$ rm thesis_chapter2_reviewed.odf
[user@host Documents]$ ls -l thesis*
ls: cannot access 'thesis*': No such file or directory
Enter fullscreen mode Exit fullscreen mode

If you attempt to use the rm command to remove a directory without using the -r option, the command will fail. Use the rm -r command to remove a subdirectory and its contents.

[user@host Documents]$ ls -R Thesis
Thesis/:
Chapter1  Chapter2  Chapter3

Thesis/Chapter1:
thesis_chapter1.odf

Thesis/Chapter2:
thesis_chapter2.odf

Thesis/Chapter3:
Enter fullscreen mode Exit fullscreen mode
[user@host Documents]$ rm -r Thesis/Chapter1
[user@host Documents]$ ls -l Thesis
total 8
drwxrwxr-x. 2 user user 4096 Feb 11 12:47 Chapter2
drwxrwxr-x. 2 user user 4096 Feb 11 12:48 Chapter3
Enter fullscreen mode Exit fullscreen mode

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.

[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.odf'? y
Enter fullscreen mode Exit fullscreen mode
rm: remove directory `Thesis/Chapter2'? y
rm: remove directory `Thesis/Chapter3'? y
rm: remove directory `Thesis'? y
[user@host Documents]$

Enter fullscreen mode Exit fullscreen mode

In the following example, the rmdir command only removes the directory that is empty. Just like the earlier example, you must use the rm -r command to remove a directory that contains content.

[user@host Documents]$ pwd
/home/student/Documents
[user@host Documents]$ rmdir ProjectY
[user@host Documents]$ rmdir ProjectX
rmdir: failed to remove `ProjectX': Directory not empty
[user@host Documents]$ rm -r ProjectX
[user@host Documents]$ ls -lR
.:
total 0
[user@host Documents]$
Enter fullscreen mode Exit fullscreen mode

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

[user@host ~]$ pwd
/home/user
[user@host ~]$ ls -l newfile.txt
-rw-r--r--. 1 user user 0 Mar 11 19:19 newfile.txt
Enter fullscreen mode Exit fullscreen mode

In the preceding example, the link count of newfile.txt is 1. It has exactly one absolute path, which is /home/user/newfile.txt.

You can use the ln command to create a new hard link (another 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.

[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
Enter fullscreen mode Exit fullscreen mode

If you want to find out whether two files are hard links of each other, one way is to use the -i option with the ls command to list the files' inode number.

[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

Enter fullscreen mode Exit fullscreen mode

Even if the original file gets deleted, the contents of the file are still available as long as at least one hard link exists. Data is only deleted from storage when the last hard link is deleted.

[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
Enter fullscreen mode Exit fullscreen mode

~ 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.
You can use the df command to list the directories that are on different file systems.
Files in two different "Mounted on" directories and their subdirectories are on different file systems. So, the system in this example, you can create a hard link between /var/tmp/link1 and /home/user/file because they are both subdirectories of / but not any other directory on the list. But you cannot create a hard link between /boot/test/badlink and /home/user/file because the first file is in a subdirectory of /boot (on the "Mounted on" list) and the second file is not.

~ 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.
The ln -s command is used to create a new soft link for the existing file /home/user/newfile-link2.txt that will be named /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/newfile-link2.txt
Enter fullscreen mode Exit fullscreen mode
[user@host ~]$ cat /tmp/newfile-symlink.txt
Soft Hello World

Enter fullscreen mode Exit fullscreen mode

The first character of the long listing for /tmp/newfile-symlink.txt is l instead of -. This indicates that the file is a soft link and not a regular file. (A d would indicate that the file is a directory.)
When the original regular file gets deleted, the soft link will still point to the file but the target is gone. A soft link pointing to a missing file is called a "dangling soft 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/newfile-link2.txt
[user@host ~]$ cat /tmp/newfile-symlink.txt
cat: /tmp/newfile-symlink.txt: No such file or directory
Enter fullscreen mode Exit fullscreen mode

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.(There is an option, -P, that will update it with the name of the actual directory instead.)
A soft link named /home/user/configfiles is created 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
Enter fullscreen mode Exit fullscreen mode

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

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

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 characternotin the enclosed class. [[:alpha:]] -> Any alphabetic character. [[:lower:]] -> Any lowercase character. [[:upper:]] -> Any uppercase character. [[:alnum:]] -> Any alphabetic character or digit. [[:point:]]-> 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. Pretend that you have run 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]$ 
Enter fullscreen mode Exit fullscreen mode

The first example will use simple pattern matches with the asterisk (*) and question mark (?) characters, and a class of characters, to match some of those file names.

[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
[user@host glob]$ ls ????
able  alfa  cast  easy  echo
[user@host glob]$ ls ?????
baker  bravo  delta
[user@host glob]$ 
Enter fullscreen mode Exit fullscreen mode

~ Tilde Expansion
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. The echo command can also be used to display the values of brace and variable expansion characters, and others.

[user@host glob]$ echo ~root
/root
[user@host glob]$ echo ~user
/home/user
[user@host glob]$ echo ~/glob
/home/user/glob
[user@host glob]$ 
Enter fullscreen mode Exit fullscreen mode

~ Brace Expansion
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.

[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
Enter fullscreen mode Exit fullscreen mode
[user@host glob]$ echo file{a{1,2},b,c}.txt
filea1.txt filea2.txt fileb.txt filec.txt
[user@host glob]$ 
Enter fullscreen mode Exit fullscreen mode

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*
RHEL6 RHEL7 RHEL8
[user@host glob]$
Enter fullscreen mode Exit fullscreen mode

~ Variable Expansion
A variable acts like a named container that can store a value in memory
You can assign data as a value to a variable using the following syntax:

[user@host ~]$ VARIABLENAME=value

Enter fullscreen mode Exit fullscreen mode

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 will try to use the rest of that string as a variable name and replace it with whatever value the variable has.

[user@host ~]$ USERNAME=operator
[user@host ~]$ echo $USERNAME
operator
Enter fullscreen mode Exit fullscreen mode

To help avoid 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

Enter fullscreen mode Exit fullscreen mode

Shell variables and ways to use them will be covered in more depth later in this course.

~ Command Substitution
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).
Today is Wednesday.
[user@host glob]$ echo The time is $(date +%M) minutes past $(date +%l%p).
The time is 26 minutes past 11AM.
[user@host glob]$ 
Enter fullscreen mode Exit fullscreen mode

~Protecting Arguments from Expansion
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.

[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.
[user@host glob]$ 
Enter fullscreen mode Exit fullscreen mode

protecting the dollar sign from expansion caused Bash to treat it as a regular character and it did not perform variable expansion on $HOME.
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.

[user@host glob]$ myhost=$(hostname -s); echo $myhost
host
[user@host glob]$ echo "***** hostname is ${myhost} *****"
***** hostname is host *****
[user@host glob]$ 
Enter fullscreen mode Exit fullscreen mode

Use single quotation marks to interpret all text 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)?
[user@host glob]$ 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)