diff --git a/README.md b/README.md index f19837c..be3e9ef 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,13 @@ Here is a list of the commands and their availability: - `pwd` - Print name of current/working directory `C++` `C` `Ultra light` `Rust` `Python` - `cat` - Concatenate files and print on the standard output`C++` `C` `Ultra light` `Rust` `Python` - `echo` - Display a line of text `C++` `C` `Ultra light` `Rust` `Python` -- `mkdir` - Create a directory`C++` `C` `Ultra light` `Rust` -- `rmdir` - Remove a directory `C++` `C` `Ultra light` `Rust` -- `rm` - Remove files or directories`C++` `C` `Ultra light` `Rust` -- `cp` - Copy files and directories `C++` `Ultra light` `Rust` -- `mv` - Move (rename) files `C++` `C` `Ultra light` `Rust` -- `touch` - Change file timestamps `C++` `C` `Ultra light` `Rust` -- `date` - Display the current time and date `C++` `C` `Ultra light` `Rust` +- `mkdir` - Create a directory`C++` `C` `Ultra light` `Rust` `Python` +- `rmdir` - Remove a directory `C++` `C` `Ultra light` `Rust` `Python` +- `rm` - Remove files or directories`C++` `C` `Ultra light` `Rust` `Python` +- `cp` - Copy files and directories `C++` `Ultra light` `Rust` `Python` +- `mv` - Move (rename) files `C++` `C` `Ultra light` `Rust` `Python` +- `touch` - Change file timestamps `C++` `C` `Ultra light` `Rust` `Python` +- `date` - Display the current time and date `C++` `C` `Ultra light` `Rust` `Python` For documentation on the commands, see the [doc/README.md](doc/README.md) folder. diff --git a/src/python/cp.py b/src/python/cp.py new file mode 100644 index 0000000..658c5e0 --- /dev/null +++ b/src/python/cp.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# Light builtins's (Python version) +# cp - copy files or directories +# A lightweight repository of useful shell commands +# GitHub: https://www.github.com/lewisevans2007/light_builtins +# Licence: GNU General Public License v3.0 +# By: Lewis Evans + +import os +import sys + +def main(args): + if len(args) < 2: + print("cp: missing file operand") + return 1 + if len(args) > 2: + print("cp: extra operand") + return 1 + if not os.path.exists(args[0]): + print("cp: cannot stat '" + args[0] + "': No such file or directory") + return 1 + if os.path.isdir(args[0]): + print("cp: omitting directory '" + args[0] + "'") + return 1 + + try: + with open(args[0], 'r') as f: + data = f.read() + except: + print("cp: cannot stat '" + args[0] + "': Permission denied") + return 1 + + try: + with open(args[1], 'w') as f: + f.write(data) + except: + print("cp: cannot stat '" + args[1] + "': Permission denied") + return 1 + return 0 + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) \ No newline at end of file diff --git a/src/python/date.py b/src/python/date.py new file mode 100644 index 0000000..d3fb5f0 --- /dev/null +++ b/src/python/date.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# Light builtins's (Python version) +# date - show the date and time +# A lightweight repository of useful shell commands +# GitHub: https://www.github.com/lewisevans2007/light_builtins +# Licence: GNU General Public License v3.0 +# By: Lewis Evans + +import sys +import datetime + +def main(args): + date_time = datetime.datetime.now() + print(date_time) + return 0 + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) \ No newline at end of file diff --git a/src/python/mkdir.py b/src/python/mkdir.py new file mode 100644 index 0000000..b5098cd --- /dev/null +++ b/src/python/mkdir.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# Light builtins's (Python version) +# mkdir - make directories +# A lightweight repository of useful shell commands +# GitHub: https://www.github.com/lewisevans2007/light_builtins +# Licence: GNU General Public License v3.0 +# By: Lewis Evans + +import os +import sys + +def main(args): + for arg in args: + os.mkdir(arg) + return 0 + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) \ No newline at end of file diff --git a/src/python/mv.py b/src/python/mv.py new file mode 100644 index 0000000..dd0bb18 --- /dev/null +++ b/src/python/mv.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +# Light builtins's (Python version) +# mv - move files or directories +# A lightweight repository of useful shell commands +# GitHub: https://www.github.com/lewisevans2007/light_builtins +# Licence: GNU General Public License v3.0 +# By: Lewis Evans + +import os +import sys + +def main(args): + if len(args) < 2: + print("mv: missing file operand") + return 1 + if len(args) > 2: + print("mv: extra operand") + return 1 + if not os.path.exists(args[0]): + print("mv: cannot stat '" + args[0] + "': No such file or directory") + return 1 + if os.path.isdir(args[0]): + print("mv: omitting directory '" + args[0] + "'") + return 1 + + try: + os.rename(args[0], args[1]) + except: + print("mv: cannot stat '" + args[1] + "': Permission denied") + return 1 + return 0 + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) \ No newline at end of file diff --git a/src/python/rm.py b/src/python/rm.py new file mode 100644 index 0000000..023de57 --- /dev/null +++ b/src/python/rm.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# Light builtins's (Python version) +# rm - remove files or directories +# A lightweight repository of useful shell commands +# GitHub: https://www.github.com/lewisevans2007/light_builtins +# Licence: GNU General Public License v3.0 +# By: Lewis Evans + +import os +import sys + +def main(args): + for arg in args: + os.remove(arg) + return 0 + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) \ No newline at end of file diff --git a/src/python/rmdir.py b/src/python/rmdir.py new file mode 100644 index 0000000..6e95cc5 --- /dev/null +++ b/src/python/rmdir.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# Light builtins's (Python version) +# rmdir - remove empty directories +# A lightweight repository of useful shell commands +# GitHub: https://www.github.com/lewisevans2007/light_builtins +# Licence: GNU General Public License v3.0 +# By: Lewis Evans + +import os +import sys + +def main(args): + for arg in args: + os.rmdir(arg) + return 0 + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) \ No newline at end of file diff --git a/src/python/touch.py b/src/python/touch.py new file mode 100644 index 0000000..681ded6 --- /dev/null +++ b/src/python/touch.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# Light builtins's (Python version) +# touch - create a file with the current time +# A lightweight repository of useful shell commands +# GitHub: https://www.github.com/lewisevans2007/light_builtins +# Licence: GNU General Public License v3.0 +# By: Lewis Evans + +import os +import sys + +def main(args): + if len(args) < 1: + print("touch: missing file operand") + return 1 + if len(args) > 1: + print("touch: extra operand") + return 1 + if os.path.exists(args[0]): + print("touch: cannot create file '" + args[0] + "': File exists") + return 1 + try: + with open(args[0], 'w') as f: + f.write("") + except: + print("touch: cannot create file '" + args[0] + "': Permission denied") + return 1 + return 0 + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) \ No newline at end of file