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

Exp3

The document outlines an experiment to implement the Linux 'mv' command using kernel APIs, emphasizing that user-space libraries cannot be utilized in the kernel. It provides a program that attempts to rename a file and discusses the limitations of the rename function across different file systems. The conclusion highlights the efficiency of using the rename() system call for moving files within the same file system.

Uploaded by

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

Exp3

The document outlines an experiment to implement the Linux 'mv' command using kernel APIs, emphasizing that user-space libraries cannot be utilized in the kernel. It provides a program that attempts to rename a file and discusses the limitations of the rename function across different file systems. The conclusion highlights the efficiency of using the rename() system call for moving files within the same file system.

Uploaded by

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

Vidyavardhini’s College of Engineering & Technology

Department of Computer Engineering

Experiment No.3
Implement basic command of linux “mv” using kernel APIs.
Date of Performance:
Date of Submission:
Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering

Aim: Implement basic command of linux “mv” using kernel APIs.


Theory:
The kernel is a stand-alone entity that can not use libraries in user-space (not even libc).
As a result, the usual user-space functions (printf, malloc, free, open, read, write,
memcpy, strcpy, etc.) can no longer be used.
Is command in Linux:
The ls command is commonly used to identify the files and directories in the working
directory. This command is one of the many often-used Linux commands that you
should know.
This command can be used by itself without any arguments and it will provide us the
output with all the details about the files and the directories in the current working
directory.
mkdir command in Linux:
This mkdir command allows you to create fresh directories in the terminal itself. The
default syntax is mkdir <directory name> and the new directory will be created.
rmdir command in Linux:
The rmdir command is used to delete permanently an empty directory. To perform this
command the user running this command must be having sudo privileges in the parent
directory.
mv command in Linux:
The mv command is generally used for renaming the files in Linux.

Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) {


// Ensure correct number of arguments
Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering

if (argc != 3) {
fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);
return EXIT_FAILURE;
}

// Attempt to rename (move) the file


if (rename(argv[1], argv[2]) == 0) {
printf("Moved successfully!\n");
return EXIT_SUCCESS;
}

// If rename fails, try linking + unlinking (for cross-filesystem moves)


if (link(argv[1], argv[2]) == 0) {
unlink(argv[1]); // Remove the original file
printf("Moved successfully using link+unlink!\n");
return EXIT_SUCCESS;
}

// If both fail, print error


perror("Error moving file");
return EXIT_FAILURE;
}
Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering

Outcome:

Conclusion:
The mv command in Linux can be efficiently implemented using the rename() system
call. This method ensures atomicity and avoids unnecessary data copying. However,
rename() does not work across different file systems; in such cases, manual copying
Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering

followed by deletion is required. This implementation provides a simple, efficient, and


reliable way to move files within the same file system.

You might also like