0% found this document useful (0 votes)
88 views

Lab 2: Exploring The Use of Different Linux Commands To Use The OS Through Terminal

The document discusses various Linux commands used to control the OS through the terminal. It introduces basic commands like echo, clear, sudo, man & help for working with files, directories, permissions, zipped files and installing packages. It provides the syntax and explanation of commands like touch, nano, cp, mv, rm, cat, locate, mkdir, rmdir, chmod, tar etc. It also includes tasks to use these commands to locate, create, copy, move, delete files and directories and compress/uncompress folders.

Uploaded by

Mehran Shabeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Lab 2: Exploring The Use of Different Linux Commands To Use The OS Through Terminal

The document discusses various Linux commands used to control the OS through the terminal. It introduces basic commands like echo, clear, sudo, man & help for working with files, directories, permissions, zipped files and installing packages. It provides the syntax and explanation of commands like touch, nano, cp, mv, rm, cat, locate, mkdir, rmdir, chmod, tar etc. It also includes tasks to use these commands to locate, create, copy, move, delete files and directories and compress/uncompress folders.

Uploaded by

Mehran Shabeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab 2: Exploring the use of different Linux commands to use the OS

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

3. man & --help


To know more about a command and how to use it, use the man command. It shows the manual
pages of the command. For example, “man cd” shows the manual pages of the cd command.
Typing in the command name and the argument helps it show which ways the command can be
used (e.g., cd –help).
Commands for Working with Files
4. touch 
The touch command is used to create a file. It can be anything, from an empty txt file to an
empty zip file. For example, touch new.txt
5. nano
The nano command is used to open and edit a text file.
syntex:
$ nano <filename.txt>
After editing the file press CTRL+O to save the changes and then press CTRL+X to exit.
6. cp
Use the cp command to copy files through the command line. It takes two arguments: The first is
the location of the file to be copied, the second is where to copy.
syntax:
$ cp <flag> {filename} /pathname/
Command Explanation
Enters interactive mode; CLI asks before
cp -i
overwriting files

cp -n Does not overwrite the file

Updates the destination file only when the


cp -u
source file is different from the destination file
Recursive copy for copying directories; Copies
cp -R
even hidden files
7. mv
Use the mv command to move files through the command line. We can also use the mv
command to rename a file. For example, if we want to rename the file “text” to “new”, we can
use “mv text new”. It takes the two arguments, just like the cp command.
syntax:

$ mv <flag> {filename} /pathname/

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

rm –r Removes even non-empty directories.

Removes non-empty directories including


rm –rp
parent and subdirectories.

9. cat

This command can read, modify or concatenate text files. It also displays file contents.

syntax:

$ cat <flag> {filename}

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

Commands for Working with Directories

11. mkdir

This command is used to create a new directory.

syntax:

$ mkdir <flag> {directoryname} /pathname/

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:

$ rmdir <flag> {directoryname} 


Command Explanation
rmdir –p Removes both the parent and child directory
Removes all the parent and subdirectories
rmdir –pv
along with the verbose.

Commands for Working with User Permissions

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}

The permissions associated with each digit is as follows.


Number Read write  execute
0 – – –
1 – – yes
2 – yes –
3 – yes yes
4 Yes – –
5 Yes – yes
6 Yes yes –
7 Yes yes yes

Commands for Installing Packages


Stable versions of most software’s will already be available in Linux repositories. Here are the
Linux Commands to install them.
14. install packages
For an RHEL based system;
syntax:
$ sudo yum install package-name
For a Debian/Ubuntu based system;
syntax:
$ sudo apt-get install package-name
For a Fedora based system;
syntax:
$ sudo dnf install package-name
Commands for Working with Zipped Files
When you download a package from the internet, the downloaded file comes in compressed
form. Here are a few commands to decompress and compress files in Linux.
15. tar
The following command is used to zip files of .tar format.
syntax:
$ tar –cvf tar-filename source-folder-name
The following command is used to unzip files of .tar format.
syntax:
$ tar –xvf tar-file-name
Task 1: Locate a file named passwd using appropriate command.
Task 2:
 Create a text file named “my_info.txt”.
 Open it and enter your name, registration number and age.
 Save it.
 Copy it to the Documents Directory.
 Delete the original file.
na

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

You might also like