Hands-on Lab: File Content, Compression,
& Networking Commands
Objectives
In this lab, you will be introduced to the use of basic Unix commands related to the
following categories:
File content viewing commands
Text processing commands
File/Folder compression & archiving commands
Networking commands
Exercise 1 - Viewing file content
In this exercise, you will work with commands for viewing file content.
Important: In order to complete this section, you must
run the following in your current directory:
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-
DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt
1.1. Display all file contents
cat
The cat command displays contents of files.
The following command prints the content of the file usdoi.txt which you
downloaded earlier.
cat usdoi.txt
1.2. Display file contents page-wise
more
The more command displays the file contents page by page.
Press spacebar to display the next page.
more usdoi.txt
1.3. Display first few lines of a file
head
Print the first 10 lines of the file usdoi.txt.
head usdoi.txt
You can specify the number of lines to be printed.
Print the first 3 lines of the file usdoi.txt.
head -3 usdoi.txt
1.4. Display last lines of a file
tail
Print the last 10 lines of the file usdoi.txt.
tail usdoi.txt
You can specify the number of lines to be printed.
Print the last 2 lines of the file usdoi.txt.
tail -2 usdoi.txt
1.5. Count lines, words or characters
wc
If you want to find the number of lines, words and characters in a file, for
example usdoi.txt, enter the command:
wc usdoi.txt
The output contains the number of lines followed by number of words followed by
number of characters in the file.
To print only the number of lines in usdoi.txt, use the -l option:
wc -l usdoi.txt
To print only the number of words in usdoi.txt, use the -w option:
wc -w usdoi.txt
To print only the number of characters in usdoi.txt, use te -c option:
wc -c usdoi.txt
Exercise 2: Customizing view of file
content
2.1. View sorted file lines
sort
To view the sorted lines of usdoi.txt:
sort usdoi.txt
To view the reverse-sorted lines of usdoi.txt:
sort -r usdoi.txt
2.2. View with repeated, consecutive lines merged into one
uniq
First download the following file:
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-
LX0117EN-SkillsNetwork/labs/module%201/zoo.txt
View the raw contents of zoo.txt:
cat zoo.txt
View the contents of zoo.txt with equal, consecutive lines merged into one:
uniq zoo.txt
2.3. Extract lines matching specified criteria
grep
The grep command allows you to specify a pattern and search for lines from the
input text that contain a match to the pattern.
The following command prints all lines in the file usdoi.txt which contain the
word people.
grep people usdoi.txt
Some of the frequently used options for grep are:
Option Description
-n Along with the matching lines, also print the line numbers
-c Get the count of matching lines
-i Ignore the case of the text while matching
-v Print all lines which do not contain the pattern
-w Match only if the pattern matches whole words
Prints all lines from the /etc/passwd file, which do not contain the pattern login.
grep -v login /etc/passwd
2.4. View lines of file with filter applied to each line
cut
The cut command allows you to view the lines of a file after a filter is applied to each
line. For example, you can use cut with the -c option to view the first two characters
of each line:
cut -c -2 zoo.txt
or each line starting from the second character:
cut -c 2- zoo.txt
2.5. View multiple files side by side
paste
Download the following file:
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-
LX0117EN-SkillsNetwork/labs/module%201/zoo_ages.txt
The paste command allows you to view multiple files at once - with lines being
aligned as columns. You can see what that looks like by entering:
paste zoo.txt zoo_ages.txt
You can also customize the delimiter. Instead of the default tab, you could specify a
comma as follows:
paste -d "," zoo.txt zoo_ages.txt
Exercise 3: File and folder archiving and
compression
3.1. Create and manage file archives
tar
The tar command allows you to pack multiple files and directories into a single
archive file.
The following command creates an archive of the entire /bin directory into a file
named bin.tar.
The options used are as follows:
Option Description
-c Create new archive file
-v Verbosely list files processed
-f Archive file name
tar -cvf bin.tar /bin
To see the list of files in the archive, use the -t option:
tar -tvf bin.tar
To untar the archive or extract files from the archive, use the -x option:
tar -xvf bin.tar
Use the ls command to verify that the folder bin is extracted.
ls -l
3.2. Package and compress archive files
zip
The zip command allows you to compress files.
The following command creates a zip file named config.zip consisting of all the files
with extension .conf in the /etc directory.
zip config.zip /etc/*.conf
The -r option can be used to zip an entire directory.
The following command creates an archive of the /bin directory.
zip -r bin.zip /bin
3.3. Extract, list, or test compressed files in a ZIP archive
unzip
The following command lists the files of the archive called config.zip
unzip -l config.zip
The following command extracts all the files in the archive bin.zip.
unzip -o bin.zip
We added the -o option to force overwrite, in case you run the command more than
once.
You should see a folder named bin created in your directory.
Exercise 4 - Networking commands
4.1. Show the system's host name
hostname
To view the current host name, run the command below .
hostname
You can use the -i option to view the IP adrress of the host:
hostname -i
4.2. Test if a host is reachable
ping
Check if www.google.com is reachable. The command keeps sending data packets to
the www.google.com server and prints the response it gets back. (Press Ctrl+C to
stop pinging)
ping www.google.com
If you want to ping only for a limited number of times, use -c option.
ping -c 5 www.google.com
4.3. Display network interface configuration
ifconfig
The ifconfig command is used to configure or display network interface parameters
for a network.
To display the configuration of all network interfaces of the system, enter:
ifconfig
To display the configuration of an ethernet adapter eth0, enter:
ifconfig eth0
eth0 is usually the primary network interface that connects your server to the
network.
You can see your server's IP address in line number 2 after the word inet.
4.4. Transfer data from or to a server
curl
You can use curl to access the file at the following URL and display the file's
contents on your screen:
curl https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-
DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt
or to access the file at the given URL and save it in your current working directory:
curl -O https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-
DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt
4.5. Downloading file(s) from a URL
wget
The wget command is similar to curl - however it's primary use is for file
downloading. One unique feature of wget is that it can recursively download files at a
URL.
To see how wget works, first remove usdoi.txt from your current directory:
rm usdoi.txt
and re-download it using wget as follows:
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-
DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.login
Practice exercises
Before you begin, ensure you're in your home directory:
cd ~
pwd
1. Problem: whoami
Display your username
2. Problem: uname -r
View the kernel version
3. Problem: wc –l /etc/passwd
Display the number of lines in the /etc/passwd file.
4. Problem: grep not install /var/log/bootstrap.log
Display the lines that contain the string 'not installed' in /var/log/bootstrap.log.
5. Problem: wget..... grep –r
https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-
DB0250EN-SkillsNetwork/labs/Bash%20Scripting/top-sites.txt contains most
popular websites. Find out all the websites that have the word org in them.
6. Problem: head -7 top-sites.txt
Print the first 7 lines of top-sites.txt
7. Problem: tail -7 top-sites.txt
Print the last 7 lines of top-sites.txt
8. Problem: zip top-sites.zip top-sites.txt top-sites
Zip the file top-sites.txt into a file called top-sites.zip
9. Problem: cut –c -2 top-sites.txt
Print the first three characters of each line from top-sites.txt
10. Problem:
Print details of the eth0 internet adapter