Introduction
In this lab, you will gain essential skills for managing text files and customizing your shell environment in Linux. You will learn how to redirect standard output and error streams to files, combine different streams, and construct powerful command pipelines to automate tasks.
Furthermore, you will explore the basics of editing text files using Vim, a widely used and powerful text editor. Finally, you will learn to configure and utilize shell variables and aliases to personalize your command-line experience and enhance productivity.
Redirect Standard Output to Files
In this step, you will learn how to redirect the standard output of commands to files. This is a fundamental skill in Linux system administration, allowing you to capture command results for later analysis, logging, or further processing.
The shell uses special file descriptors to manage input and output. The most common ones are:
0: Standard Input (stdin) - Typically from the keyboard.1: Standard Output (stdout) - Typically to the terminal screen.2: Standard Error (stderr) - Typically to the terminal screen for error messages.
We will focus on redirecting stdout in this section.
Overwriting a File with >
The > operator redirects the standard output of a command to a specified file. If the file does not exist, it will be created. If the file already exists, its contents will be overwritten.
Let's start by creating a simple text file with the current date and time.
First, ensure you are in your home directory's
projectfolder.cd ~/project[labex@host project]$Now, use the
datecommand and redirect its output to a new file namedcurrent_datetime.txt.date > current_datetime.txtThis command will execute
date, but instead of printing the date to your terminal, it will save it intocurrent_datetime.txt.Verify the content of the file using the
catcommand.cat current_datetime.txtMon Day XX HH:MM:SS AM/PM TimeZone YYYYThe output will show the current date and time, similar to the example above.
Now, let's try redirecting the output of
echoto the same file. This will overwrite the previous content.echo "This is a new line of text." > current_datetime.txtCheck the file content again.
cat current_datetime.txtThis is a new line of text.As you can see, the original date and time were replaced by the new line of text.
Appending to a File with >>
The >> operator redirects the standard output of a command to a specified file, appending the new content to the end of the file. If the file does not exist, it will be created.
Let's append more content to our current_datetime.txt file.
Append another line of text to
current_datetime.txt.echo "This line is appended." >> current_datetime.txtView the file's content.
cat current_datetime.txtThis is a new line of text. This line is appended.Notice that the new line was added after the existing content.
Let's append the current date and time again.
date >> current_datetime.txtCheck the file content one more time.
cat current_datetime.txtThis is a new line of text. This line is appended. Mon Day XX HH:MM:SS AM/PM TimeZone YYYYThe date and time are now at the end of the file.
Redirecting Output of Other Commands
You can redirect the output of almost any command. Let's try redirecting the output of ls and wc.
Redirect the output of
ls -l(long listing format) to a file namedfile_list.txt.ls -l > file_list.txtInspect the content of
file_list.txt.cat file_list.txttotal 4 -rw-r--r-- 1 labex labex 80 Jun 4 07:04 current_datetime.txt -rw-r--r-- 1 labex labex 0 Jun 4 07:04 file_list.txtThis file now contains the detailed listing of files in your current directory. The exact file sizes and timestamps will vary based on when you run the commands.
Now, let's count the number of lines in
file_list.txtusingwc -land redirect that count to another file,line_count.txt.wc -l file_list.txt > line_count.txtView the content of
line_count.txt.cat line_count.txt3 file_list.txtThe output shows that
file_list.txthas 3 lines (including thetotalline and the two file entries).
This concludes the first part of redirecting standard output. You've learned how to create and overwrite files using > and append to them using >>.
Redirect Standard Error and Combine Streams
In this step, you will learn how to redirect standard error (stderr) and how to combine stdout and stderr into a single stream. This is crucial for managing error messages generated by commands, allowing you to log them or discard them as needed.
Recall that stderr is file descriptor 2. We use 2> to redirect error messages.
Redirecting Standard Error to a File
Sometimes, commands produce error messages that you want to capture separately from their standard output.
Ensure you are in your
~/projectdirectory.cd ~/project[labex@host project]$Let's try to list the contents of a non-existent directory. This will generate an error message.
ls non_existent_directoryls: cannot access 'non_existent_directory': No such file or directoryYou can see the error message printed directly to the terminal.
Now, let's redirect this error message to a file named
errors.log.ls non_existent_directory 2> errors.logThis time, you won't see the error message on your terminal.
Check the content of
errors.log.cat errors.logls: cannot access 'non_existent_directory': No such file or directoryThe error message is now stored in the file.
Discarding Standard Error
Often, you might want to run a command that produces noisy error messages that you don't care about. In such cases, you can redirect stderr to /dev/null. /dev/null is a special device file that discards all data written to it.
Try the
lscommand with the non-existent directory again, but this time, discard the error.ls non_existent_directory 2> /dev/nullYou will see no output on the terminal, and no error message is saved to a file.
Combining Standard Output and Standard Error
There are situations where you want to capture both stdout and stderr into the same file. This can be done in a few ways.
Method 1: > file 2>&1
This method redirects stdout to a file, and then redirects stderr to the same location as stdout. The order 2>&1 is important: it means "redirect file descriptor 2 (stderr) to the same place as file descriptor 1 (stdout)".
Let's create a command that produces both standard output and standard error. We'll use
findto search for a file in a directory where we have permissions and in a directory where we don't.find ~/project /root -name "current_datetime.txt" > combined_output.log 2>&1Here,
find ~/project -name "current_datetime.txt"will producestdout(if found), andfind /root -name "current_datetime.txt"will likely producestderrdue to permission issues.Examine the
combined_output.logfile.cat combined_output.log/home/labex/project/current_datetime.txt find: ‘/root’: Permission deniedYou can see both the successful output (the path to the file) and the error message are captured in the same file.
Method 2: &> file (Bash specific)
Bash provides a shorthand for combining stdout and stderr to a file: &>. This is equivalent to > file 2>&1.
Let's try the same
findcommand using the&>shorthand.find ~/project /root -name "file_list.txt" &> combined_output_shorthand.logCheck the content of
combined_output_shorthand.log.cat combined_output_shorthand.log/home/labex/project/file_list.txt find: ‘/root’: Permission deniedThe result is the same as the previous method, demonstrating the convenience of
&>.
Appending Combined Streams
Just like with stdout, you can append combined stdout and stderr to a file using >> file 2>&1 or &>> file.
Append more output and errors to
combined_output.log.find ~/project /root -name "line_count.txt" >> combined_output.log 2>&1View the updated
combined_output.log.cat combined_output.log/home/labex/project/current_datetime.txt find: ‘/root’: Permission denied /home/labex/project/line_count.txt find: ‘/root’: Permission deniedThe new output and errors are appended to the existing content.
You have now successfully learned how to redirect standard error and how to combine standard output and standard error into a single file. This knowledge is essential for robust scripting and system administration tasks.
Construct and Understand Command Pipelines
In this step, you will learn about command pipelines, a powerful feature in the Linux shell that allows you to chain multiple commands together. The output of one command becomes the input of the next, enabling complex data processing and manipulation.
The pipe operator | (vertical bar) is used to connect commands in a pipeline. It redirects the standard output (stdout) of the command on its left to the standard input (stdin) of the command on its right.
Basic Pipelines
Let's start with a simple example to understand how pipelines work.
Ensure you are in your
~/projectdirectory.cd ~/project[labex@host project]$First, let's list the files in the current directory.
lscombined_output.log combined_output_shorthand.log current_datetime.txt errors.log file_list.txt line_count.txtNow, let's pipe the output of
lsto thewc -lcommand, which counts the number of lines it receives.ls | wc -l6The
lscommand lists the files, and its output (each file name on a new line) is fed as input towc -l, which then counts these lines, effectively telling you how many files/directories are in the current location.Let's try another common use case: piping
ls -ltolessfor paginated output. This is useful when a command produces too much output to fit on a single screen.ls -l /usr/bin | lesstotal 200000 -rwxr-xr-x 1 root root 12345 Jan XX HH:MM [filename] ... (press 'q' to quit less) ...The
ls -l /usr/bincommand lists all files in/usr/binwith detailed information. This output is then sent toless, allowing you to scroll through it page by page. Pressqto exitless.
Filtering Output with grep
The grep command is often used in pipelines to filter lines that match a specific pattern.
Let's list all processes running on the system using
ps auxand then filter for processes related tobash.ps aux | grep bashlabex 1234 0.0 0.1 12345 6789 ? Ss HH:MM 0:00 /usr/bin/bash labex 5678 0.0 0.0 9876 5432 pts/0 S+ HH:MM 0:00 grep bashThe
ps auxcommand lists all running processes. Its output is piped togrep bash, which then displays only the lines containing the word "bash". You might see two lines: one for your current bash shell and one for thegrepcommand itself.To exclude the
grepcommand from the output, you can usegrep -v(invert match) or refine your pattern. Let's trygrep -v grep.ps aux | grep bash | grep -v greplabex 1234 0.0 0.1 12345 6789 ? Ss HH:MM 0:00 /usr/bin/bashNow, only the actual bash process is shown.
Using sort and uniq
sort is used to sort lines of text, and uniq is used to report or omit repeated lines. They are often used together.
Let's create a file with some unsorted, repeated words.
echo -e "apple\nbanana\napple\norange\nbanana" > fruits.txtView the content of
fruits.txt.cat fruits.txtapple banana apple orange bananaNow, let's sort the lines in
fruits.txt.cat fruits.txt | sortapple apple banana banana orangeTo get only the unique sorted words, pipe the output of
sorttouniq.cat fruits.txt | sort | uniqapple banana orangeThis pipeline first sorts the lines, then
uniqremoves duplicate adjacent lines.
The tee Command
The tee command is special in pipelines. It reads standard input, writes it to standard output, and simultaneously writes it to one or more files. It's like a "T" junction in a pipe, allowing data to flow in two directions.
Let's list the files and save the output to
ls_output.txtwhile also displaying it on the screen.ls -l | tee ls_output.txttotal 24 -rw-r--r-- 1 labex labex 123 Jan XX HH:MM combined_output.log -rw-r--r-- 1 labex labex 123 Jan XX HH:MM combined_output_shorthand.log -rw-r--r-- 1 labex labex 123 Jan XX HH:MM current_datetime.txt -rw-r--r-- 1 labex labex 123 Jan XX HH:MM errors.log -rw-r--r-- 1 labex labex 123 Jan XX HH:MM file_list.txt -rw-r--r-- 1 labex labex 123 Jan XX HH:MM fruits.txt -rw-r--r-- 1 labex labex 123 Jan XX HH:MM line_count.txt -rw-r--r-- 1 labex labex 0 Jan XX HH:MM ls_output.txtYou will see the
ls -loutput on your terminal, and a file namedls_output.txtwill be created with the same content.Verify the content of
ls_output.txt.cat ls_output.txttotal 24 -rw-r--r-- 1 labex labex 123 Jan XX HH:MM combined_output.log ... (same as above) ...You can also use
tee -ato append the output to a file.echo "--- End of list ---" | tee -a ls_output.txt--- End of list ---The line "--- End of list ---" is printed to the terminal and appended to
ls_output.txt.Check the updated
ls_output.txt.cat ls_output.txttotal 24 ... (previous ls -l output) ... --- End of list ---
Pipelines are incredibly versatile and form the backbone of many powerful shell scripts and one-liner commands. By combining simple commands, you can perform complex data transformations efficiently.
Edit Text Files with Vim Basics
In this step, you will learn the fundamental operations of Vim, a powerful and widely used text editor in the Linux environment. Vim operates in different modes, which can be a bit challenging for beginners, but mastering the basics will significantly boost your productivity.
Vim is a modal editor, meaning it has different modes for different tasks:
- Normal Mode (Command Mode): This is the default mode when you open Vim. In this mode, keystrokes are interpreted as commands (e.g., moving the cursor, deleting text, copying text).
- Insert Mode: In this mode, anything you type is inserted into the file. You enter Insert Mode from Normal Mode by pressing
i(insert at cursor),a(append after cursor),o(open new line below), etc. To return to Normal Mode, pressEsc. - Visual Mode: This mode allows you to select blocks of text for operations like copying, cutting, or deleting. You enter Visual Mode from Normal Mode by pressing
v(character-wise),Shift+V(line-wise), orCtrl+V(block-wise). PressEscto return to Normal Mode. - Command-Line Mode (Ex Mode): This mode is used for executing commands that typically start with a colon (
:), such as saving (:w), quitting (:q), or searching (/). You enter this mode from Normal Mode by pressing:.
Opening and Basic Navigation
Ensure you are in your
~/projectdirectory.cd ~/project[labex@host project]$Open a new file named
my_document.txtusingvim.vim my_document.txtYour terminal will now display the Vim interface. You are in Normal Mode.
In Normal Mode, you can navigate using the arrow keys or
h(left),j(down),k(up),l(right). Since the file is empty, there's not much to navigate yet.
Insert Mode: Adding Text
To start typing, you need to enter Insert Mode. Press
i(for insert). You should see-- INSERT --at the bottom left of your terminal, indicating you are in Insert Mode.Type the following lines:
This is the first line. This is the second line. This is the third line.To exit Insert Mode and return to Normal Mode, press the
Esckey. The-- INSERT --indicator should disappear.
Saving and Quitting
In Normal Mode, to save the file, type
:wand pressEnter.:wYou should see
my_document.txt[New]3L, 60B writtenat the bottom, confirming the save.To quit Vim, type
:qand pressEnter.:qYou will be returned to your shell prompt.
Verify the content of
my_document.txtusingcat.cat my_document.txtThis is the first line. This is the second line. This is the third line.
Editing Existing Files
Open
my_document.txtagain.vim my_document.txtIn Normal Mode, move your cursor to the beginning of the second line (using
jor arrow keys).Press
Shift+Vto enter Visual Line Mode. The entire second line will be highlighted.Press
yto "yank" (copy) the selected line.Move your cursor to the end of the third line (using
jor arrow keys).Press
pto "put" (paste) the yanked line below the current line. The second line will now appear again as the fourth line.Now, let's delete a line. Move your cursor to the fourth line (the one you just pasted).
Press
dd(doubled) to delete the entire line.To undo your last change, press
u. The deleted line will reappear.To save and quit in one command, type
:wqand pressEnter.:wqVerify the content of
my_document.txtagain.cat my_document.txtThis is the first line. This is the second line. This is the third line. This is the second line.The file should now have four lines, with the second line duplicated.
Discarding Changes
Sometimes you make changes and decide you don't want to save them.
Open
my_document.txtagain.vim my_document.txtEnter Insert Mode by pressing
i.Add a new line at the end:
This line should not be saved.Press
Escto return to Normal Mode.Try to quit using
:q.:qVim will warn you:
E37: No write since last change (add ! to override). This means you have unsaved changes.To quit without saving, type
:q!and pressEnter.:q!You will be returned to the shell prompt, and your changes will be discarded.
Verify the content of
my_document.txt.cat my_document.txtThis is the first line. This is the second line. This is the third line. This is the second line.The last line you added should not be present.
You have now covered the very basic operations in Vim: opening files, inserting text, navigating, saving, quitting, and discarding changes. These are the essential skills to get started with Vim.
Configure and Use Shell Variables and Aliases
In this step, you will learn how to configure and use shell variables and aliases. These are powerful features that allow you to customize your shell environment, store data, and create shortcuts for frequently used commands, significantly improving your command-line efficiency.
Shell Variables
Shell variables are named entities that store data. They can store numbers, text, or other data that can be used by the shell or by programs executed within the shell.
Ensure you are in your
~/projectdirectory.cd ~/project[labex@host project]$Setting a Local Variable: Let's create a simple variable named
MY_MESSAGE.MY_MESSAGE="Hello, LabEx!"Note that there are no spaces around the
=sign.Accessing a Variable: To access the value of a variable, you prepend its name with a
$sign.echo $MY_MESSAGEHello, LabEx!Variable Expansion with Braces: Sometimes, you need to clearly delimit the variable name, especially when it's followed by other characters. Use curly braces
{}for this.echo "The message is: ${MY_MESSAGE}."The message is: Hello, LabEx!.If you omit the braces, the shell might interpret
MY_MESSAGE.as the variable name, which doesn't exist.Listing All Set Variables: You can use the
setcommand to list all currently set shell variables and functions. This output can be very long, so it's often piped toless.set | lessBASH=/usr/bin/bash BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath ... (press 'q' to quit less) ...Press
qto exitless.Unsetting a Variable: To remove a variable, use the
unsetcommand.unset MY_MESSAGEVerify that the variable is no longer set.
echo $MY_MESSAGEYou should see an empty line, indicating the variable is unset.
Environment Variables
Environment variables are a special type of shell variable that are inherited by child processes. This means that any program or script launched from your current shell will have access to these variables. They are typically used to configure the environment for applications.
Setting an Environment Variable: Use the
exportcommand to make a variable an environment variable.export EDITOR=vimThis sets the
EDITORenvironment variable, which many programs use to determine your preferred text editor.Listing Environment Variables: Use the
envcommand to list only the environment variables.env | grep EDITOREDITOR=vimUnexporting a Variable: You can unexport a variable without unsetting it using
export -n. This makes it a local variable again.export -n EDITORVerify it's no longer an environment variable.
env | grep EDITORYou should see no output. However, it's still a local variable:
echo $EDITORvimTo completely remove it, use
unset.unset EDITOR
Shell Aliases
Aliases are shortcuts for commands. They allow you to define a new command that expands to a longer command or a sequence of commands. This is very useful for frequently used commands with many options.
Creating an Alias: Let's create an alias for
ls -lto make it shorter.alias ll='ls -l'Note the single quotes around the command to ensure it's treated as a single string.
Using an Alias: Now, you can simply type
llinstead ofls -l.lltotal 24 -rw-r--r-- 1 labex labex 123 Jan XX HH:MM combined_output.log ... (output of ls -l) ...Listing Aliases: Use the
aliascommand without any arguments to see all defined aliases.aliasalias ll='ls -l'You might see other default aliases depending on your shell configuration.
Creating a More Complex Alias: You can also create aliases for commands with arguments or multiple commands.
alias myip='ip a | grep "inet " | grep -v "127.0.0.1" | awk "{print \$2}" | cut -d/ -f1'Here,
myipwill show your primary IP address. Note the\$2to escape the$sign so it's passed toawkand not interpreted by the shell when the alias is defined.Test the
myipalias.myip172.17.0.2(Your IP address may vary)
Unsetting an Alias: To remove an alias, use the
unaliascommand.unalias llVerify that the alias is removed.
aliasalias myip='ip a | grep "inet " | grep -v "127.0.0.1" | awk "{print \$2}" | cut -d/ -f1'llshould no longer be in the list.
Shell variables and aliases are temporary and will be lost when you close your terminal session. To make them permanent, you need to add them to your shell's configuration files (e.g., ~/.bashrc or ~/.profile), which will be covered in more advanced topics.
Summary
In this lab, you learned fundamental Linux command-line skills essential for managing text files and the shell environment. You began by mastering output redirection, specifically using > to overwrite files and >> to append content, allowing you to capture command results for logging or further processing. You also explored redirecting standard error (2>) and combining standard output and error (&>) to manage all command output effectively.
Furthermore, you gained proficiency in constructing and understanding command pipelines using the | operator, enabling you to chain commands and process data sequentially. You were introduced to basic text editing with Vim, covering essential commands for inserting, saving, and quitting files. Finally, you learned how to configure and use shell variables for storing data and creating aliases to simplify frequently used commands, enhancing your command-line efficiency and customization.



