0% found this document useful (0 votes)
3 views7 pages

Os 1-3 Programs

The document provides an overview of basic UNIX commands and their functionalities, including commands for file manipulation and process management. It also includes a C program that demonstrates the use of UNIX system calls like fork, exec, and others, as well as functions to simulate common UNIX commands such as cp, ls, and grep. The document serves as a practical guide for understanding and using UNIX commands and programming in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Os 1-3 Programs

The document provides an overview of basic UNIX commands and their functionalities, including commands for file manipulation and process management. It also includes a C program that demonstrates the use of UNIX system calls like fork, exec, and others, as well as functions to simulate common UNIX commands such as cp, ls, and grep. The document serves as a practical guide for understanding and using UNIX commands and programming in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Practicing of Basic UNIX Commands

AIM:
To study about the basics of UNIX

UNIX:
It is a multi-user operating system. Developed at AT & T Bell Industries, USA in
1969.

Ken Thomson along with Dennis Ritchie developed it from MULTICS


(Multiplexed Information and Computing Service) OS.
By1980, UNIX had been completely rewritten using C language.

LINUX:
It is similar to UNIX, which is created by Linus Torualds. All UNIX
commands works in Linux. Linux is a open source software. The main feature
of Linux is coexisting with other OS such as windows and UNIX.

STRUCTURE OF A LINUXSYSTEM:
It consists of three parts.

a) UNIX kernel
b) Shells
c) Tools and Applications

UNIX KERNEL:
Kernel is the core of the UNIX OS. It controls all tasks, schedule all
Processes and carries out all the functions of OS.

Decides when one programs tops and another starts.

SHELL:
Shell is the command interpreter in the UNIX OS. It accepts
command from the user and analyses and interprets them
 pwd – Print Working Directory

 Shows the current directory you're in.


 Example: pwd

 ls – List Directory Contents

 Displays the files and directories in the current directory.


 Example: ls

 cd – Change Directory

 Used to change the current working directory.


 Example: cd /home/user/Documents

 mkdir – Make Directory

 Creates a new directory.


 Example: mkdir new_folder

 rmdir – Remove Directory

 Removes an empty directory.


 Example: rmdir old_folder

 touch – Create a New File

 Creates an empty file.


 Example: touch newfile.txt

 rm – Remove File

 Deletes a file.
 Example: rm oldfile.txt

 cp – Copy Files or Directories

 Copies a file or directory.


 Example: cp file1.txt file2.txt

 mv – Move or Rename Files

 Moves or renames a file or directory.


 Example: mv file1.txt file2.txt (rename)
 Example: mv file1.txt /home/user/Documents/ (move)

 cat – Concatenate and Display File Content

 Displays the contents of a file.


 Example: cat file.txt

 man – Manual Pages

 Displays the manual or documentation for a command.


 Example: man ls

 echo – Display a Message or Variable

 Prints a message or the value of a variable.


 Example: echo "Hello World!"

 chmod – Change File Permissions

 Modifies the permissions of a file or directory.


 Example: chmod 755 file.txt

 ps – Process Status

 Lists the currently running processes.


 Example: ps

2. can you write in one c programme Write programs using the following UNIX
operating system calls fork, exec, getpid, exit, wait, close, stat, opendir and
readdir.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>

int main() {
pid_t pid;
int status;

printf("Parent process, PID: %d\n", getpid());

// Create a new process using fork()


pid = fork();

if (pid == 0) {
// Child process
printf("Child process created, PID: %d\n", getpid());

// Using exec() to run a command (ls -l)


printf("Child process executing 'ls -l' using exec()...\n");
execl("/bin/ls", "ls", "-l", NULL);

// If exec fails
perror("exec failed");
exit(EXIT_FAILURE);

} else if (pid > 0) {


// Parent process
printf("Parent process waiting for child to complete...\n");

// Using wait() to wait for the child process to finish


wait(&status);
printf("Child process completed.\n");

// Using opendir() and readdir() to read the current directory


printf("Listing files in the current directory using opendir() and readdir():\n");
DIR *dir = opendir(".");
if (dir == NULL) {
perror("opendir failed");
exit(EXIT_FAILURE);
}

struct dirent *entry;


while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);

// Using stat() to get file information


struct stat fileStat;
const char *filename = "example.txt";
if (stat(filename, &fileStat) == 0) {
printf("\nFile '%s' information:\n", filename);
printf("File size: %ld bytes\n", fileStat.st_size);
printf("Number of links: %ld\n", fileStat.st_nlink);
printf("File permissions: %o\n", fileStat.st_mode & 0777);
} else {
perror("stat failed");
}

// Using close() to close a file descriptor


int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("open failed");
} else {
printf("\nOpened file 'example.txt' with file descriptor: %d\n", fd);
close(fd);
printf("File descriptor %d closed.\n", fd);
}

} else {
// fork() failed
perror("fork failed");
exit(EXIT_FAILURE);
}

return 0;
}

3. Simulate UNIX commands like cp, ls, grep, etc.,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

// Function to simulate the `cp` command


void simulate_cp(const char *src, const char *dest) {
int source_fd = open(src, O_RDONLY);
if (source_fd == -1) {
perror("Error opening source file");
return;
}

int dest_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0644);


if (dest_fd == -1) {
perror("Error opening destination file");
close(source_fd);
return;
}

char buffer[1024];
ssize_t bytes;
while ((bytes = read(source_fd, buffer, sizeof(buffer))) > 0) {
write(dest_fd, buffer, bytes);
}

close(source_fd);
close(dest_fd);
printf("File copied from %s to %s\n", src, dest);
}

// Function to simulate the `ls` command


void simulate_ls(const char *path) {
DIR *dir = opendir(path);
if (!dir) {
perror("Error opening directory");
return;
}

struct dirent *entry;


while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}

closedir(dir);
}

// Function to simulate the `grep` command


void simulate_grep(const char *pattern, const char *filename) {
FILE *file = fopen(filename, "r");
if (!file) {
perror("Error opening file");
return;
}

char line[1024];
while (fgets(line, sizeof(line), file)) {
if (strstr(line, pattern)) {
printf("%s", line);
}
}

fclose(file);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s command [arguments]\n", argv[0]);
printf("Commands:\n");
printf(" cp <source> <destination>\n");
printf(" ls <directory_path>\n");
printf(" grep <pattern> <filename>\n");
return 1;
}

if (strcmp(argv[1], "cp") == 0) {
if (argc != 4) {
printf("Usage: %s cp <source> <destination>\n", argv[0]);
return 1;
}
simulate_cp(argv[2], argv[3]);
} else if (strcmp(argv[1], "ls") == 0) {
const char *path = (argc == 3) ? argv[2] : ".";
simulate_ls(path);
} else if (strcmp(argv[1], "grep") == 0) {
if (argc != 4) {
printf("Usage: %s grep <pattern> <filename>\n", argv[0]);
return 1;
}
simulate_grep(argv[2], argv[3]);
} else {
printf("Unknown command: %s\n", argv[1]);
return 1;
}

return 0;
}

You might also like