0% found this document useful (0 votes)
13 views21 pages

OS2 - Week 8 - Week9

This document outlines a lab exercise for students at the College of Telecommunications & Electronics in Jeddah, focusing on file and directory management in a Linux operating system. It covers commands for listing, copying, moving, and searching for files and directories, including the use of options like -a, -l, -R, and wildcards. The document also explains how to handle hidden files and provides examples of commands to manipulate files effectively.

Uploaded by

salshehri9522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views21 pages

OS2 - Week 8 - Week9

This document outlines a lab exercise for students at the College of Telecommunications & Electronics in Jeddah, focusing on file and directory management in a Linux operating system. It covers commands for listing, copying, moving, and searching for files and directories, including the use of options like -a, -l, -R, and wildcards. The document also explains how to handle hidden files and provides examples of commands to manipulate files effectively.

Uploaded by

salshehri9522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‫المملكـــــــة العربيــــة السعوديـــــة‬

KINGDOM OF SAUDI ARABIA


‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs

-Week 4 – Lab 1
Part 1: Listing Files and Directories
In these tasks you will explore the concepts of files and directories.

On a Linux OS, data is stored in files and files are stored in directories. You may be used to the
term folders to describe directories.

Directories are actually files, too; the data that they hold are the names of the files that have been
entered into the them, along with the inode number (a unique identifier number assigned to each
file) for where the data for that file exists on the disk. As a Linux user, you will want to know how
to manipulate these files and directories.

Now open the terminal application and complete the following tasks

1. To list the contents of the current directory, use the ls command:

cd
ls

Your output should be similar to the following:

In the output of the previous ls command the file names were placed in a light blue color. This
is a feature that many distributions of Linux automatically provide through a feature called an
alias (more on this feature in a later lab).

The color indicates what type the item is. The following table describes some of the more
common colors:

Color Type of File


Black or White Regular file
Blue Directory file
Cyan Symbolic link file (a file that points to another file)
Green Executable file (AKA, a program)

Designed by Aiman Alobadi 1 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs

2. Not all files are displayed by default. There are files, called hidden files, that are not displayed
by default. To display all files, including hidden files, use the -a option to the ls command:

ls –a

Hidden files begin with a period (a dot character). Typically these files and often directories are
hidden because they are not files you normally want to see.

For example, the .bashrc file shown in the example above contains configuration information
for the bash shell. This is a file that you normally don't need to view on a regular basis.

Two important "dot files" exist in every directory: . (which represents the current directory) and
.. (which represents the directory above the current directory)

3. By itself, the ls command just provided the names of the files and directories within the
specified (or current) directory. Execute the following command to see how the -l option
provides more information about a file:

ls -l /etc/hosts

Your output should be similar to the following:

So, what does all of this extra output mean? The following table provides a brief
breakdow‫ال‬8‫غةخ‬n of what each part of the output of ls -l means:

The first character, a - in the previous example, indicates what type of "file" this is. A -
-
character is for plain file while a d character would be for a directory.
rw-r--r-- This represents the permissions of the file. Permissions are discussed in a later lab.
1 This represents something called a hard link count (discussed later).
root The user owner of the file.
root The group owner of the file.
189 The size of the file in bytes

Designed by Aiman Alobadi 2 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


Dec 12
The date/time when the file was last modified.
16:14

4. Sometimes you want to see not only the contents of a directory, but also the contents of the
subdirectories. You can use the -R option to accomplish this:

ls -R /etc/udev

The -R option stands for "recursive". All of the files in the /etc/udev directory will be
displayed as well as all of the files in each subdirectory, in this case the rules.d subdirectory.

Warning: Be careful of the -R option. Some directories are very, very large!

5. You can use file globbing (wildcards) to limit which files or directories you see. For example,
the * character can match "zero or more of any characters" in a filename. Execute the following
command to display only the files that begin with the letter s in the /etc directory:

ls -d /etc/s*

Your output should be similar to the following:

Note that the -d option prevents files from subdirectories from being displayed. It should
always be used with the ls command when you are using file globbing.

6. The ? character can be used to match exactly 1 character in a file name. Execute the following
command to display all of the files in the /etc directory that are exactly four characters long:

ls -d /etc/????

Your output should be similar to the following:

Designed by Aiman Alobadi 3 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs

7. By using square brackets ( [ ] ) you can specify a single character to match from a set of
characters. . Execute the following command to display all of the files in the /etc directory that
begin with the letters "a", "b", "c" or "d":

ls –d /etc/[abcd]*

Your output should be similar to the following:

Part 2: Copying, Moving and Renaming Files and Directories


In this tasks, you will copy, move, and remove files and directories.

Now open the terminal application and complete the following tasks

8. Make a copy of the /etc/hosts file and place it in the current directory. Then list the contents
of the current directory before and after the copy:

ls
cp /etc/hosts hosts
ls

Your output should be similar to the following:

Designed by Aiman Alobadi 4 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


Notice how the second ls command displays a copy of the hosts file.

9. Next you will remove the file, then copy it again, but have the system tell you what is being
done. This can be achieved using the -v or --verbose option. Enter the following commands:

rm hosts
ls
cp –v /etc/hosts hosts
ls

Note that the rm command is used to delete a file. More information on this command will be
provided later in this lab.

Your output should be similar to the following:

Note that the -v switch displays the source and target when the cp command is executed.

10. Enter the following commands to copy the /etc/hosts file, using the period (.) character to
indicate the current directory as the target:

rm hosts
ls
cp –v /etc/hosts .
ls

Your output should be similar to the following:

Designed by Aiman Alobadi 5 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs

The period . character is a handy way to say "the current directory". It can be used with all
Linux commands, not just the cp command.

11. Enter the following commands to copy from the source directory and preserve file attributes by
using the -p option:

rm hosts
ls
cd /etc
ls -l hosts
cp –p hosts /home/sysadmin
cd
ls –l hosts

Your output should be similar to the following:

Notice that the date and permission modes were preserved. Note that the timestamp in the
output above is the same for both the original and the copy (Dec 12 16:14) in the example
provided above. Your output may vary.

12. Type the following commands to copy using a different target name:

rm hosts
cp -p /etc/hosts ~
cp hosts newname
ls –l hosts newname
rm hosts newname

Designed by Aiman Alobadi 6 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs

The first copy with the -p option preserved the original timestamp. Recall that the tilde ~
represents your home directory (/home/sysadmin).

The second copy specified a different filename (newname) as the target. Because it was issued
without the -p option, the system used the current date and time for the target, thus, it did not
preserve the original timestamp found in the source file ( /etc/hosts).

Finally, note that you can remove more than one file at a time as shown in the last rm command.

13. To copy all files in a directory use the -R option. For this task, you will copy the /etc/udev
directory and display the contents of the copied directory:

mkdir Myetc
cp –R /etc/udev Myetc
ls –l Myetc
ls –lR Myetc

14. To remove a directory use the -r option to the rm command:

Designed by Aiman Alobadi 7 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


ls
rm -r Myetc
ls

Your output should be similar to the following:

Note that the rmdir command can also be used to delete directories, but only if the directory is
empty (if it contains no files).

Also note the -r option. This option removes directories and their contents recursively.

15. Moving a file is analogous to a "cut and paste". The file is “cut” (removed) from the original
location and “pasted” to the specified destination. Move a file in the local directory by executing
the following commands:

touch premove
ls
mv premove postmove
ls
rm postmove

Linux Command Description


touch premove Creates an empty file called premove
mv premove This command “cuts” the premove file and “pastes” it to a file called
postmove postmove
rm postmove Removes postmove file

Your output should be similar to the following:

Designed by Aiman Alobadi 8 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs

Week 4 – Lab 2
Part 1: Finding Files and Directories
In these tasks, we will explore how to search for a file on the system. This is useful to know in
situations when you can't find a file on the system, either one that you created or one that was
created by someone else.

Now open the terminal application and complete the following tasks

1. An easy way to search for a file is to use the locate command. For example, you can find the
location of the crontab file by executing the following command:

locate crontab

2. Note that the output from the previous example includes files that have "crontab*" as part of
their name. To find files that are just named "crontab", use the following command:

locate -b "\crontab

Note: The locate command makes use of a database that is traditionally updated once per day
(normally in the middle of the night). This database contains a list of all files that were on the
system when the database was last updated.

As a result, any files that you created today will not normally be searchable with the locate
command. If you have access to the system as the root user (the system administrator account),
you can manually update this file by running the updatedb command. Regular users can not
update the database file.

Another possible solution to searching for "newer" files is to make use of the find command.
This command searches the live filesystem, rather than a static database. The find command
isn't part of the Linux Essentials objectives for this lab, so it is only mentioned here. Execute

Designed by Aiman Alobadi 9 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


man find if you want to explore this command on your own or wait for the lab that explores the
find command.

3. You may just want to find where a command (or its man pages) is located. This can be
accomplished with the whereis command :

whereis passwd

The whereis command only searches for commands and man pages, not just any file.

Recall from earlier that there is more than one passwd man page on the system. This is why you
see multiple file names and man pages (the files that end in .gz are man pages) when you
execute the previous command.

4. Search for files beginning in your home directory containing the name “bash.”

find ~ -name "*bash*"

Your output should be similar to the following:

Remember that ~ is used to represent your home directory.

5. Find files that were modified (or created) less than 5 minutes ago in the specified directory by
using the following commands:

find ~/Music -mmin -5


touch ~/Music/mysong
find ~/Music -mmin -5

Your output should be similar to the following:

Designed by Aiman Alobadi 10 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


The first find command does not find files that were modified within the time specified. You
then created a file by using the touch command and ran the find command again, resulting in
the find command discovering the new file.

The Music directory was displayed with the second find command because the directory was
modified, the result of a file being added to the directory.

6. Execute the following command to find files in the /usr directory that are larger than 2MB in
size:

find /usr -size +2M

Your output should be similar to the following:

7. Find files of type “directory” in the specified location.

find /usr/share/bug -type d

Your output should be similar to the following:

8. To verify that the output displays directories, use the -ls option. The find command uses the -
print option by default which displays just file names. The -ls option provides file details:

find /usr/share/bug -type d -ls

Designed by Aiman Alobadi 11 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


Your output should be similar to the following:

Recall that the "d" character before the permissions "rwxr-x-r-x" indicates that the file is
actually a directory.

Part 2: Viewing Large Text Files


Although large text files can be viewed with the cat command, it is inconvenient to scroll
backwards towards the top of the file. Additionally, really large files can't be displayed in this
manner because the terminal window only stores a specific number of lines of output in memory.

The use of the more or less commands allows for the user to the view data a "page" or a line at a
time. These "pager" commands also permit other forms of navigation and searching that will be
demonstrated in this section.

Note: examples are given using both the more and less commands. Most of the time, the
commands work the same, however the less command is more advanced and has more features.
The more command is still important to know because some Linux distributions don't have the less
command, but all Linux distributions have the more command.

Designed by Aiman Alobadi 12 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


If you are not interested in viewing the entire file or output as a command, then there are numerous
commands that are able to filter the contents of the file or output. In this section, you will learn the
use of the head and tail commands to be able to extract information from the top or bottom of the
output of a command or file contents.

Now open the terminal application and complete the following tasks

1. The /etc/passwd is likely too large to be displayed on the screen without scrolling the screen.
To see a demonstration of this, use the cat command to display the entire contents of the
/etc/passwd file:

cat /etc/passwd

Your output should be similar to the following:

2. Use the more command to display the entire contents of the /etc/passwd file:

more /etc/passwd

Your output should be similar to the following:

Designed by Aiman Alobadi 13 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs

Note: The "--More--(92%)" indicates you are "in" the more command and 92% through the
current data.

3. While you are in the more command, you can view the help screen by pressing the h key:

Your output should be similar to the following:

Designed by Aiman Alobadi 14 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


4. Press the Spacebar to view the rest of the document:

<SPACE>

In the next example, you will learn how to search a document using either the more or less
commands.

Searching for a pattern within both the more and less commands is done by typing the slash, /,
followed by the pattern to find. If a match is found, the screen should scroll to the first match.
To move forward to the next match, press the n key. With the less command you can also
move backwards to previous matches by pressing the N (capital n)key.

5. Use the less command to display the entire contents of the /etc/passwd file. Then search for
the word bin, use n to move forward, and N to move backwards. Finally, quit the less pager by
typing the letter q:

less /etc/passwd
/bin
n n n N N N q

Unlike the more command which automatically exits when you reach the end of a file, you must
press a quit key such as q to quit the less program.

6. You can use the head command to display the top part of a file. By default, the head command
will display the first ten lines of the file:

head /etc/passwd

Your output should be similar to the following:

Designed by Aiman Alobadi 15 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


7. Use the tail command to display the last ten lines of the /etc/passwd file:

tail /etc/passwd

Your output should be similar to the following:

8. Use the head command to display the first two lines of the /etc/passwd file:

head -2 /etc/passwd

Your output should be similar to the following:

9. Execute the following command line to pipe the output of the ls command to the tail
command, displaying the last five file names in the /etc directory:

ls /etc | tail -5

Your output should be similar to the following:

Designed by Aiman Alobadi 16 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


As you've seen, both head and tail commands output ten lines by default. You could also use
an option -# (or you can use the option -n #, where # is a number of lines to output). Both
commands can be used to read standard input from a pipe that receives output from a command.

You have also seen where head and tail commands are different: the head command starts
counting its lines to output from the top of the data, whereas the tail command counts the
number of lines to output from the bottom of the data. There are some additional differences
between these two commands as demonstrated in the next few tasks.

10. Another way to specify how many lines to output with the head command is to use the option -
n -#, where # is number of lines counted from the bottom of the output to exclude. Notice the
minus symbol (-) in front of the #. For example, if the /etc/passwd contains 24 lines and the
following command will display lines 1-4, excluding the last twenty lines:

head -n -20 /etc/passwd

Part 3: Command Line Pipes and Redirection


Normally, when you execute a command, the output is displayed in the terminal window. This
output (also called a channel) is called standard output, symbolized by the term stdout. The file
descriptor number for this channel is 1.

Standard error (stderr) occurs when an error occurs during the execution of a command; it has a
file descriptor of 2. Error messages are also sent to the terminal window by default.

In this lab, you will use characters that redirect the output from standard output ( stdout) and
standard error (stderr) to a file or to another command instead of the terminal screen.

Standard input, stdin, usually is provided by you to a command by typing on the keyboard; it has a
file descriptor of 0. However, by redirecting standard input, files can also be used as stdin.

Now open the terminal application and complete the following tasks

Designed by Aiman Alobadi 17 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


1. Use the redirection symbol > to redirect the output from the normal output of stdout
(terminal) to a file. Type the following:

echo "Hello World"


echo "Hello World" > mymessage
cat mymessage

Your output should be similar to the following:

The first command echos the message (stdout) to the terminal.

The second command redirects the output; instead of sending it to the terminal, the output is
sent to a file called mymessage.

The last command displays the contents of the mymessage file.

2. When you use the > symbol to redirect stdout, the contents of the file are first destroyed. Type
the following commands to see a demonstration:

cat mymessage
echo Greetings > mymessage
cat mymessage

Your output should be similar to the following:

Notice that using one redirection symbol overwrites an existing file. This is called "clobbering"
a file.

3. You can avoid clobbering a file by using >> instead of > . By using >> you append to a file.
Execute the following commands to see a demonstration of this:

cat mymessage
echo "How are you?" >> mymessage
cat mymessage

Designed by Aiman Alobadi 18 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


Your output should be similar to the following:

Notice that by using >> all existing data is preserved and the new data is appended at the end
of the file.

4. The find command is a good command to demonstrate how stderr works. This command
searches the filesystem for files based on criteria such as filename. Run the following command
and observe the output:

find /etc -name hosts

Your output should be similar to the following:

Notice the error message indicating you do not have permission to access
certain files/directories. This is because as a regular user, you don't have
right to "look inside" some directories. These types of error messages are
sent to stderr, not stdout.

The find command will be covered in greater detail later. The command is just being used now
to demonstrate the difference between stdout and stderr.

5. To redirect stderr (error messages) to a file issue the following command:

find /etc -name hosts 2> [Link]


cat [Link]

Your output should be similar to the following:

Designed by Aiman Alobadi 19 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


Recall that the file descriptor for stderr is the number 2, so it is used along with the > symbol
to redirect the sdterr output to a file called [Link]. Note that 1> is the same as >.

Note: the previous example demonstrates why knowing redirection is important. If you want to
"ignore" the errors that the find command displays, you can redirect those messages into a file
and look at them later, making it easier to focus on the rest of the output of the command.

6. You can also redirect stdout and stderr into two separate files.

find /etc -name hosts > [Link] 2> [Link]


cat [Link]
cat [Link]

Your output should be similar to the following:

Notice that a space is permitted but not required after the > redirection
symbol.

7. To redirect both standard output (stdout) and standard error (stderr) to one file, first redirect
stdout to a file and then redirect stderr to that same file by using the notation 2>&1.

find /etc -name hosts > [Link] 2>&1


cat [Link]

Your output should be similar to the following:

The 2>&1 part of the command means send the stderr (channel 2) to the same place where
stdout (channel 1) is going.

Designed by Aiman Alobadi 20 / 21


‫المملكـــــــة العربيــــة السعوديـــــة‬
KINGDOM OF SAUDI ARABIA
‫المؤسسة العامة للتدريب التقني والمهني‬
Technical and Vocational Training Corporation
College of Telecommunications & Electronics – Jeddah
‫كلية االتصاالت واإللكترونيات بجدة‬
Department of Computer Technology
‫قسم تقنية الحاسب‬

Operating System – 2 Labs


8. Another popular form of redirection is to take the output of one command and send it into
another command as input. For example, the output of some commands can be massive,
resulting in the output scrolling off the screen too quickly to read. Execute the following
command to take the output of the ls command and send it into the more command which
displays one page of data at a time:

ls -l /etc | more

Your output should be similar to the following:

You will need to press the spacebar to continue or you can also press CTRL-c to escape this
listing.

The cut command is useful for extracting fields from files that are either delimited by a
character, like the colon (:) in /etc/passwd, or that have a fixed width. It will be used in the
next few examples as it typically provides a great deal of output that we can use to demonstrate
using the | character.

Designed by Aiman Alobadi 21 / 21

You might also like