Operating Systems Lab - Week 03
Course: CS211P - Operating System (1)
Faculty: Faculty of Computers and Information - Mansoura University
Semester: Fall 2024-2025
Grade: 2
Table of Contents
- Echo and Cat Commands
- Ownership of Linux Files
- File Permissions
- Archiving and Compression
- System Information Commands
- Lab Exercises
1. Echo and Cat Commands
1.1 The echo Command
The echo command is used to display text or strings on the terminal.
Basic Syntax:
echo stringExample:
echo "Hello, Operating Systems!"Output: Hello, Operating Systems!
1.2 Redirecting Output to a File
Instead of displaying output on the terminal, you can redirect it to a file using the >> operator (append) or > operator (overwrite).
Append to a file:
echo "This is a new line" >> fileName.txtOverwrite a file:
echo "This will replace all content" > fileName.txtNote:
>creates a new file or overwrites existing content>>appends to the end of an existing file or creates a new one if it doesn't exist
1.3 The cat Command
The cat (concatenate) command is used to read and display file contents.
Basic Syntax:
cat fileNameExample:
cat myfile.txt1.4 Advanced cat Usage
Display contents with line numbers:
cat -n fileNameExample:
cat -n script.shOutput:
1 #!/bin/bash
2 echo "Hello World"
3 # This is a commentView contents from multiple files:
cat fileName1 fileName2Example:
cat file1.txt file2.txt2. Ownership of Linux Files
In Linux, every file and directory has three types of ownership:
2.1 Owner (User - u)
- The user who created the file
- Has primary control over the file
- Can modify permissions
2.2 Group (g)
- A group can contain multiple users
- All users in the group share the same permissions for the file
- Useful for collaborative work
2.3 Others (o)
- Represents everyone else on the system
- Users who are neither the owner nor in the group
- Typically have the most restricted access
Example:
ls -l myfile.txtOutput:
-rw-r--r-- 1 mahmoud students 1234 Oct 12 10:30 myfile.txt- Owner:
mahmoud - Group:
students - Others: all other users
3. File Permissions
Linux uses a permission system to control access to files and directories. There are three types of permissions:
3.1 Permission Types
| Permission | Symbol | Numeric Value | For Files | For Directories |
|---|---|---|---|---|
| Read | r | 4 | Open and read a file | List directory contents |
| Write | w | 2 | Modify file contents | Create, delete, rename files in directory |
| Execute | x | 1 | Run as a program | Access directory (cd into it) |
| No Permission | - | 0 | No access | No access |
3.2 Permission Structure
Permissions are displayed in the following format:
-rwxrwxrwxBreaking it down:
- rwx rwx rwx
│ │ │ │
│ │ │ └─── Other permissions
│ │ └─────── Group permissions
│ └─────────── Owner permissions
└────────────── File type (- for file, d for directory)Example:
-rw-r--r--- Owner: read + write (rw-)
- Group: read only (r--)
- Others: read only (r--)
3.3 Numeric Permission Calculation
Permissions can be represented as three-digit octal numbers:
| Permission | Calculation | Value |
|---|---|---|
| rwx | 4 + 2 + 1 | 7 |
| rw- | 4 + 2 + 0 | 6 |
| r-x | 4 + 0 + 1 | 5 |
| r-- | 4 + 0 + 0 | 4 |
| -wx | 0 + 2 + 1 | 3 |
| -w- | 0 + 2 + 0 | 2 |
| --x | 0 + 0 + 1 | 1 |
| --- | 0 + 0 + 0 | 0 |
Example:
chmod 755means:- Owner: rwx (7)
- Group: r-x (5)
- Others: r-x (5)
3.4 Changing Permissions with chmod
The chmod (change mode) command modifies file permissions.
Method 1: Symbolic Method
Syntax:
chmod [who][operation][permission] filenameWho:
u= user (owner)g= groupo= othersa= all (user, group, and others)
Operation:
+= add permission-= remove permission== set exact permission
Examples:
# Add execute permission for owner
chmod u+x script.sh
# Remove write permission for group
chmod g-w file.txt
# Add read and write for all
chmod a+rw document.txt
# Remove execute for others
chmod o-x program
# Set exact permissions: owner=rw, group=r, others=nothing
chmod u=rw,g=r,o= file.txtMethod 2: Numeric (Octal) Method
Syntax:
chmod [number] filenameExamples:
# Owner: rwx, Group: ---, Others: r--
chmod 704 file.txt
# Owner: rw-, Group: r--, Others: r--
chmod 644 document.txt
# Owner: rwx, Group: r-x, Others: r-x
chmod 755 script.sh
# Owner: rw-, Group: rw-, Others: ---
chmod 660 private.txtCommon Permission Patterns:
644- Standard file permissions (owner can read/write, others can read)755- Standard executable/directory permissions700- Private file/directory (only owner has access)777- All permissions for everyone (rarely recommended)
4. Archiving and Compression
4.1 Archiving vs. Compression
Archiving:
- Collects multiple files and directories into a single file
- Does not reduce file size
- Uses
.tarextension - Useful for organizing and transferring multiple files
Compression:
- Reduces the size of files
- Saves disk space and bandwidth
- Uses extensions like
.gz,.zip - Useful for storing and transmitting large files
4.2 The tar Command (Archiving)
tar stands for "Tape Archive" and is used to create archive files.
Creating an Archive
Syntax:
tar cvf archive_name.tar file1 file2 directory_nameOptions:
c= create a new archivev= verbose (show progress)f= specify filename
Example:
tar cvf backup.tar file1.txt file2.txt documents/Extracting an Archive
Syntax:
tar xvf archive_name.tarOptions:
x= extract files from archivev= verbosef= specify filename
Example:
tar xvf backup.tarViewing Archive Contents
Syntax:
tar tvf archive_name.tarOptions:
t= list contentsv= verbosef= specify filename
Example:
tar tvf backup.tar4.3 The gzip Command (Compression)
gzip compresses a single file and adds .gz extension.
Compressing a File
Syntax:
gzip filenameExample:
gzip largefile.txt
# Creates: largefile.txt.gz (original file is removed)Note: By default, gzip removes the original file after compression.
Decompressing a File
Syntax:
gzip -d filename.gz
# OR
gunzip filename.gzExample:
gzip -d largefile.txt.gz
# Restores: largefile.txtCombining tar and gzip
You can create compressed archives using both commands together:
Method 1: Two-step process
tar cvf archive.tar files/
gzip archive.tar
# Creates: archive.tar.gzMethod 2: Single command
tar czf archive.tar.gz files/
# c = create, z = compress with gzip, f = filenameExtracting compressed tar archives:
tar xzf archive.tar.gz
# x = extract, z = decompress with gzip, f = filename4.4 The zip Command (Compression)
zip compresses files and directories into a .zip archive (Windows compatible).
Creating a Zip Archive
Syntax:
zip -r archive_name.zip file1 file2 directory_nameOptions:
-r= recursive (include directories)
Example:
zip -r backup.zip file1.txt file2.txt documents/Extracting a Zip Archive
Syntax:
unzip archive_name.zipExample:
unzip backup.zipExtract to specific directory:
unzip backup.zip -d /path/to/destination/View contents without extracting:
unzip -l backup.zip5. System Information Commands
5.1 Disk Space Commands
df - Disk Filesystem Usage
Shows disk space usage for all mounted filesystems.
Syntax:
df -hOptions:
-h= human-readable format (KB, MB, GB)
Example Output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 28G 42% /
/dev/sda2 100G 75G 20G 79% /homedu - Directory Usage
Shows disk space used by directories.
Syntax:
du -hs directory_nameOptions:
-h= human-readable format-s= summary (total size only)
Example:
du -hs /home/mahmoud/documents/
# Output: 2.3G /home/mahmoud/documents/5.2 Memory Usage
free - Memory and Swap Usage
Shows memory (RAM) and swap space usage.
Syntax:
free
# OR for human-readable format
free -hExample Output:
total used free shared buff/cache available
Mem: 7.7G 2.1G 3.2G 256M 2.4G 5.1G
Swap: 2.0G 0B 2.0G5.3 System Information Commands
Hostname and Network
# Show computer name
hostname
# Show IP address
hostname -IDate and Time
# Show current date and time
date
# Show calendar for current month
cal
# Show calendar for specific year
cal 2025User Information
# Show current username (Method 1)
echo $USER
# Show current username (Method 2)
whoami
# Show user ID and groups
idExample Output:
$ whoami
mahmoud
$ id
uid=1000(mahmoud) gid=1000(mahmoud) groups=1000(mahmoud),27(sudo),999(docker)5.4 Command History
# Show recently used commands
history
# Save history to a file
history > commands.txt
# Execute a specific command from history
!123 # Runs command number 123
# Execute the last command
!!
# Search command history
history | grep "chmod"5.5 Network Testing
ping - Check Network Connection
Tests connectivity to a server or IP address.
Syntax:
ping server_name
# OR
ping IP_addressExample:
ping google.com
ping 8.8.8.8Stop ping with: Ctrl + C
Limit number of pings:
ping -c 5 google.com
# Sends only 5 ping packets5.6 Exit Terminal
exit
# OR press Ctrl + D6. Lab Exercises
Exercise 1: Echo and Cat Commands
- Create a file named
greeting.txtwith the text "Hello from FCIS" usingecho - Display the contents of
greeting.txtusingcat - Append "Mansoura University" to the file
- Display the file with line numbers
- Create another file
info.txtwith your name and student ID - Display both files together using a single
catcommand
Solution:
echo "Hello from FCIS" > greeting.txt
cat greeting.txt
echo "Mansoura University" >> greeting.txt
cat -n greeting.txt
echo "Name: Mahmoud Abas" > info.txt
echo "Student ID: 12345" >> info.txt
cat greeting.txt info.txtExercise 2: File Permissions
- Create a file named
script.shwith some text - Check its current permissions using
ls -l - Give the owner execute permission using symbolic method
- Remove write permission from group using symbolic method
- Set permissions to 755 using numeric method
- Create a directory and set its permissions to 700
Solution:
echo "#!/bin/bash" > script.sh
echo "echo 'Test script'" >> script.sh
ls -l script.sh
chmod u+x script.sh
chmod g-w script.sh
chmod 755 script.sh
mkdir mydir
chmod 700 mydir
ls -ld mydirExercise 3: Archiving and Compression
- Create three text files with different content
- Create a tar archive containing all three files
- List the contents of the archive
- Extract the archive to verify
- Compress one of the files using
gzip - Create a zip archive of all files
- Check the size difference between original and compressed files
Solution:
echo "File 1 content" > file1.txt
echo "File 2 content" > file2.txt
echo "File 3 content" > file3.txt
tar cvf myarchive.tar file1.txt file2.txt file3.txt
tar tvf myarchive.tar
tar xvf myarchive.tar
gzip file1.txt
ls -lh
zip myfiles.zip file2.txt file3.txt file1.txt.gz
ls -lhExercise 4: System Information
- Display your computer's hostname
- Show your IP address
- Display current date and time
- Show this month's calendar
- Check disk space usage in human-readable format
- Check the size of your home directory
- Display memory usage
- Show your username using three different methods
- Ping google.com 5 times
Solution:
hostname
hostname -I
date
cal
df -h
du -hs ~
free -h
echo $USER
whoami
id
ping -c 5 google.comExercise 5: Comprehensive Challenge
Create a script that:
- Creates a directory named "lab3_project"
- Inside it, creates three subdirectories: "docs", "scripts", "data"
- Creates sample files in each directory
- Sets appropriate permissions (755 for directories, 644 for files)
- Creates a compressed tar archive of the entire project
- Shows the size comparison between the directory and the archive
Solution:
# Create directory structure
mkdir -p lab3_project/{docs,scripts,data}
# Create sample files
echo "Documentation file" > lab3_project/docs/readme.txt
echo "#!/bin/bash" > lab3_project/scripts/run.sh
echo "echo 'Running script'" >> lab3_project/scripts/run.sh
echo "Sample data: 1,2,3,4,5" > lab3_project/data/data.csv
# Set permissions
chmod 755 lab3_project lab3_project/*
chmod 644 lab3_project/docs/* lab3_project/data/*
chmod 755 lab3_project/scripts/*
# Create compressed archive
tar czf lab3_project.tar.gz lab3_project/
# Show size comparison
echo "Directory size:"
du -hs lab3_project/
echo "Archive size:"
ls -lh lab3_project.tar.gzSummary
In this lab, you learned:
✅ Echo and Cat Commands: Display text and read file contents
✅ File Ownership: Understanding user, group, and others
✅ File Permissions: Reading, writing, and executing files using symbolic and numeric methods
✅ Archiving: Using tar to bundle files together
✅ Compression: Reducing file sizes with gzip and zip
✅ System Commands: Checking disk space, memory, network connectivity, and user information
Additional Resources
- Linux Manual Pages:
man command_name(e.g.,man chmod) - Linux Command Help:
command_name --help(e.g.,chmod --help) - Online Resources:
Tips for Success
- Practice regularly - Linux commands become easier with repetition
- Use tab completion - Press Tab to auto-complete filenames and commands
- Read error messages - They usually tell you what went wrong
- Use
manpages - They contain detailed information about commands - Be careful with permissions - Wrong permissions can make files inaccessible
- Backup before compression - Always keep a copy of important files
End of Lab 03