0% found this document useful (0 votes)
21 views11 pages

Linux Command Basics for Students

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)
21 views11 pages

Linux Command Basics for Students

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

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 [Link]
5. nano
The nano command is used to open and edit a text file.
syntex:
$ nano <[Link]>
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 [Link], 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”.

Common questions

Powered by AI

The 'sudo' command in Linux is significant because it allows a permitted user to run a command as the superuser or another user, as specified in the sudoers file. This is crucial for executing tasks that require higher privileges, such as installing software or modifying system configuration files. For example, creating a new user with 'sudo useradd <username>' or installing packages with 'sudo apt-get install package-name' in Debian-based systems . Using 'sudo' helps safeguard system integrity by ensuring that only authorized users can perform critical operations, thereby preventing unauthorized users from making potentially harmful changes.

The 'mv' command in Linux can be used both to move files to a different location and to rename them. For moving files, the command follows the syntax '$ mv <source-filename> <destination-directory>', which relocates the file to a new directory . For renaming a file, the same command is used but with the desired new file name as the second operand, such as '$ mv oldfile.txt newfile.txt', which changes the file's name from 'oldfile.txt' to 'newfile.txt' . This dual functionality makes 'mv' a versatile tool in file management.

The 'chmod' command in Linux is used to change access permissions of files and directories. These permissions are designated by a three-digit number, where each digit represents a different set of users: the owner, the group, and others. The possible permissions are read, write, and execute, signified by numbers 4, 2, and 1, respectively. Summing these numbers provides the numeric representation of permissions. For example, a permission set of 7 (4+2+1) denotes all permissions (read, write, execute) are granted. Permissions like 4 only give read access, and 5 (4+1) gives read and execute rights . This method allows precise control over who can access and modify files.

The 'man' command in Linux enhances command usability by providing comprehensive manual pages for any command, covering detailed descriptions, options, and examples of how to use the command effectively. For instance, running 'man cd' brings up the manual for the 'cd' command . This can be advantageous over using '--help', which typically provides concise, but less detailed, information about the command's options and usage without examples. Therefore, 'man' is more suited for in-depth learning and understanding of command functionalities, offering a broader overview and background than '--help'.

The 'nano' command in Linux serves as a text editor for creating and editing text files directly from the terminal. It opens files and allows users to edit content with various commands for saving changes and exiting, such as pressing CTRL+O to save and CTRL+X to exit . In contrast, the 'touch' command is used to create new, empty files without opening them for editing. It serves to initialize files rather than modify them. Thus, while 'nano' is intended for direct content editing, 'touch' serves for file creation and timestamp management.

The 'cat' command in Linux is useful for working with text files because it can read, concatenate, and output file contents to the terminal. It allows quick viewing and modification of file content. The '-b' option of 'cat' adds line numbers to non-blank lines only, making it useful for inspecting files where the differentiation between content and spacing is important. On the other hand, the '-n' option adds numbers to all lines, including blank ones, which is useful for referencing specific lines of code or text within a document . These options provide added utility for file analysis and debugging.

To search for a file across a Linux system when the exact file name is unknown, the 'locate' command can be used. This command is similar to performing a search in Windows and is effective even if the full file name is not known. The use of the '-i' flag in 'locate' command makes the search case-insensitive, meaning it will not differentiate between uppercase and lowercase letters . For example, using 'locate -i hello' will list all files containing 'hello' in any combination of cases.

Using 'sudo yum install' would be applicable on systems based on Red Hat Linux (RHEL) and its derivatives, such as CentOS and Fedora, which use the YUM package manager for handling software installations . On the other hand, 'sudo apt-get install' is used for Debian-based systems like Ubuntu, employing the APT package handling utility. The choice between these commands implies reliance on different repositories and may reflect variations in the available package versions and dependencies. Overall, it emphasizes the need to understand the underlying system environment and available package management tools when installing software on different Linux distributions.

The 'rm' command in Linux is used to remove files from a directory, and it does not remove directories by default, but it can do so when used with the '-r' or '-rp' flags, allowing it to remove non-empty directories and their subdirectories . On the other hand, 'rmdir' is specifically used to remove directories but can only remove empty directories unless flags like '-p' or '-pv' are used to remove parent and child directories, including verbose output . 'rm' would be used when you need to delete files or clear a non-empty directory, while 'rmdir' would be used for cleaning up empty directories.

The 'tar' command in Linux is appropriate for creating package archives, often in .tar format, and is used to compress multiple files or entire directories into one archive file, and it can also extract files from such archives. Commands like '$ tar –cvf archive.tar folder/' create a .tar archive, while '$ tar –xvf archive.tar' extracts files from it . Unlike other compression commands that may focus on compressing files into specific formats (like zip or gz), 'tar' combines files into one archive which can then be optionally compressed using utilities like 'gzip' or 'bzip2'. This makes 'tar' particularly suitable for system backups and data transfer, emphasizing portability and ease of access over individual compression strength.

You might also like