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

unix

The document outlines several programs for practicing basic UNIX commands, including listing files, printing the working directory, reading files, and simulating commands like cp, ls, and grep. Each program is accompanied by sample outputs demonstrating their functionality. The results indicate that all programs were executed successfully.

Uploaded by

Praveena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

unix

The document outlines several programs for practicing basic UNIX commands, including listing files, printing the working directory, reading files, and simulating commands like cp, ls, and grep. Each program is accompanied by sample outputs demonstrating their functionality. The results indicate that all programs were executed successfully.

Uploaded by

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

Date: Exp. No.: Page No.

:
AIM:

To write a program of practicingofBasicUNIXCommands.

PROGRAM:

import os

def list_files():
path = input("Enter directory path for ls: ")
if os.path.isdir(path):
print("\n".join(os.listdir(path)))
else:
print("Invalid directory path")
def print_working_directory():
print(f"Current working directory: {os.getcwd()}")
def read_file():
file_name = input("Enter file name for cat: ")
if os.path.isfile(file_name):
with open(file_name, 'r') as file:
print(file.read())
else:
print("Invalid file path")

def main():
while True:
print("\n1. List files (ls)")
print("2. Print working directory (pwd)")
print("3. Read file (cat)")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == '1':
list_files()
elif choice == '2':
print_working_directory()
elif choice == '3':
read_file()
elif choice == '4':

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


Date: Exp. No.: Page No.:
break
else:
print("Invalid choice")

if __name__ == "__main__":
main()
SAMPLE OUTPUT:
1. List files (ls)
2. Print working directory (pwd)
3. Read file (cat)
4. Exit
Enter your choice: 1
Enter directory path for ls: /home/user/documents
file1.txt
file2.txt
file3.pdf

1. List files (ls)


2. Print working directory (pwd)
3. Read file (cat)
4. Exit
Enter your choice: 2
Current working directory: /home/user

1. List files (ls)


2. Print working directory (pwd)
3. Read file (cat)
4. Exit
Enter your choice: 3
Enter file name for cat: /home/user/documents/file1.txt
This is the content of file1.txt.

1. List files (ls)


2. Print working directory (pwd)
3. Read file (cat)
4. Exit

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


Date: Exp. No.: Page No.:
Enter your choice: 4
EXECUTED OUTPUT:
1. List files (ls)
2. Print working directory (pwd)
3. Read file (cat)
4. Exit
Enter your choice: 2
Current working directory: /home/user/projects

1. List files (ls)


2. Print working directory (pwd)
3. Read file (cat)
4. Exit
Enter your choice: 1
Enter directory path for ls: /home/user/projects
project1/
project2/
README.md

1. List files (ls)


2. Print working directory (pwd)
3. Read file (cat)
4. Exit
Enter your choice: 3
Enter file name for cat: /home/user/projects/README.md
# Project Title
This is a brief description of the project.

1. List files (ls)


2. Print working directory (pwd)
3. Read file (cat)
4. Exit
Enter your choice: 4
RESULT:
Hence the program of practicing unix commands was executed successfully.

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


Date: Exp. No.: Page No.:

AIM:
To WriteprogramsusingthefollowingUNIXoperatingsystemcalls fork, exec, getpid, exit, wait, close, stat,
opendir and readdir
PROGRAM:
import os
import sys
def create_process():
pid = os.fork()
if pid == 0:
print(f"Child process: {os.getpid()}")
os.execvp('ls', ['ls', '-l'])
else:
print(f"Parent process: {os.getpid()}")
os.wait()

def get_file_stats(file_path):
try:
stat_info = os.stat(file_path)
print(f"File Size: {stat_info.st_size} bytes")
print(f"Last Modified: {stat_info.st_mtime}")
except FileNotFoundError:
print("File not found")

def read_directory(dir_path):
try:
dir = os.opendir(dir_path)
for entry in os.listdir(dir_path):
print(entry)
except FileNotFoundError:
print("Directory not found")

def main():
user_choice = input("Enter 1 to create process, 2 to get file stats, 3 to read directory: ")
if user_choice == '1':

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


Date: Exp. No.: Page No.:
create_process()
elif user_choice == '2':
file_path = input("Enter file path: ")
get_file_stats(file_path)
elif user_choice == '3':
dir_path = input("Enter directory path: ")
read_directory(dir_path)
else:
print("Invalid choice")
sys.exit(1)
if __name__ == "__main__":
main()
SAMPLE OUTPUT:
1. Run commands
2. File information
3. List directory
4. Exit
Enter choice: 1
Enter commands (space separated, e.g., ls -l): ls -l; pwd
total 0
-rw-r--r-- 1 user user 0 Oct 1 12:00 file1.txt
-rw-r--r-- 1 user user 0 Oct 1 12:00 file2.txt
/home/user
1. Run commands
2. File information
3. List directory
4. Exit
Enter choice: 2
Enter file path: /home/user/file1.txt
File: /home/user/file1.txt
Size: 0 bytes
Permissions: 644
1. Run commands
2. File information
3. List directory
4. Exit

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


Date: Exp. No.: Page No.:
Enter choice: 3
Enter directory path: /home/user
file1.txt
file2.txt
Documents
1. Run commands
2. File information
3. List directory
4. Exit
Enter choice: 4
EXPECTED OUTPUT:
Enter 1 to create process, 2 to get file stats, 3 to read directory: 2
Enter file path: /home/user/documents/report.txt
File Size: 2048 bytes
Last Modified: 1664620800.0

Enter 1 to create process, 2 to get file stats, 3 to read directory: 1


Parent process: 12345
Child process: 12346
total 8
-rw-r--r-- 1 user user 512 Oct 1 12:00 summary.docx
-rw-r--r-- 1 user user 1024 Oct 1 12:00 presentation.pptx

Enter 1 to create process, 2 to get file stats, 3 to read directory: 3


Enter directory path: /home/user/documents
report.txt
summary.docx
presentation.pptx

Enter 1 to create process, 2 to get file stats, 3 to read directory: 4


Invalid choice
RESULT:
Hence the program for operating system calls fork, exec, getpid, exit, wait, close, stat,
opendir and readdir was executed successfully.

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


Date: Exp. No.: Page No.:
AIM:
To SimulateUNIXcommandslikecp,ls,grep,etc.,

PROGRAM:
import os
import shutil

def simulate_ls(path):
try:
files = os.listdir(path)
for file in files:
print(file)
except FileNotFoundError:
print("Directory not found.")

def simulate_cp(source, destination):


try:
shutil.copy(source, destination)
print(f"Copied {source} to {destination}")
except FileNotFoundError:
print("Source file not found.")
except Exception as e:
print(f"Error: {e}")

def simulate_grep(pattern, filename):


try:
with open(filename, 'r') as file:
lines = file.readlines()
for line in lines:
if pattern in line:
print(line.strip())
except FileNotFoundError:
print(f"File {filename} not found.")

def main():
while True:

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


Date: Exp. No.: Page No.:
command = input("Enter a command (ls, cp, grep, exit): ").strip()
if command == "ls":
path = input("Enter the directory path: ").strip()
simulate_ls(path)
elif command == "cp":
source = input("Enter the source file path: ").strip()
destination = input("Enter the destination file path: ").strip()
simulate_cp(source, destination)
elif command == "grep":
pattern = input("Enter the pattern to search for: ").strip()
filename = input("Enter the file name: ").strip()
simulate_grep(pattern, filename)
elif command == "exit":
break
else:
print("Unknown command.")

if __name__ == "__main__":
main()
SAMPLE OUTPUT:
Enter a command (ls, cp, grep, exit): ls
Enter the directory path: /home/user/pictures
Directory not found.

Enter a command (ls, cp, grep, exit): cp


Enter the source file path: /home/user/documents/file2.txt
Enter the destination file path: /home/user/documents/file2_copy.txt
Copied /home/user/documents/file2.txt to /home/user/documents/file2_copy.txt

Enter a command (ls, cp, grep, exit): grep


Enter the pattern to search for: TODO
Enter the file name: /home/user/documents/todo_list.txt
File /home/user/documents/todo_list.txt not found.

Enter a command (ls, cp, grep, exit): exit

OPERATING SYSTEM LABORATORY CSE DEPARTMENT


Date: Exp. No.: Page No.:
EXECUTED OUTPUT:
Enter a command (ls, cp, grep, exit): ls
Enter the directory path: /home/user/documents
file1.txt
file2.txt
report.pdf
Enter a command (ls, cp, grep, exit): cp
Enter the source file path: /home/user/documents/file1.txt
Enter the destination file path: /home/user/documents/file1_copy.txt
Copied /home/user/documents/file1.txt to /home/user/documents/file1_copy.txt
Enter a command (ls, cp, grep, exit): grep
Enter the pattern to search for: important
Enter the file name: /home/user/documents/report.pdf
File /home/user/documents/report.pdf not found.
Enter a command (ls, cp, grep, exit): exit
RESULT:
Hence the program for to simulate unix commands was executed successfully.

OPERATING SYSTEM LABORATORY CSE DEPARTMENT

You might also like