linux_terminal_commands
linux_terminal_commands
Objectives
In this lab, you will be introducted to the use basic Unix commands related to the following categories:
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.)
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.
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.
date
1. date
Copied!
It has several options which help you get date in your favourite format.
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
ls
1. ls
Copied!
The following command will list all the files in the /bin directory.
1. 1
1. ls /bin
Copied!
1. ls /bin/b*
Copied!
1. ls /bin/*r
Copied!
uname
1. uname
Copied!
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!
ps
ps lists the processes that are currently running and their PIDs (process ids).
1. 1
1. ps
Copied!
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
It shows the summary information of the system and the list of processes or threads which are currently managed by the Kernel.
1. top
Copied!
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.
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 :
N - sort by process ID
echo
Copied!
Use the -e option of the echo command when working with special characters.
1. 1
Copied!
about:blank 4/15
03.09.2023, 10:09 about:blank
1.9. Download a file from the internet.
wget
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!
1. 1
1. ls
Copied!
man
man command displays the user manual of the command given as argument.
1. 1
1. man ls
Copied!
pwd
1. 1
1. pwd
Copied!
mkdir
To create a directory named ‘scripts’ in your current directory, run the following command.
1. 1
1. mkdir scripts
Copied!
1. ls
Copied!
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!
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!
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!
rmdir
1. mkdir /tmp/dummy
Copied!
1. 1
1. ls /tmp
Copied!
1. rmdir /tmp/dummy
Copied!
1. 1
1. ls /tmp
Copied!
Note: Before removing any directory make sure that you do not have any files or sub directories.
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
Copied!
Along with the listing of txt files, you may get some Permission denied errors.
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!
cat
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.
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!
more
1. more usdoi.txt
Copied!
head
1. head usdoi.txt
Copied!
1. head -3 usdoi.txt
Copied!
tail
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.
1. tail -2 usdoi.txt
Copied!
cp
1. cp usdoi.txt usdoi-backup.txt
Copied!
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!
1. ls
Copied!
mv
If the source and target directories are same, it works like rename operation.
1. mv users.txt user-info.txt
Copied!
1. 1
1. ls
Copied!
1. mv user-info.txt /tmp
Copied!
1. 1
1. ls
Copied!
1. 1
1. ls -l /tmp
about:blank 9/15
03.09.2023, 10:09 about:blank
Copied!
touch
1. touch myfile.txt
Copied!
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!
1. ls -l
Copied!
rm
The rm command is ideally used along with the -i option, which makes it ask for confirmation before deleting.
1. rm -i myfile.txt
Copied!
1. ls -l
Copied!
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.
Copied!
1. 1
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
Copied!
1. ls -l
Copied!
zip
The following command creates a zip named config.zip and of all the files with extension .conf in the /etc directory.
1. 1
Copied!
1. 1
Copied!
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!
Permission symbol
read r
write w
execute x
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!
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!
1. ls -l usdoi.txt
Copied!
1. 1
1. chmod +r usdoi.txt
Copied!
1. 1
1. ls -l usdoi.txt
Copied!
1. 1
Copied!
1. ls -l usdoi.txt
Copied!
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.
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!
1. 1
1. wc -c usdoi.txt
Copied!
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
Copied!
| 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
Copied!
hostname
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!
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!
ifconfig
1. 1
1. ifconfig
Copied!
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.
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:
2. Problem:
3. Problem:
4. Problem:
5. Problem:
about:blank 14/15
03.09.2023, 10:09 about:blank
Click here for Solution
6. Problem:
7. Problem:
8. Problem:
List the files in /etc directory in the ascending order of their access time.
9. Problem:
10. Problem:
11. Problem:
Display the lines that contain the string ‘not installed’ in /var/log/bootstrap.log page-wise.
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.
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
about:blank 15/15