Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

lecture 2

This document provides an overview of Linux directories, files, and environment variables, detailing their structure, types, and management commands. It covers essential commands for file manipulation, permissions, and ownership, as well as the hierarchical organization of the Linux file system. Additionally, it explains how to set and remove environment variables and the significance of various common environment variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lecture 2

This document provides an overview of Linux directories, files, and environment variables, detailing their structure, types, and management commands. It covers essential commands for file manipulation, permissions, and ownership, as well as the hierarchical organization of the Linux file system. Additionally, it explains how to set and remove environment variables and the significance of various common environment variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Linux and shell programming

Lecture 2: Linux Directories and Files

© spring 2025 – Dr. Dina Awny


Table of Contents

● Linux Environment variables


● Linux Directories
● Linux Files
● Linux File System
● Essential Files commands
Linux Set Environment Variable

● 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

● By removing an environment variable we can remove all existing component of


particular variable.
● To remove an environment variable, execute the unset command followed by
variable name:

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

c. Special Files − Some special files provide access to hardware such as


hard drives, CD-ROM drives, modems, and Ethernet adapters. Other
special files are similar to aliases or shortcuts and enable you to access a
single file using different names.

Page - 9
Understanding the File System Tree

● Linux organizes its files in what is called a hierarchical directory structure.


● The first directory in the file system is called the root directory. The root
directory contains files and subdirectories, which contain more files and
subdirectories, and so on.

Page - 10
Important directories
Directory Purpose
/ The root of the primary filesystem hierarchy
/bin Contain essential command binaries (user executables)

/boot Contain the kernel and bootloader files


/dev Populated with files representing attached devices
/etc Configuration files specific to the host
/home Common location for user home directories
/lib Essential libraries to support /bin and /sbin executables
/mnt Mount point for temporarily mounting a filesystem
/opt Add-on application software packages
/root Home directory for the root user
/sbin Contains system or administrative executables
Page - 11
Important directories (cont.)
Directory Purpose

/var Contains site-specific data which is served by this system

/tmp Location for creating temporary files

/usr The root of the secondary filesystem hierarchy

/usr/bin Contains most of the user commands

/usr/sbin Contains any non-essential binaries used exclusively by the system


administrator (root).

/usr/lib Shared libraries to support /usr/bin and /usr/sbin

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

● To view the information in megabytes and gigabytes, use the


command df -h.
● The -h argument stands for "human-readable" format.
● To view the system's partitions and disk space usage in a graphical
format, use the Gnome System Monitor by clicking on
Applications → System Tools → System Monitor
● Select the File Systems tab to view the system's partitions.
● The following figure illustrates the File Systems tab.
● or using the command gnome-system-monitor.

Page - 14
Types of Files in Linux

•In Linux, everything is a file


• Regular Files: These are your everyday files – text documents, images, videos, etc.
• Directories: Directories are special files that contain other files and directories.
• Links: Links are like shortcuts in Windows. They point to other files or directories.
- Symbolic links (soft links): Think of these as shortcuts that can point to files or directories, even on
different file systems.
- Hard links: These are more like additional names for the same file.
• Device Files: Device files represent hardware devices. They're usually found in the /dev directory.
• Named Pipes and Sockets: These are special files used for inter-process communication.

Page - 15
File Management Commands in Linux

● Listing Files and Directories Ls


ls -l # Long format with more details
The ls command is for listing files and directories. ls -a # Show hidden files
ls -lh # Human-readable file sizes
● Creating Directories
Use mkdir to create new directories:
mkdir my_new_directory
mkdir -p parent/child/grandchild # Create nested directories
● Changing Directories
Navigate through the file system with cd: cd /home/username
cd .. # Move up one level
cd ~ # Go to home directory
Page - 16
Files and Directories

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

• Permissions allow users to Permission Effect of File Effect on Directory


protect files and (Symbol)
directories read (r) Allows for file contents to be Without execute permission on the directory,
• Three permissions types: read or copied. allows for a non-detailed listing of files.
With execute permission, ls -l can provide a
read, write and execute detailed listing.
• Permissions have different write (w) Allows for contents to be Allows for files to be added or removed from
meaning on files vs modified or overwritten. directory. For this permission to work, the
directories directory must also have execute permission.
execute (x) Allows for file to be run as a Allows user to change to the directory, if
process, although text script parent directories have execute permission, as
files require read permission, as well
well.

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

• Based on the following permissions:


● -rwxr-xr-x 1 root root 118644 Apr 4 2014 /bin/ls

○ - 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)

a To refer to the user, group and others (all)

• Next, specify an operator (+, =, -):


Symbol Meaning
+ Add the permission, if necessary
= Specify the exact permission
- Remove the permission, if necessary
• Lastly, specify permission (r, w, x):
Page - 28
The symbolic method
● Examples Example Meaning
chmod u+x myscript Adds execute permission for the user owner
chmod g-w file1 Removes write permission from the group owner

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

• Must always specify 3 values (owner, group, everyone else)


Example Meaning
chmod 764 myscript Results in rwxrw-r--
chmod 644 myfile Results in rw-r--r--
chmod 744 myscript Results in rwxr--r--
chmod 000 myfile Results in ---------

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

002 664 or rw-rw-r-- 775 or rwxrwxr-x Default for ordinary


users
022 644 or rw-r--r-- 755 or rwxr-xr-x Default for root user
007 660 or rw-rw---- 770 or rwxrwx--- No access for others
077 600 or rw------- 700 or rwx------ Private to user

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

Next lecture finding files and getting help

You might also like