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

Linux Unit2

The document provides a comprehensive overview of Linux, covering essential commands, shell types, text editors, file system structure, and file permissions. It details various Linux commands for file management, introduces popular shells like Bash and Ksh, and explains text editors such as Vim and Nano. Additionally, it outlines the Linux file system hierarchy, directory purposes, and the importance of file permissions for security and access control.

Uploaded by

paddeytchicken
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 Unit2

The document provides a comprehensive overview of Linux, covering essential commands, shell types, text editors, file system structure, and file permissions. It details various Linux commands for file management, introduces popular shells like Bash and Ksh, and explains text editors such as Vim and Nano. Additionally, it outlines the Linux file system hierarchy, directory purposes, and the importance of file permissions for security and access control.

Uploaded by

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

Linux

Mr. Ranjan Raj Regmi

2024/04/08 1
Linux

2.Basic of Linux
• Linux Commands:
1. ls (List): List files and directories in the current directory.
Example: ls –l (Long format listing), ls –a (List hidden files),
ls /path/to/directory (List files in a specific directory)

2. pwd (Print Working Directory): Display the current working


directory.
Example: pwd

3. cd (Change Directory): Change the current directory.


Example : cd /path/to/directory

4. mkdir (Make Directory): Create a new directory.


Example : mkdir my_directory

2
Linux

• Linux Commands:

5. rmdir(Remove Directory): Remove an empty directory.


Example: rmdir my_directory

6. touch: Create an empty file or update the timestamp of an existing file.


Example: touch myfile.txt

7. rm (Remove): Remove files and directories.


Example : rm myfile.txt (Remove a file), rm –rf my_directory (-r means Remove a
directory and its contents if means forcefully)

8. cp (Copy): Copy files or directories.


Example : cp file1.txt file2.txt (Copy a file), cp –r directory1/directory2/ (Copy a
directory and its contents)

3
Linux

• Linux Commands:

9. Mv (Move/Rename): Move or rename files or directories.


Example: mv file1.txt newfile.txt (Rename a file), mv file.txt /new/location/ (Move a file)

10. Cat (Concatenate): Display the content of a file.


Example: cat myfile.txt

11. chmod (Change Mode): Change file permissions.


Example : chmod 755 myfile.sh (Give read, write, and execute permissions to the owner,
and read and execute permissions to others)

12. chown (Change Owner): Change the owner of a file or directory.


Example : chown user:group myfile.txt

13. Df (Disk Free): Display disk space usage.


Example: df –h(Display usage in human-redable format)

4
Linux

• Linux Commands:

14. Mv (Move/Rename): Move or rename files or directories.


Example: mv file1.txt newfile.txt (Rename a file), mv file.txt /new/location/ (Move a file)

15. Cat (Concatenate): Display the content of a file.


Example: cat myfile.txt

16. chmod (Change Mode): Change file permissions.


Example : chmod 755 myfile.sh (Give read, write, and execute permissions to the owner, and read and
execute permissions to others)

17. chown (Change Owner): Change the owner of a file or directory.


Example : chown user:group myfile.txt

18. du (Disk Usage): Display the content of a file.


Example: du –sh /path/to/directory

5
Linux

Shell
Shells are command-line interfaces (CLIs) or command interpreters in Linux and Unix-like
operating systems. They provide users with a way to interact with the system by entering
commands and scripts. Each shell has its own features and syntax.
Three popular shells in Linux:
1. Cch (C Shell):
• History: Csh was developed by Bill Joy at the University of California, Berkeley, in the late 1970s.
It was designed to resemble the C programming language syntax.
• Features:
• C-like syntax: Csh uses a syntax similar to the C programming language, which may be more
familiar to programmers with a C background.
• History substitution: Csh provides the ability to recall and reuse previous commands using
history substitution.
• Scripting capabilities: While not as popular as Bash for scripting, Csh can be used for writing
shell scripts.
• Use Cases: Csh is less commonly used today as a default interactive shell but may still be found
on some systems. It's mostly used by those who are already familiar with it or for maintaining
legacy scripts. 6
Linux

Shell
2. Ksh (Korn Shell):
• History: Ksh was developed by David Korn at AT&T Bell Laboratories in the early 1980s.
It was designed as an improved version of the original Unix shell (sh).
• Features:
• Rich scripting: Ksh is known for its powerful scripting capabilities, making it a popular
choice for writing complex shell scripts.
• Command-line editing: Ksh offers advanced command-line editing features, including
vi and emacs modes.
• Job control: Ksh provides extensive job control features for managing and
manipulating background and foreground processes.
• Use Cases: Ksh is often used by system administrators and script developers due to its
scripting capabilities and extended features.

7
Linux

Shell
3. Bash (Bourne-Again Shell):
•History: Bash, as the name suggests, is an evolution of the Bourne Shell (sh), which was the
original Unix shell. It was created by Brian Fox for the GNU Project in 1989.
•Features:
•Wide adoption: Bash is the default shell on most Linux distributions and macOS, making
it one of the most widely used shells.
•Scripting: Bash is highly regarded for its scripting capabilities and is the go-to choice for
writing shell scripts.
•Command-line editing: Bash provides intuitive command-line editing with features like
history recall, tab completion, and more.
•Extensibility: Users can customize their Bash environment with aliases, functions, and
configuration files like .bashrc.
•Use Cases: Bash is the most commonly used shell for interactive sessions, scripting, and
automation tasks on Linux and Unix systems. It is versatile and well-documented, making it
a great choice for users of all levels.
8
Linux

Shell
Differences

Feature Csh Ksh Bash


Syntax C-like syntax C-like syntax C-like syntax
Scripting Limited scripting capabilities Strong scripting capabilities Strong scripting capabilities
Advanced (history, tab
Command-Line Editing Basic Advanced (vi/emacs modes)
completion, etc.)
Job Control Limited Extensive Extensive
Aliases Limited support Supported Supported
Customization Limited Supported Supported
Most commonly used for
Interactive Use Less common Used for interactive sessions
interactive sessions
Not commonly used as Used as default on some Default on most Linux
Default Shell
default systems distributions
History Substitution Supported Supported Supported
Scripting Popularity Less popular for scripting Popular for scripting Very popular for scripting

9
Linux

Additional Shell in Linux

1. Dash (Debian Almquist Shell):


•Dash is a lightweight POSIX-compliant shell that is designed for efficiency
and is often used as the default /bin/sh on many Linux distributions due
to its speed.
2. Zsh (Z Shell):
•Zsh is an interactive shell with advanced features like enhanced tab
completion, spelling correction, and extensive customization options. It is
known for its user-friendly interface and is often used as an alternative to
Bash.

10
Linux

Text Editors
Text editors are essential tools for creating and editing text files in a variety of environments, including
the command line. Here are detailed explanations of three popular text editors: Vim, Pico, and Nano.

1. Vim (Vi IMproved):


•Description: Vim is a highly configurable and powerful text editor that is based on the classic Vi
editor. It is known for its efficiency, extensibility, and wide range of features. Vim provides different
modes, including normal mode (for navigation and commands), insert mode (for inserting text), and
command-line mode (for executing commands).
•How to Use:
•To open a file with Vim: vim filename
•Navigation: Use the keyboard shortcuts or commands in normal mode (e.g., h, j, k, l, arrow keys).
•Insert text: Press i to enter insert mode, type your text, and press Esc to return to normal mode.
•Saving and quitting:
•:w to save changes.
•:q to quit.
•:wq to save and quit.
•Use Cases:
•Vim is popular among programmers, system administrators, and power users.
•It's suitable for editing code, configuration files, and other text-based documents.
11
•Vim is highly customizable and supports various plugins and extensions.
Linux

2. Pico:
•Description: Pico is a simple and user-friendly text editor that is part of the
Pine email client suite. It's designed for basic text editing tasks and is
characterized by its straightforward and minimalistic interface.
•How to Use:
•To open a file with Pico: pico filename
•Navigation: Use arrow keys to move around the text.
•Editing: Simply start typing to add or modify text.
•Saving and quitting:
•Press Ctrl + O to save changes.
•Press Ctrl + X to exit Pico.
•Use Cases:
•Pico is an excellent choice for beginners or those who prefer a
straightforward and intuitive text editor.
•It's suitable for quick edits, composing emails, or making simple changes
to text files.
12
Linux

3. Nano:
•Description: Nano is a user-friendly and easy-to-use text editor known for its
simplicity and accessibility. It provides a simple interface for basic text editing
tasks without the complexity of more advanced editors like Vim.
•How to Use:
•To open a file with Nano: nano filename
•Navigation: Use arrow keys to move the cursor.
•Editing: Start typing to add or modify text.
•Saving and quitting:
•Press Ctrl + O to save changes.
•Press Ctrl + X to exit Nano.
•Use Cases:
•Nano is suitable for users who want a straightforward text editor without
the need to learn extensive commands.
•It's useful for quick text edits, viewing files, or performing simple text-
related tasks.
13
Linux

Linux File System Structure

• Linux file system has a hierarchal file structure as it contains a root directory and its
subdirectories. All other directories can be accessed from the root directory. A partition
usually has only one file system, but it may have more than one file system.
• A file system is designed in a way so that it can manage and provide space for non-volatile
storage data. All file systems required a namespace that is a naming and organizational
methodology. The namespace defines the naming process, length of the file name, or a
subset of characters that can be used for the file name. It also defines the logical structure
of files on a memory segment, such as the use of directories for organizing the specific files.
Once a namespace is described, a Metadata description must be defined for that particular
file.

14
Linux

15
Linux

Directories and their special purpose

•/boot: Contains boot loader files, including the Linux kernel and related boot files.
•/etc: Stores configuration files for the system and various programs.
•/home: User home directories, where individual users store their files and personal settings.
•/root: The home directory for the root (administrative) user.
•/opt: Contains third-party applications, usually not managed by the system's package manager.
•/dev: Houses device files that represent system hardware (e.g., disks, terminals).
•/var: Stores variable files such as logs, spools, and caches that change dynamically during system
operations.
•/bin: Essential user binaries like basic command-line utilities (e.g., ls, cat).
•/sbin: System binaries, mainly for system administration tasks, usually requiring root privileges.
•/usr: Contains user applications and utilities, along with their libraries and documentation.
•/proc: Virtual filesystem providing process and kernel information.
•/mnt: Used to temporarily mount filesystems, such as removable media or network filesystems.
•/sys: Virtual filesystem that represents kernel objects, providing access to kernel settings and system
hardware information.
•/media: Directory for mounting removable media like CDs, USB drives, and external devices.
•/run: Contains runtime data used during the system’s operation (e.g., system services).
•/tmp: Temporary files, which are deleted either periodically or upon reboot.
•/lost+found: Directory used by the fsck utility to recover files that may become corrupted or lost
during a crash.
•/lib: Contains essential shared libraries needed by the binaries in /bin and /sbin.
•/srv: Service data directory where files related to specific services (like web or FTP servers) are stored.

16
Linux

Permission

Linux file permissions are essential for controlling access to files and directories, ensuring
the security and integrity of the system. Users and administrators must understand how to
set, view, and modify permissions to manage file access effectively. Linux file permissions
are a crucial aspect of the operating system's security model. They determine who can
access, modify, and execute files and directories on a Linux system. File permissions are
primarily defined for three categories: the owner of the file, the group associated with the
file, and others (everyone else).

17
Linux

File Permission Categories:

1.Owner (User): The user who owns the file or directory. This user has the most control over
the file, including the ability to change its permissions.
2.Group: Files and directories can be associated with a specific group. All users belonging to
that group share the group's permissions for that file.
3.Others (World): Everyone else who is not the owner or a member of the group falls into this
category.

File Permissions:

Linux file permissions are represented using a combination of letters and symbols:

r: Read permission (can view the file's contents or list directory contents).
w: Write permission (can modify or delete the file or create new files in a directory).
x: Execute permission (can run a program or script if it is marked as executable).
These permissions are assigned to each category (owner, group, others) for each file or
directory.
18
Linux

• Permission Settings:
File permission settings are often displayed in the following format:
-rw-r--r—

In this example, the file has the following permissions:

• The owner has read (r) and write (w) permissions.


• The group has read (r) permission.
• Others have read (r) permission.

19
Linux

Numeric Representation:
File permissions can also be represented numerically using octal (base-8) values:

4 corresponds to read (r) permission.


2 corresponds to write (w) permission.
1 corresponds to execute (x) permission.
Using numeric representation, you can set permissions for each category (owner, group,
others) and combine them to create a three-digit number. For example:

644 represents -rw-r--r-- (Owner can read and write, group and others can only read).
755 represents -rwxr-xr-x (Owner can read, write, and execute; group and others can read and
execute).
20
Linux

• Changing File Permissions:

You can change file permissions using the chmod command. For example, to give the owner write permission
on a file, you can use:

- chmod u+w filename

21
Linux

File Ownership:
Linux also assigns an owner and group to each file and directory. You can view ownership using the ls -l
command. To change ownership, use the chown command, and to change group ownership, use chgrp.

Permission Symbol Numeric Value

File Permission Table:


Read r 4

Here's a table representing the file permission settings:


Write w 2

Execute x 1

22
Linux

Example Permissions:

In the permission string drwxr-x---, the first


letter, "d" stands for "directory." It indicates Permission
Owner Group Others
that the entry to which these permissions Settings

apply is a directory. The "d" is a special


character that distinguishes directories from
regular files in a Linux file listing. -rw-r--r-- 4 4 4

If you don't use the "d" and only have the -rwxr-xr-x 7 5 5
permission string like rwxr-x--- it would be
interpreted as permissions for a regular file,
not a directory. In other words, it would drwxr-x--- 7 5 0
indicate that the file has the following
permissions:

23
Linux

Example Permissions:

The owner can read, write, and execute. Members of the group can read and execute. Others
(world) have no permissions (cannot read, write, or execute).

Including the "d" is essential to convey that these permissions are meant for a directory. This
distinction is crucial because directories and regular files serve different purposes and have
different operations associated with them. For example:

Reading a directory (r permission for a directory) allows you to list its contents.
Writing to a directory (w permission for a directory) permits you to create, delete, or rename files
and subdirectories within it.
Executing a directory (x permission for a directory) is required to change into (cd) that directory
or access its contents.
For regular files, the same "rwx" permissions control reading, writing, and executing the file
itself. 24

You might also like