akatore / Hands-on-Lab-Common-Linux-Unix-commands

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

<style></style>
</head>

cognitiveclass.ai logo

Hands-on Lab: Common Linux/Unix commands

Estimated time needed: 40 minutes

Objectives

In this lab, you will be introducted to the use basic Unix commands related to the following categories:

  • General purpose commands.
  • Directory management commands.
  • File management commands.
  • Access control commands.
  • Text processing commands.
  • Networking commands.

About Skills Network Cloud IDE

Skills Network Cloud IDE (based on Theia and Docker) provides an environment for hands on labs for course and project related labs. Theia is an open source IDE (Integrated Development Environment), that can be run on desktop or on the cloud. to complete this lab, we will be using the Cloud IDE based on Theia.

Important Notice about this lab environment

Please be aware that sessions for this lab environment are not persisted. Every time you connect to this lab, a new environment is created for you. Any data or files you may have saved in the earlier session would get lost. Plan to complete these labs in a single session, to avoid losing your data.

Exercise 1 - General purpose commands

Open a new terminal, by clicking on the menu bar and selecting Terminal->New Terminal, as in the image below.

This will open a new terminal at the bottom of the screen as in the image below.

Run the commands below on the newly opened terminal. (You can copy the code by clicking on the little copy button on the bottom right of the codeblock below and then paste it, wherever you wish.)

1.1. Display the name of the current user

whoami

ebnf
whoami

It will display the user name as theia. You are logged into this lab as theia.

You can get a list of currently logged in users using the command 'who'. But this command doesn't work in theia environment yet.

1.2. Know the user and group identity information

id

This command displays the user id and group id information of the current user.

applescript
id

It will display the uid(user id) and gid(group id) for the user theia.

1.3. Display date and time

date

The date command displays current date and time.

applescript
date

It has several options which help you get date in your favourite format.
The following command displays current date in mm/dd/yy format.

applescript
date "+%D"

Here are some of the popular format specifiers that you can try out.

Specifier Explaination
%d Display the day of the month (01 to 31)
%h Displays abbreviated month name (Jan to Dec)
%m Displays the month of year (01 to 12)
%Y Display four-digit year
%T Display the time in 24 hour format as HH:MM:SS
%H Display the hour

1.4. List the files and directories

ls

List the files and directories in the current directory.

ebnf
ls

You many not get any output if your directory is empty.

The following command will list all the files in the /bin directory.

jboss-cli
ls /bin

List all files starting with b in the /bin directory.

awk
ls /bin/b*

List all files ending with r in the /bin directory.

awk
ls /bin/*r

1.5. Get basic information about the operating system

uname

By default the command prints the kernel name.

ebnf
uname

You will see Linux printed in the output.

Using the -a opton prints all the system information in the following order: Kernel name, network node hostname, kernel release date, kernel version, machine hardware name, hardware platform, operating system.

css
uname -a

1.6. Get information about active processes

ps

ps lists the processes that are currently running and their PIDs (process ids).

ebnf
ps

The output contains the processes that are owned by you.

The -e option displays all the processes running on the system. The includes processes owned by other users also.

ebnf
ps -e

1.7. Get information on the running processes and system resources

top

top command provides a dynamic real-time view of the running system.

It shows the summary information of the system and the list of processes or threads which are currently managed by the Kernel.

It gives information related to cpu and memory usage per process.

coq
top

Here is a sample output.

When started for the first time, you'll be presented with the following elements on the main top screen.

  1. Summary Area. - shows information like system uptime, number of users, load average, memory usage etc.
  2. Fields/Columns Header.
  3. Task Area.

The output keeps refreshing until you press 'q' or Ctrl+c

If you want to exit automatically after a specified number of repetitions, use the -n option as in the following example:

apache
top -n 10

using 'top' we can find out which process is consuming the most resources. You can press the following keys while 'top' is running to sort the list :

M - sort by memory usage
P - sort by CPU usage
N - sort by process ID
T - sort by the running time

1.8. Display Messages

echo

echo command displays the given text on the screen.

bash
echo "Welcome to the linux lab"

These special characters help you better format your output.

Special Character Meaning
\n Represents a newline character
\t A tab character

Use the -e option of the echo command when working with special characters.

bash
echo -e "This will be printed \nin two lines"

1.9. Download a file from the internet.

wget

wget command helps you to donwload a file at a given url.

This command download the file usdoi.txt from the given url.

awk
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt

Verify using the ls command.

ebnf
ls

1.10. View the Reference Manual

man

man command displays the user manual of the command given as argument.

For example, to see the manual page of 'ls' command, use:

powershell
man ls

Exercise 2 - Directory Management Commands

2.1. Print the name of your current working directory.

pwd

bash
pwd

This will print /home/project.

2.2. Create a directory

mkdir

mkdir creates a new directory.

To create a directory named 'scripts' in your current directory, run the following command.

arduino
mkdir scripts

Use the ls command to verify if the scripts directories got created.

ebnf
ls

2.3. Change your current working directory.

cd

To get into the directory scripts directory, run the command below .

bash
cd scripts

Use the pwd command to verify if your current working directory has changed.

bash
pwd

If you use cd without any directory name, it will move you back to your home directory.

bash
cd

Use the pwd command to verify if your current working directory has changed.

bash
pwd

Run the command below to move to the parent directory. .. is a shortcut that refers to the parent directory of your current directory.

gams
cd ..

2.4. Get a list of files in a directory

ls

Lists the files in the current directory or the directory given as argument.

ebnf
ls -l

Prints a long list of files that has additional information compared to the simple ls command.

Here are some popular options that you can try with the ls command.

Option Description
-a list all the files including hidden files
-d list directories themselves, not their contents
-h with -l and -s, print sizes like 1K, 234M, 2G etc
-l long listing of files which include information about permission, owner, size etc
-F classify files by appending type indcator like ,/ etc. to file names
-r reverse order while sorting
-S sort by file size, largest first
-t sort by time, newest first

Get a long listing of all files in /etc, including hidden files, if any.

jboss-cli
ls -la /etc

To list the files based on modifcation time, use -t option.

The most recently modified file will be on top.

This is more frequently used with -l option.

powershell
ls -lt

To view the current directory attributes instead of their contents, use the following command. If you want any other directory's attributes, provide the directory name as argument.

jboss-cli
ls -ld /etc

To list the files sorted by file size in descending order, use -S option.

stata
ls -lS

To get the files sorted by file size in ascending order, add -r option.

ebnf
ls -lrS

2.5. Delete directory

rmdir

rmdir removes a directory.

Let us create a dummy directory in the the /tmp folder .

awk
mkdir /tmp/dummy

Verify by using the ls command.

jboss-cli
ls /tmp

Delete the dummy directory

awk
rmdir /tmp/dummy

Verify by using the ls command.

jboss-cli
ls /tmp

Note: Before removing any directory make sure that you do not have any files or sub directories.

2.6. Search and locate files

find

Find command is used to search for files in a directory. You can search for files based on different categories like file name, file type, owner, size, timestamps etc.

Find command conducts the search in the entire directory tree starting from the directory name given.

This command finds all txt files in the subfolders of the /etc directory.

routeros
find /etc -name '.txt'

Along with the listing of txt files, you may get some Permission denied errors.

That is normal, as you have limited access on the lab machine.

2.7. Display the amount of disk space available on file systems

df

The 'df' command displays the information of device name, total blocks, total disk space, used disk space, available disk space and mount points on a file system.

Without any arguments, the commands shows the information for all currently mounted file systems.

ebnf
df

Use the -h option to view the disk space usage in human readable format, i.e, in megabytes, gigabytes etc.

ebnf
df -h

Exercise 3 - File Management Commands

3.1. Display file contents

cat

cat command displays contents of files.

The following command prints the content of the file usdoi.txt which you have downloaded earlier.

ebnf
cat usdoi.txt

If you get a file not found error, then run these commands.

cd takes you to your home directory.

wget download the file.

cat will print the file onto the screen.

awk
cd wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt cat usdoi.txt

3.2. Display file contents page-wise

more

The more command displays the file contents page by page.

Press spacebar to display the next page.

dos
more usdoi.txt

3.3. Display first few lines of a file

head

Print the first 10 lines of the file usdoi.txt.

ebnf
head usdoi.txt

You can specify the number of lines to be printed.

Print the first 3 lines of the file usdoi.txt.

apache
head -3 usdoi.txt

3.4. Display last lines of a file

tail

Print the last 10 lines of the file usdoi.txt.

ebnf
tail usdoi.txt

You can specify the number of lines to be printed.

Print the last 2 lines of the file usdoi.txt.

apache
tail -2 usdoi.txt

3.5. Copy files

cp

Copy usdoi.txt into a file named usdoi-backup.txt.

stylus
cp usdoi.txt usdoi-backup.txt

Use ls command to verify if the copy was successful.

ebnf
ls

The command below copies the content of /etc/passwd to a file named 'users.txt' under the current directory.

awk
cp /etc/passwd users.txt

Use ls command to verify if the copy was successful.

ebnf
ls

3.6. Move, Rename a file

mv

mv command moves a file from one directory to another.

While moving a file, if the target file is existing, it is overwritten.

If the source and target directories are same, it works like rename operation.

Rename users.txt as user-info.txt

nginx
mv users.txt user-info.txt

Use ls command to verify.

ebnf
ls

Move user-info.txt to the /tmp directory

nginx
mv user-info.txt /tmp

Use ls command to verify.

ebnf
ls

jboss-cli
ls -l /tmp

3.7. Create a blank file

touch

Create an empty file named myfile.txt

ebnf
touch myfile.txt

Use ls command to verify.

ebnf
ls -l

If the file already exists, the touch command updates the access timestamp of the file.

ebnf
touch usdoi.txt

Use ls command to verify.

ebnf
ls -l

3.8. Remove files

rm

The rm command is ideally used along with the -i option, which makes it ask for confirmation before deleting.

Remove the file myfile.txt. Press y to confirm deletion, or n to cancel

stylus
rm -i myfile.txt

Use ls command to verify.

ebnf
ls -l

3.9. Create and manage file archives

tar

tar command allows you to copy 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
mipsasm
tar -cvf bin.tar /bin

To see the list of files in the archive, use -t option:

ebnf
tar -tvf bin.tar

To untar the archive or extract files from the archive, use -x option:

ebnf
tar -xvf bin.tar

Use ls command to verify that the folder bin is extracted.

ebnf
ls -l

3.10. Package and compress archive files

zip

zip command allows you to compress files.

The following command creates a zip named config.zip and of all the files with extension .conf in the /etc directory.

awk
zip config.zip /etc/*.conf

The -r option can be used to zip the entire folder.

The following command creates an archive of the '/bin' directory.

python
zip -r bin.zip /bin

3.11. Extract, list, or test compressed files in a ZIP archive

unzip

The following command lists the files of the archive called 'config.zip'

arduino
unzip -l config.zip

The following command extracts all the files in the archive bin.zip.

python
unzip bin.zip

You should see a folder named bin created in your directory.

Exercise 4 - Access Control Commands

Each file/directory has permissions set for the file owner, group owner and others.

The following permissions are set for each file:

Permission symbol
read r
write w
execute x

To see the permissions currently set for a file, run ls -l command.

For example, to see the permissions for a file named 'usdoi.txt' in your current directory, run:

ebnf
ls -l usdoi.txt

A sample output looks like:

-rw-r--r-- 1 theia theia 8121 May 31 16:45 usdoi.txt

The permissions set here are 'rw-r--r--'

Here, owner has read and write permissions, group owner has read permission and others also have read permission.

4.1 chmod

chmod command lets you change the permissions set for a file.

The change of permissions is specified with the help of a combination of the following characters:

Option Description.
r, w and x representing read, write and execute permissions respectively
u,g and o representing user categories owner, group and others respectively
+, - representing grant and revoke operations respectively

The command below removes read permission for all (user,group and other) on usdoi.txt.

ebnf
chmod -r usdoi.txt

Verify the changed permissions.

ebnf
ls -l usdoi.txt

Add read access to all on usdoi.txt.

ebnf
chmod +r usdoi.txt

Verify the changed permissions.

ebnf
ls -l usdoi.txt

To remove the read permission for 'others' category.

ebnf
chmod o-r usdoi.txt

Verify the changed permissions.

ebnf
ls -l usdoi.txt

Exercise 5 - Text Processing Commands

5.1. 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'.

ebnf
wc usdoi.txt

The output contains the number of lines followed by number of words followed by number of characters in the file.

Print only the number of lines in 'usdoi.txt'.

ebnf
wc -l usdoi.txt

Print only the number of words in 'usdoi.txt'.

ebnf
wc -w usdoi.txt

Print only the number of characters in 'usdoi.txt'.

ebnf
wc -c usdoi.txt

5.2. Perform search operations within the text

grep

grep command allows you to specify patterns and search for lines matching the pattern, from the input text.

The following command prints all lines in the file usdoi.txt which contain the word people.

ebnf
grep people usdoi.txt

Some of the frequently used options of grep are:

Option Description.
-n Along with the matching lines, print the line numbers also
-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.

gradle
grep -v login /etc/passwd

Exercise 6 - Networking commands

6.1. Show the system's host name

hostname

To view the current host name, run the command below .

ebnf
hostname

You can use the -i option to view the IP adrress of the host:

css
hostname -i

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

stylus
ping www.google.com

If you want to ping only for a limited number of times, use -c option.

stylus
ping -c 5 www.google.com

6.3. Display network interface configuration

ifconfig

Configures or displays network interface parameters for a network.

Display the configuration of all network interfaces of the system:

ebnf
ifconfig

Display the configuration of the ethernet adapter.

apache
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 the line number 2 after the word inet.

6.4. Transfer data from or to a server

curl

Access the file at the given url and display the contents on to the screen.

awk
curl https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt

Access the file at the given url and save it in the current directory.

awk
curl -O https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt

Practice exercises

  1. Problem:

Display the content of /home directory.

Click here for Hint

Use 'ls' command.

Click here for Solution
jboss-cli
ls /home

  1. Problem:

Ensure that you are in your home directory.

Click here for Hint

Use 'cd' to move to the home directory and then use 'pwd' to verify'.

Click here for Solution
bash
cd pwd

  1. Problem:

Create a new directory called 'final' in your home directory.

Click here for Hint

Use 'mkdir' command.

Click here for Solution
arduino
mkdir final

  1. Problem:

View the permissions of the newly created directory 'final'.

Click here for Hint

Use -d and -l options of the ls command.

Click here for Solution
actionscript
ls -ld final

  1. Problem:

Create a new blank file named 'display.sh' in the final directory

Click here for Hint

use 'cd and 'touch' commands.

Click here for Solution
stata
cd final touch display.sh

  1. Problem:

Copy display.sh as report.sh.

Click here for Hint

Use the cp command.

Click here for Solution
stata
cp display.sh report.sh

  1. Problem:

Delete the file 'display.sh'.

Click here for Hint

Use 'rm' command.

Click here for Solution
stata
rm -i display.sh

  1. Problem:

List the files in /etc directory in the ascending order of their access time.

Click here for Hint

Use the ls command with right options.

Click here for Solution
jboss-cli
ls -ltr /etc

  1. Problem:

Display the current time.

Click here for Hint

Use the format option '%T' of the date command.

Click here for Solution
applescript
date "+%T"

  1. Problem:

Display the number of lines in the /etc/passwd file.

Click here for Hint

use the wc command with right option.

Click here for Solution
awk
wc -l /etc/passwd

  1. Problem:

Display the lines that contain the string 'not installed' in /var/log/bootstrap.log page-wise.

Click here for Hint

use the grep and more commands.

Click here for Solution
stata
grep "not installed" /var/log/bootstrap.log | more

  1. Problem:

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.

Click here for Hint

use the wget command to download the file.

use the grep command to search

Click here for Solution
awk
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-SkillsNetwork/labs/Bash%20Scripting/top-sites.txt grep org top-sites.txt

Authors

Ramesh Sannareddy

Other Contributors

Rav Ahuja

Change Log

Date (YYYY-MM-DD) Version Changed By Change Description
2021-05-30 0.1 Ramesh Sannareddy Created initial version of the lab

Copyright (c) 2021 IBM Corporation. All rights reserved.

<script>window.addEventListener('load', function() { snFaculty.inject(); });</script> <script src="https://skills-network-assets.s3.us.cloud-object-storage.appdomain.cloud/scripts/inject.43989f87.js"></script> <script src="https://unpkg.com/@highlightjs/cdn-assets@10.7.1/highlight.min.js"></script> <script src="https://unpkg.com/highlightjs-badge@0.1.9/highlightjs-badge.min.js"></script>

<style> @media print { .code-badge { display: none; } } .code-badge-pre { position: relative; } .code-badge { display: flex; flex-direction: row; white-space: normal; background: transparent; background: #333; color: white; font-size: 0.875em; opacity: 0.5; transition: opacity linear 0.5s; border-radius: 0 0 0 7px; padding: 5px 8px 5px 8px; position: absolute; right: 0; top: 0; } .code-badge.active { opacity: 0.8; } .code-badge:hover { opacity: .95; } .code-badge a, .code-badge a:hover { text-decoration: none; } .code-badge-language { margin-right: 10px; font-weight: 600; color: goldenrod; } .code-badge-copy-icon { font-size: 1.2em; cursor: pointer; padding: 0 7px; margin-top:2; } .fa.text-success:{ color: limegreen !important } </style>
<div class="code-badge">
    <div class="code-badge-language">{{language}}</div>
    <div title="Copy to clipboard">
        <i class="{{copyIconClass}} code-badge-copy-icon"></i>
    </div>
 </div>

About


Languages

Language:HTML 100.0%