Introduction to Shells
A shell is a command-line interpreter that provides a user interface for accessing the services of the operating system. It allows users to execute commands, run programs, and manage files. There are several types of shells available in Unix-like systems, each with its own features and capabilities.
Common Shells
- Bourne Shell (sh): The original Unix shell, known for its simplicity and scripting capabilities.
- Bourne Again Shell (bash): An improved version of the Bourne shell, offering more features and better scripting capabilities. It is the default shell in many Linux distributions, including RHEL.
- C Shell (csh): A shell with C-like syntax, useful for scripting and interactive use.
- Korn Shell (ksh): A powerful shell that incorporates features from both the Bourne and C shells.
- Z Shell (zsh): An extended version of the Bourne shell, offering many enhancements and interactive features.
Basic Shell Commands
Understanding basic shell commands is crucial for navigating and managing a Unix-like system.
ls: List directory contents.cd: Change the current directory.pwd: Print the current working directory.cp: Copy files and directories.mv: Move or rename files and directories.rm: Remove files and directories.mkdir: Create directories.rmdir: Remove empty directories.touch: Create empty files or update the timestamp of existing files.echo: Display a line of text.cat: Concatenate and display file content.more/less: View file content one page at a time.head/tail: Display the beginning or end of a file.
Editing Files with vim
vim (Vi IMproved) is a powerful and highly configurable text editor, an improved version of the classic Unix text editor vi. It’s widely used for editing files on Unix-like systems due to its efficiency and robustness.
Starting vim
To start vim, type vim followed by the name of the file you want to edit.
vim filename
If the file does not exist, vim will create it.
Basic vim Modes
vim operates in several modes, each serving a different purpose:
- Normal Mode: This is the default mode. In this mode, you can navigate through the text and perform various commands.
- Insert Mode: This mode is used for editing text. You can enter Insert Mode from Normal Mode by pressing
i. - Visual Mode: This mode is used for selecting text. Enter Visual Mode from Normal Mode by pressing
v. - Command-Line Mode: This mode is used for executing commands. Enter Command-Line Mode from Normal Mode by pressing
:.
Basic Navigation
- Move the cursor: Use
h,j,k, andlto move the cursor left, down, up, and right, respectively. - Move to the beginning of the line: Press
0. - Move to the end of the line: Press
$. - Move to the beginning of the file: Press
gg. - Move to the end of the file: Press
G.
Editing Text
- Enter Insert Mode: Press
i. - Exit Insert Mode: Press
Esc. - Delete a character: Press
xin Normal Mode. - Undo the last change: Press
u. - Redo the last undone change: Press
Ctrl-r.
Saving and Exiting
- Save changes: Type
:win Command-Line Mode. - Save changes and exit: Type
:wqin Command-Line Mode. - Exit without saving: Type
:q!in Command-Line Mode.
Searching and Replacing
- Search for a pattern: Type
/patternand pressEnterin Normal Mode. - Move to the next match: Press
n. - Move to the previous match: Press
N. - Search and replace: Type
:s/old/new/gin Command-Line Mode to replace all occurrences ofoldwithnewin the current line. Use:%s/old/new/gto replace in the entire file.
Understanding the Shell Environment
The shell environment in Unix-like systems is a powerful interface for interacting with the operating system. Understanding the shell environment is crucial for efficient system administration.
Environment Variables
Environment variables are dynamic values that affect the behavior of processes on a system.
- Common Environment Variables:
PATH: Specifies the directories where executable files are located.HOME: Specifies the current user’s home directory.USER: Specifies the current user’s username.SHELL: Specifies the current user’s shell.
- Viewing Environment Variables: Use the
echocommand to view the value of an environment variable.
echo $PATH
- Setting Environment Variables: Use the
exportcommand to set a new environment variable or change the value of an existing one.
export VARIABLE_NAME=value
Shell Configuration Files
Shell configuration files customize the shell environment and can be used to set environment variables, aliases, and functions.
- Common Shell Configuration Files:
~/.bashrc: Executed for interactive non-login shells.~/.bash_profile: Executed for login shells.~/.profile: Another configuration file executed for login shells.
Aliases
Aliases are shortcuts for longer commands.
- Creating an Alias: Use the
aliascommand to create a new alias.
alias ll=’ls -la’
- Viewing Aliases: Use the
aliascommand without arguments to list all current aliases.
alias
- Removing an Alias: Use the
unaliascommand to remove an alias.
unalias ll
Finding Help
Finding help and documentation is crucial for learning and troubleshooting in Unix-like systems.
Using the man Command
The man (manual) command displays the user manual for commands and other aspects of the system.
- View a Manual Page: Type
manfollowed by the command name.
man ls
- Navigate the Manual Page: Use the arrow keys or
Page UpandPage Downto scroll. Pressqto quit.
Using the info Command
The info command provides more detailed documentation than man pages.
- View an Info Page: Type
infofollowed by the command name.
info ls
- Navigate the Info Page: Use the arrow keys to scroll. Press
qto quit.
Using the --help Option
Most commands have a --help option that displays a brief summary of how to use the command.
- View Help for a Command: Type the command followed by
--help.
ls –help
Online Resources
There are numerous online resources for Unix-like systems documentation and community support.
- Official Documentation: Refer to the official documentation for your specific Unix-like system.
- Community Forums: Participate in community forums and mailing lists for help and discussions.
- Online Tutorials: Many websites offer tutorials and guides for Unix-like systems.
Conclusion
Mastering basic shell skills, including editing files with vim, understanding the shell environment, and finding help, is crucial for effective system administration in Unix-like systems. This guide provides a comprehensive overview of these essential skills, offering the necessary knowledge and practical examples to enhance your proficiency in managing RHEL systems.
Exercise
Authenticating on the Server
- Action: Authenticate on the server1 server that you created.
- Description: Login to the server as the user created during installation.
- Purpose: Gain access to the server to perform the exercises.
Opening the Terminal
- Action: Click Activities, type term, and click the terminal icon.
- Description: Open a terminal in the graphical user interface.
- Purpose: Access the terminal for command-line exercises.
Executing the time ls Command
- Command: time ls
- Description: This command executes the
lscommand, listing the contents of the current directory. The Bash built-intimecommand shows information about the time it took to complete this command. - Purpose: To measure and display the execution time of the
lscommand.
- Description: This command executes the
Finding the Path of time
- Command: which time
- Description: This command shows the filename
/usr/bin/timethat was found in the$PATHvariable. - Purpose: To locate the executable file for the
timecommand.
- Description: This command shows the filename
Checking time as a Shell Keyword
- Command: time
- Description: This command shows that
timeis a shell keyword. - Purpose: To differentiate between the internal shell keyword and the external command.
- Description: This command shows that
Displaying the $PATH Variable
- Command: echo $PATH
- Description: This command shows the contents of the
$PATHvariable. - Purpose: To verify the directories included in the search path for executable files.
- Description: This command shows the contents of the
Executing /usr/bin/time ls Command
- Command: /usr/bin/time ls
- Description: This command runs the
lscommand using the external/usr/bin/timecommand. - Purpose: To show the difference between the internal shell keyword
timeand the externaltimecommand.
- Description: This command runs the
Changing to Home Directory
- Command: cd
- Description: This command changes the current directory to the user’s home directory.
- Purpose: To ensure the user is in their home directory before starting the exercise.
Printing the Working Directory
- Command: pwd
- Description: This command prints the current working directory.
- Purpose: To verify the current directory.
Listing Directory Contents
- Command: ls
- Description: This command lists the contents of the current directory.
- Purpose: To display files and directories.
Redirecting STDOUT to Null Device
- Command: ls > /dev/null
- Description: This command redirects standard output to the null device, effectively discarding it.
- Purpose: To suppress the command output.
Executing a Non-Existent Command
- Command: ls ilwehgi > /dev/null
- Description: This command attempts to list a non-existent file, showing an error message on the screen.
- Purpose: To demonstrate error handling.
Redirecting STDERR to Null Device
- Command: ls ilwehgi 2> /dev/null
- Description: This command redirects standard error to the null device, suppressing error messages.
- Purpose: To suppress error messages.
Listing /etc Contents while Hiding Errors
- Command: ls ilwehgi /etc 2> /dev/null
- Description: This command lists the contents of the
/etcdirectory while hiding error messages. - Purpose: To show valid and invalid output redirection.
- Description: This command lists the contents of the
Redirecting STDOUT and STDERR Separately
- Command: ls ilwehgi /etc 2> /dev/null > output
- Description: This command redirects standard output to a file named
outputwhile suppressing error messages. - Purpose: To save valid output to a file.
- Description: This command redirects standard output to a file named
Displaying File Contents
- Command: cat output
- Description: This command displays the contents of the file
output. - Purpose: To verify the contents of the output file.
- Description: This command displays the contents of the file
Overwriting File Contents
- Command: echo hello > output
- Description: This command overwrites the
outputfile with the text “hello”. - Purpose: To demonstrate file content manipulation.
- Description: This command overwrites the
Appending to a File
- Command: ls >> output
- Description: This command appends the directory listing to the
outputfile. - Purpose: To add content to an existing file without overwriting it.
- Description: This command appends the directory listing to the
Recursively Listing All Files and Directories
- Command: ls -R /
- Description: This command recursively lists all files and directories starting from the root directory.
- Purpose: To show all files and directories, which may produce a lengthy output.
Paginating Output with less
- Command: ls -R / | less
- Description: This command pipes the recursive directory listing into
less, allowing for paginated viewing. - Purpose: To improve the readability of lengthy output.
- Description: This command pipes the recursive directory listing into
Quitting less
- Command: q
- Description: This command quits the
lesspager. - Purpose: To exit the pager and end the command.
- Description: This command quits the
Attempting to Write to a Terminal Device File
- Command: ls > /dev/tty1
- Description: This command attempts to write the directory listing to the terminal device file
/dev/tty1. - Purpose: To demonstrate permission restrictions for device files.
- Description: This command attempts to write the directory listing to the terminal device file
Displaying Command History
- Command: history
- Description: This command displays the history of commands used in the current session.
- Purpose: To review previously executed commands.
Reverse Searching Command History
- Command: Ctrl-r
- Description: This command initiates a reverse search in the command history.
- Purpose: To quickly find and execute a previously used command.
Filtering Command History
- Command: history | grep cat
- Description: This command pipes the history output through
grepto search for commands containing the text “cat”. - Purpose: To filter history by specific commands.
- Description: This command pipes the history output through
Repeating a Command from History
- Command: !nn
- Description: This command executes the command with the history number
nn. - Purpose: To repeat a specific command from history.
- Description: This command executes the command with the history number
Clearing Command History
- Command: history -c
- Description: This command clears the in-memory command history.
- Purpose: To erase the current session’s history.
Attempting Tab Completion
- Command: gd then Tab and Tab again
- Description: This command attempts to use tab completion to find commands starting with “gd”.
- Purpose: To demonstrate Bash’s tab completion features.
Completing a Command with More Context
- Command: gdi then Tab
- Description: This command completes the command using more specific input to “gdi”.
- Purpose: To show accurate tab completion with more context.
Changing to /etc Directory
- Command: cd /etc
- Description: This command changes the current directory to
/etc. - Purpose: To navigate to a specific directory.
- Description: This command changes the current directory to
Completing a File Name
- Command: cat ps then Tab
- Description: This command completes the filename to
passwdwhen there is only one match. - Purpose: To demonstrate file name auto-completion.
- Description: This command completes the filename to
Editing a File with Vim
- Command: vim ~/testfile
- Description: This command opens Vim and creates/edits a file named
testfilein the home directory. - Purpose: To practice using the Vim editor.
- Description: This command opens Vim and creates/edits a file named
Entering Input Mode in Vim
- Command: i
- Description: This command switches Vim to input mode for typing text.
- Purpose: To start entering text in Vim.
Exiting Input Mode in Vim
- Command: Esc
- Description: This command exits input mode and returns Vim to command mode.
- Purpose: To switch from text entry to command execution.
Writing Changes in Vim
- Command:
- Description: This command writes the changes to the file.
- Purpose: To save the file in Vim.
Navigating to a Specific Line in Vim
- Command: :3
- Description: This command moves the cursor to line 3 in the file.
- Purpose: To navigate within the file.
Deleting a Line in Vim
- Command: dd
- Description: This command deletes the current line in Vim.
- Purpose: To remove text.
Undoing the Last Change in Vim
- Command: u
- Description: This command undoes the last change made in Vim.
- Purpose: To reverse a previous action.
Opening a New Line in Vim
- Command: o
- Description: This command opens a new line below the current line.
- Purpose: To insert new lines for additional text.
Substituting Text in Vim
- Command: :%s/ox/OX/g
- Description: This command substitutes all instances of “ox” with “OX” in the file.
- Purpose: To perform text substitution.
Writing and Quitting Vim
- Command:
- Description: This command writes changes and quits Vim.
- Purpose: To save and exit Vim.
Displaying the LANG Variable
- Command: echo $LANG
- Description: This command shows the current language setting.
- Purpose: To verify language and locale settings.
Viewing Command Help
- Command: ls –help
- Description: This command displays help information for the
lscommand. - Purpose: To learn about command options.
- Description: This command displays help information for the
Temporarily Changing the Language
- Command: LANG=es_ES.UTF-8
- Description: This command temporarily changes the language setting to Spanish.
- Purpose: To demonstrate language setting changes.
Editing .bashrc
- Command: vim .bashrc
- Description: This command opens the
.bashrcfile in Vim for editing. - Purpose: To modify user-specific shell settings.
- Description: This command opens the
Adding an Environment Variable to .bashrc
- Command: COLOR=red
- Description: This command adds a custom environment variable
COLORset to “red” in.bashrc. - Purpose: To demonstrate adding a variable to configuration.
- Description: This command adds a custom environment variable
Verifying an Environment Variable
- Command: echo $COLOR
- Description: This command displays the value of the
COLORvariable. - Purpose: To check that the variable is set correctly.
- Description: This command displays the value of the
Opening the man Manual Page
- Command: man man
- Description: This command opens the manual page for the
mancommand. - Purpose: To read documentation on using
man.
- Description: This command opens the manual page for the
Searching Within man Pages
- Command: /-k
- Description: This command searches within the
manpage for the-koption. - Purpose: To find specific options in the manual.
- Description: This command searches within the
Continuing the Search in man Pages
- Command: n
- Description: This command continues searching in the
manpage. - Purpose: To navigate through search results.
- Description: This command continues searching in the
Quitting man Pages
- Command: q
- Description: This command quits the
manpage. - Purpose: To exit the manual viewer.
- Description: This command quits the
Reading the apropos Manual Page
- Command: man apropos
- Description: This command opens the manual page for the
aproposcommand. - Purpose: To understand the
aproposcommand.
- Description: This command opens the manual page for the
Reading the mandb Manual Page
- Command: man mandb
- Description: This command opens the manual page for the
mandbcommand. - Purpose: To learn about updating the man database.
- Description: This command opens the manual page for the
Updating the Manual Page Database
- Command: sudo mandb
- Description: This command updates the manual page database.
- Purpose: To refresh the man page index as a superuser.
Reading the ls Manual Page
- Command: man ls
- Description: This command opens the manual page for the
lscommand. - Purpose: To get detailed information about
ls.
- Description: This command opens the manual page for the
Navigating to the End of the man Page
- Command: G
- Description: This command moves to the end of the
manpage. - Purpose: To quickly navigate to the end of the document.
- Description: This command moves to the end of the
Opening ls Info Page with pinfo
- Command: pinfo ‘(coreutils) ls invocation’
- Description: This command opens the
lsinfo page withpinfo. - Purpose: To read detailed documentation with better formatting.
- Description: This command opens the