Lab 2: Exploring The Use of Different Linux Commands To Use The OS Through Terminal
Lab 2: Exploring The Use of Different Linux Commands To Use The OS Through Terminal
through terminal
Lab Objective: To introduce students with different Linux commands to control the OS through
terminal.
There are too many commands that a Linux terminal understands. You can do each and
everything on your Linux OS using these commands. Linux commands are usually sorted into
following different categories.
Linux Basic Commands
Commands for Working with Files
Commands for Working with Directories
Commands for Working with User Permissions
Commands for Working with Zipped Files
Linux Basic Commands
1. echo
This command writes its arguments to standard output.
syntax:
$ echo "<text>"
2. clear
This command is used to clear the terminal screen. Contents will not actually be deleted in this
case, only scrolled down. You can also clear the screen by pressing Ctrl+L on the keyboard.
syntax:
$ clear
3. sudo
This command executes only that command with root/ superuser privileges.
syntax:
$ sudo <command>
Command Explanation
sudo useradd <username> Adding a new user
sudo passwd <username> Setting a password for the new user
sudo userdel <username> Deleting the user
sudo groupadd <groupname> Adding a new group
sudo groupdel <groupname> Deleting the group
sudo usermod -g <groupname> <username> Adding a user to a primary group
8. rm
This command removes files from a directory. By default, the rm command does not remove
directories. Once removed, the contents of a file cannot be recovered.
syntax:
$ rm <flag> {filename}
Command Explanation
9. cat
This command can read, modify or concatenate text files. It also displays file contents.
syntax:
Command Explanation
cat -b This is used to add line numbers to non-blank lines
cat -n This is used to add line numbers to all lines
cat -s This is used to squeeze blank lines into one line
cat –E Show $ at the end of line
10. locate
The locate command is used to locate a file in a Linux system, just like the search command in
Windows. This command is useful when you don't know where a file is saved or the actual name
of the file.
Using the -i argument with the command helps to ignore the case (it doesn't matter if it is
uppercase or lowercase). So, if you want a file that has the word “hello”, it gives the list of all the
files in your Linux system containing the word "hello" when you type in “locate -i hello”. If you
remember two words, you can separate them using an asterisk (*). For example, to locate a file
containing the words "hello" and "this", you can use the command “locate -i *hello*this”.
11. mkdir
syntax:
Command Explanation
Creates both a new parent directory and a sub-
mkdir -p
directory
This is used to create multiple subdirectories
mkdir –p <filename1>/{f1,f2,f3}
inside the new parent directory
12. rmdir
This command is used to remove a specified directory. Although by default, it can only remove
an empty directory, there are flags which can be deployed to delete the non-empty directories as
well.
syntax:
13. chmod
This command is used to change the access permissions of files and directories. Consider the
example below.
On trying to run the newly created file named chmodtest.sh, an error is thrown. After modifying
the permissions of the file using the said Linux command, it turns executable.
syntax:
$ chmod <permissions of user,group,others> {filename}
Task 3:
Use nano command to create a file named “file1” and write Hello world.
Use touch command to create another file named “file2”.
Create a directory named folder1 and move both files to the directory using appropriate
command.
Compress the directory folder1.
Task 4:
Unzip the directory in task 3.
Delete the file named “file2” and leave the file1 as it is.
Show what is written in the file1.
Delete the directory containing “file1”.