Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

linux_terminal_commands

This document is a hands-on lab guide for learning common Linux/Unix commands, covering general purpose, directory management, file management, access control, text processing, and networking commands. It provides step-by-step exercises to execute commands in a Skills Network Cloud IDE environment, with important notices regarding session persistence. The lab aims to familiarize users with essential command-line operations and file handling in a Unix-like system.

Uploaded by

theatkjonio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

linux_terminal_commands

This document is a hands-on lab guide for learning common Linux/Unix commands, covering general purpose, directory management, file management, access control, text processing, and networking commands. It provides step-by-step exercises to execute commands in a Skills Network Cloud IDE environment, with important notices regarding session persistence. The lab aims to familiarize users with essential command-line operations and file handling in a Unix-like system.

Uploaded by

theatkjonio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

03.09.

2023, 10:09 about:blank

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) provides an environment for hands on labs for course and project related labs. Theia is an open source IDE
(Integrated Development Environment) platform, that can be run on desktop or on the cloud. In this environment you will see lab instructions on the left
side of your browser tab, and the Theia-based IDE to excute the lab instructions on right side.

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.

about:blank 1/15
03.09.2023, 10:09 about:blank

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

1. 1

1. whoami

Copied!

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.

1. 1

1. id

Copied!

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.


1. 1

1. date

Copied!

It has several options which help you get date in your favourite format.

The following command displays current date in mm/dd/yy format.


1. 1

1. date "+%D"

Copied!

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)
about:blank 2/15
03.09.2023, 10:09 about:blank
Specifier Explaination
%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.


1. 1

1. ls

Copied!

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

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

1. ls /bin

Copied!

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


1. 1

1. ls /bin/b*

Copied!

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


1. 1

1. ls /bin/*r

Copied!

1.5. Get basic information about the operating system

uname

By default the command prints the kernel name.


1. 1

1. uname

Copied!

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.
1. 1

1. uname -a

Copied!

1.6. Get information about active processes

ps

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

1. 1

1. ps

Copied!

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.

1. 1

1. ps -e

Copied!

about:blank 3/15
03.09.2023, 10:09 about:blank
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.


1. 1

1. top

Copied!

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:

1. 1

1. top -n 10

Copied!

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.


1. 1

1. echo "Welcome to the linux lab"

Copied!

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.
1. 1

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

Copied!

about:blank 4/15
03.09.2023, 10:09 about:blank
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.

1. 1

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

Copied!

Verify using the ls command.

1. 1

1. ls

Copied!

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:

1. 1

1. man ls

Copied!

Exercise 2 - Directory Management Commands


2.1. Print the name of your current working directory.

pwd
1. 1

1. pwd

Copied!

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.
1. 1

1. mkdir scripts

Copied!

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


1. 1

1. ls

Copied!

2.3. Change your current working directory.

cd

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

1. 1

1. cd scripts

Copied!

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

1. 1

about:blank 5/15
03.09.2023, 10:09 about:blank
1. pwd

Copied!

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

1. 1

1. cd

Copied!

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

1. 1

1. pwd

Copied!

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

1. cd ..

Copied!

2.4. Get a list of files in a directory

ls

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

1. ls -l

Copied!

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.

1. 1

1. ls -la /etc

Copied!

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.


1. 1

1. ls -lt

Copied!

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.
1. 1

1. ls -ld /etc

Copied!

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

about:blank 6/15
03.09.2023, 10:09 about:blank
1. ls -lS

Copied!

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

1. ls -lrS

Copied!

2.5. Delete directory

rmdir

rmdir removes a directory.

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


1. 1

1. mkdir /tmp/dummy

Copied!

Verify by using the ls command.

1. 1

1. ls /tmp

Copied!

Delete the dummy directory


1. 1

1. rmdir /tmp/dummy

Copied!

Verify by using the ls command.

1. 1

1. ls /tmp

Copied!

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.
1. 1

1. find /etc -name '*.txt'

Copied!

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.
1. 1

1. df

Copied!

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

about:blank 7/15
03.09.2023, 10:09 about:blank
1. 1

1. df -h

Copied!

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.
1. 1

1. cat usdoi.txt

Copied!

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.


1. 1
2. 2
3. 3

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

Copied!

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.


1. 1

1. more usdoi.txt

Copied!

3.3. Display first few lines of a file

head

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


1. 1

1. head usdoi.txt

Copied!

You can specify the number of lines to be printed.

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


1. 1

1. head -3 usdoi.txt

Copied!

3.4. Display last lines of a file

tail

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

1. 1

1. tail usdoi.txt

Copied!

about:blank 8/15
03.09.2023, 10:09 about:blank
You can specify the number of lines to be printed.

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


1. 1

1. tail -2 usdoi.txt

Copied!

3.5. Copy files

cp

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


1. 1

1. cp usdoi.txt usdoi-backup.txt

Copied!

Use ls command to verify if the copy was successful.


1. 1

1. ls

Copied!

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

1. cp /etc/passwd users.txt

Copied!

Use ls command to verify if the copy was successful.


1. 1

1. ls

Copied!

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


1. 1

1. mv users.txt user-info.txt

Copied!

Use ls command to verify.

1. 1

1. ls

Copied!

Move user-info.txt to the /tmp directory


1. 1

1. mv user-info.txt /tmp

Copied!

Use ls command to verify.

1. 1

1. ls

Copied!

1. 1

1. ls -l /tmp

about:blank 9/15
03.09.2023, 10:09 about:blank
Copied!

3.7. Create a blank file

touch

Create an empty file named myfile.txt


1. 1

1. touch myfile.txt

Copied!

Use ls command to verify.


1. 1

1. ls -l

Copied!

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

1. touch usdoi.txt

Copied!

Use ls command to verify.


1. 1

1. ls -l

Copied!

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.


1. 1

1. rm -i myfile.txt

Copied!

Use ls command to verify.


1. 1

1. ls -l

Copied!

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 |
1. 1

1. tar -cvf bin.tar /bin

Copied!

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

1. 1

1. tar -tvf bin.tar

Copied!

about:blank 10/15
03.09.2023, 10:09 about:blank
To untar the archive or extract files from the archive, use -x option:

1. 1

1. tar -xvf bin.tar

Copied!

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


1. 1

1. ls -l

Copied!

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.
1. 1

1. zip config.zip /etc/*.conf

Copied!

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

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

1. 1

1. zip -r bin.zip /bin

Copied!

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’

1. 1

1. unzip -l config.zip

Copied!

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

1. 1

1. unzip bin.zip

Copied!

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:

1. 1

1. ls -l usdoi.txt

Copied!

A sample output looks like:

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


about:blank 11/15
03.09.2023, 10:09 about:blank
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.

1. 1

1. chmod -r usdoi.txt

Copied!

Verify the changed permissions.


1. 1

1. ls -l usdoi.txt

Copied!

Add read access to all on usdoi.txt.

1. 1

1. chmod +r usdoi.txt

Copied!

Verify the changed permissions.

1. 1

1. ls -l usdoi.txt

Copied!

To remove the read permission for ‘others’ category.

1. 1

1. chmod o-r usdoi.txt

Copied!

Verify the changed permissions.


1. 1

1. ls -l usdoi.txt

Copied!

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’.
1. 1

1. wc usdoi.txt

Copied!

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’.

1. 1

1. wc -l usdoi.txt

Copied!

about:blank 12/15
03.09.2023, 10:09 about:blank
Print only the number of words in ‘usdoi.txt’.
1. 1

1. wc -w usdoi.txt

Copied!

Print only the number of characters in ‘usdoi.txt’.

1. 1

1. wc -c usdoi.txt

Copied!

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.

1. 1

1. grep people usdoi.txt

Copied!

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.

1. 1

1. grep -v login /etc/passwd

Copied!

Exercise 6 - Networking commands


6.1. Show the system’s host name

hostname

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

1. 1

1. hostname

Copied!

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

1. 1

1. hostname -i

Copied!

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)

1. 1

1. ping www.google.com

Copied!

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

1. 1

about:blank 13/15
03.09.2023, 10:09 about:blank
1. ping -c 5 www.google.com

Copied!

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:

1. 1

1. ifconfig

Copied!

Display the configuration of the ethernet adapter.

1. 1

1. ifconfig eth0

Copied!

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.
1. 1

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

Copied!

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

1. 1

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

Copied!

Practice exercises
1. Problem:

Display the content of /home directory.

Click here for Hint


Click here for Solution

2. Problem:

Ensure that you are in your home directory.

Click here for Hint


Click here for Solution

3. Problem:

Create a new directory called ‘final’ in your home directory.

Click here for Hint


Click here for Solution

4. Problem:

View the permissions of the newly created directory ‘final’.

Click here for Hint


Click here for Solution

5. Problem:

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

Click here for Hint

about:blank 14/15
03.09.2023, 10:09 about:blank
Click here for Solution

6. Problem:

Copy display.sh as report.sh.

Click here for Hint


Click here for Solution

7. Problem:

Delete the file ‘display.sh’.

Click here for Hint


Click here for Solution

8. Problem:

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

Click here for Hint


Click here for Solution

9. Problem:

Display the current time.

Click here for Hint


Click here for Solution

10. Problem:

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

Click here for Hint


Click here for Solution

11. Problem:

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

Click here for Hint


Click here for Solution

12. 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


Click here for Solution

Authors
Ramesh Sannareddy

Other Contributors
Rav Ahuja

Change Log
Date (YYYY-MM-DD) Version Changed By Change Description
2023-05-04 0.2 Benny Li Minor formatting changes
2021-05-30 0.1 Ramesh Sannareddy Created initial version of the lab

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

about:blank 15/15

You might also like