lecture 2
lecture 2
● The environment variables are dynamic values that are stored within a
system and used by applications launched in shells or sub-shells.
● The environment is the track for a computer application to interact with the
system.
● The environment variable can have information about the default
applications of the system, the system locale, the path of the executable file
and the keyboard layout setting, and more.
Common Environment Variables
1. PATH: This variable contains a list of directories in which our system
looks for files. It separates directories by a (:) colon.
2. USER: This variable holds the username.
3. HOME: This variable holds the default path to the user's home directory.
4. EDITOR: This variable contains the path to the specified editor.
5. UID: This variable contains the path to the user's unique id.
6. TERM: This variable contains the path to the default terminal emulator.
7. SHELL: This variable contains the path to the default shell that is being
used by the user.
Page - 4
Common Environment Variables
•ENV: This variable displays all the environment variable.
Page - 5
Set Environment Variable in Linux
● There are multiple commands in Linux that allow us to set and create the
environment variable.
● Use the export command to set a new environment variable.
● To create a new variable, use the export command followed by a variable name and
its value.
● Syntax:
Page - 6
Removing an Environment Variable
Page - 7
File Management
• All data in linux is organized into files. All files are organized into directories.
In linux, there are three basic types of files
a. Ordinary Files − An ordinary file is a file on the system that contains data,
text, or program instructions. In this tutorial, you look at working with
ordinary files.
b. Directories − Directories store both special and ordinary files. For users
familiar with Windows or Mac OS, Unix directories are equivalent to folders.
Page - 8
File management
Page - 9
Understanding the File System Tree
Page - 10
Important directories
Directory Purpose
/ The root of the primary filesystem hierarchy
/bin Contain essential command binaries (user executables)
Page - 12
Gathering File System Information df Command
• The df command reports the system's disk space usage. By default, df shows the partition size
in 1 kilobyte blocks and the amount of used and available disk space in kilobytes.
Page - 13
Command df -h
Page - 14
Types of Files in Linux
Page - 15
File Management Commands in Linux
cp source.txt destination.txt
● Copying Files and Directories cp -r source_dir destination_dir # Copy directories recursively
The cp command is used for copying.
mv oldname.txt newname.txt # Rename
mv file.txt /home/user/Documents/ # Move
● Moving and Renaming Files
Use mv for both moving and renaming.
rm file.txt
● Removing Files and Directories rm -r directory # Remove directory and its contents
Be careful with these commands –
cat file.txt # Display entire file
less file.txt # View file page by page
● Viewing File Contents head -n 5 file.txt # View first 5 lines
There are several ways to view file contents. tail -n 5 file.txt # View last 5 lines
Page - 17
Creating Parent Directories
● If you want to create a directory within a directory that doesn't exist yet?
The -p option has got you covered.
mkdir -p Projects/linux/ File.txt
This creates the entire path, even if the parent directories don't exist.
● Removing Directories
Sometimes, we need to do some pruning. To remove an empty directory, use
rmdir MyNewdir
- If you want to remove a directory and all its contents, use rm -r, but use this with
caution: rm -r Projects
Page - 18
Renaming Directories
• Renaming Directories
In Unix/Linux, we don't have a separate "rename" command. Instead, we use
the mv command: mv oldname newname
The directories . (dot) and .. (dot dot)
Let's finish with two special directory names:
• . (single dot) represents the current directory
• .. (double dot) represents the parent directory
These are incredibly useful in relative pathnames. For example: cp ../file.txt .
This copies a file named "file.txt" from the parent directory to the current directory
Page - 19
Absolute/Relative Pathnames
• Absolute Pathnames
An absolute pathname starts from the root directory (/). It's like giving someone your full address,
including the country, city, and street name.
Example: cd /home/username/Documents
• Relative Pathnames
A pathname can also be relative to your current working directory.
Relative pathnames never begin with /.
• CD - ➔ PREVIOUS PATH
Example:
• CD == CD ~ “DELTA” ➔ RETURN TO HOME
cd Documents
If you're already in your home directory, this will take you to the Documents directory.
Page - 20
File Permissions and Ownership
Page - 21
Displaying permissions
• Permissions are displayed with ls -l command:
● -rwxr-xr-x 1 root root 118644 Apr 4 2024 Desktop
● First character: file type (- for file, d for directory)
● Next 9 characters: permissions
● Next 1 character: # of links the file has to other files
● 1st “root”: file owner,
● 2nd “root”: the primary group name the owner belongs to
● 118644: file size in bytes
● Apr 4 2014 shows the date and time on which the file was recently modified.
● Last column shows the file or directory name.
• ls -d */
If you only want to display the sub-directories excluding all other files.
Page - 22
Permission sets
• Permissions are broken into three sets:
○ - The first set for the user who owns the file displayed (rwxr-xr-x)
○ - The second set for the group that owns the file displayed (rwxr-xr-x)
○ - The last set for everyone else (rwxr-xr-x)
• The term "everyone else" means anyone who is not the user that owns the file or a
member of the group that owns the file.
• To display the file size of your list in a particular format or size, then you can use this
command:
•K = Kilobyte M = Megabyte
•G = Gigabyte T = Terabyte
•P = Petabyte E = Exabyte
Page - 23
Changing File Ownership
• When a file or directory is created, the owner is automatically assigned using
the effective user ID at the time of creation
• The chown command is used to change the ownership of a file or directory
• File owner can only be changed by a user with root privileges:
Page - 24
Changing the Primary Group
• When a file or directory is created, the primary group of the effective user ID
creating it is automatically assigned
• The chgrp command is used to change the primary group of a file or directory
• The primary group can only be changed by the file owner and a user with
root privileges:
● # ls -l testfile
● -rw-rw-r-- 1 user1 user1 212 Apr 4 2014 testfile
● # chgrp admins testfile
● # ls -l testfile
● -rw-rw-r-- 1 user1 admins 212 Apr 4 2014 testfile
Page - 25
Understanding Permissions
○ - If you are the root user, your permissions would be “rwx”, or read, write, and
execute
○ - If you are not the root user, but are a member of the root group, your permissions
would be "r-x", or read and execute
○ - If you are not the root user or a member of the root group, then your permissions
would be "r-x", or read and execute
Page - 26
Changing Permissions
• The chmod command is used to change the permissions of a file or directory
• To change the permissions of a file, you must either be the user who owns the file or
the root user
• Two methods used to change:
● - Symbolic (relative) method – uses a combination of letters and symbols to add or
remove
● - Octal (numeric) method – uses three numbers to represent file permissions for
owner, group, everyone else
Page - 27
The symbolic Method
• First, specify who (u, g, o, a):
Symbol Meaning
u The user who owns the file
g The group who owns the file
o People other than the user owner or member of the group owner (others)
chmod o=r,g-w,u+x myscript Assigns others read, removes write from the group
owner and adds execute permission from the user
owner
chmod a=- file1 Assign everyone no permission
Page - 29
The octal method
• Uses numeric values for permissions Permission Octal Value
read (r) 4
write (w) 2
execute (x) 1
Page - 30
Default Permissions
• The umask command is used to set or display the default file and directory permissions for newly
created files and directories.
• It controls which permission bits are not set on new files and directories.
• The umask value is a three- or four-digit octal number that represents permission bits to be masked
out (i.e., removed) when a file or directory is create
• The default file permission before applying umask is 666 (rw-rw-rw-) for files and 777 (rwxrwxrwx)
for directories
• Does not affect the special advanced permissions of setuid, setgid or sticky bit
• To know permissions from umask output
• Files: 666 – umask output = 644 (rw-r--r--)
• Directories: 777 – umask output= 755 (rwxr-xr-x)
Page - 31
Default Permissions Examples
umask File permissions Directory permissions Description
Page - 32
Quiz Time: What Have You Learned?
1) What is the core of the Linux operating system?
a) Shell b) terminal c)Kernel d) command
2) In which of the following directory does the configuration files are present?
a) /etc/ b) / c) /bin/ d) /root/
3) Using which of the following command can hidden files be viewed?
a) ls -l b) ls -k c) ls –al d) ls –h
4) Which of the following sign is used to represent the user home directory?
a) ~ b) $ c) . d) /
5) Directory is a type of file.
a) True b) False
6) Which of the following directory contains system or administrative executables
a) /bin b) /sbin c) /usr/sbin d) /usr/bin
7) Which the following command reports the system's disk space usage
a) - hf
Page 33 b) df c) stat d) fh
Quiz Time: What Have You Learned?
1) What is the core of the Linux operating system?
a) Shell b) terminal c)Kernel d) command
2) In which of the following directory does the configuration files are present?
a) /etc/ b) / c) /bin/ d) /root/
3) Using which of the following command can hidden files be viewed?
a) ls -l b) ls -k c) ls –al d) ls –h
4) Which of the following sign is used to represent the user home directory?
a) ~ b) $ c) . d) /
5) Directory is a type of file.
a) True b) False
6) Which of the following directory contains system or administrative executables
a) /bin b) /sbin c) /usr/sbin d) /usr/bin
7) Which the following command reports the system's disk space usage
a) - hf
Page 34 b) df c) stat d) fh
orl
Thank you !
Study hard