From 0e47055559f3fbd958af737ed389cd6e254626e0 Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Tue, 19 May 2026 23:02:37 +0100 Subject: [PATCH 1/8] only read one file and print one file content --- implement-shell-tools/cat/cat.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 implement-shell-tools/cat/cat.py diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..74fa293e8 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,9 @@ +import sys + +args = sys.argv[1:] + +path = args[0] + +with open(path, "r") as file: + content = file.read() + print(content) \ No newline at end of file From f4ce299b6fc70021d4cdf49454fe465e1e9949bb Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Tue, 19 May 2026 23:05:24 +0100 Subject: [PATCH 2/8] Add -n flag to number all lines --- implement-shell-tools/cat/cat.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index 74fa293e8..0e6fb9176 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -2,8 +2,24 @@ args = sys.argv[1:] -path = args[0] +show_numbers = False +paths = [] -with open(path, "r") as file: - content = file.read() - print(content) \ No newline at end of file +# separate flags and paths +for arg in args: + if arg == "-n": + show_numbers = True + else: + paths.append(arg) + +for path in paths: + with open(path, "r") as file: + lines = file.readlines() + + if show_numbers: + i = 1 + for line in lines: + print(f"{i} {line.rstrip()}") + i += 1 + else: + print("".join(lines)) \ No newline at end of file From d5912a70a7a7b6520075665b957eb6df6edeffed Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Tue, 19 May 2026 23:10:09 +0100 Subject: [PATCH 3/8] Add -b flag to number non-empty lines --- implement-shell-tools/cat/cat.py | 34 ++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index 0e6fb9176..105aeb7c1 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -2,13 +2,15 @@ args = sys.argv[1:] -show_numbers = False +option = "none" paths = [] -# separate flags and paths +# parse args for arg in args: if arg == "-n": - show_numbers = True + option = "n" + elif arg == "-b": + option = "b" else: paths.append(arg) @@ -16,10 +18,22 @@ with open(path, "r") as file: lines = file.readlines() - if show_numbers: - i = 1 - for line in lines: - print(f"{i} {line.rstrip()}") - i += 1 - else: - print("".join(lines)) \ No newline at end of file + line_number = 1 + + for line in lines: + + line = line.rstrip("\n") + + if option == "n": + print(f"{line_number} {line}") + line_number += 1 + + elif option == "b": + if line != "": + print(f"{line_number} {line}") + line_number += 1 + else: + print("") + + else: + print(line) \ No newline at end of file From e862624c350fcafad63cdbf82d3e4f073552c65d Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Tue, 19 May 2026 23:21:20 +0100 Subject: [PATCH 4/8] Add basic ls -1 functionality --- implement-shell-tools/ls/ls.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 implement-shell-tools/ls/ls.py diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..929cff9b5 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,16 @@ +import sys +import os + +args = sys.argv[1:] + +# default folder = current folder +path = "." + +# if user gives a folder +if len(args) > 1: + path = args[1] + +files = os.listdir(path) + +for file in files: + print(file) \ No newline at end of file From 5fe87a03b55401f18ffe8a24846b3ec732cae30f Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Tue, 19 May 2026 23:24:08 +0100 Subject: [PATCH 5/8] Add -a flag to show hidden files --- implement-shell-tools/ls/ls.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py index 929cff9b5..9feabcb06 100644 --- a/implement-shell-tools/ls/ls.py +++ b/implement-shell-tools/ls/ls.py @@ -3,14 +3,22 @@ args = sys.argv[1:] -# default folder = current folder +show_all = False path = "." -# if user gives a folder -if len(args) > 1: - path = args[1] +for arg in args: + + if arg == "-a": + show_all = True + + elif arg != "-1": + path = arg files = os.listdir(path) for file in files: + + if not show_all and file.startswith("."): + continue + print(file) \ No newline at end of file From afbb144d38aed93a8eb1422a39d3f7bc08eeae9e Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Tue, 19 May 2026 23:29:22 +0100 Subject: [PATCH 6/8] Add basic wc for single file --- implement-shell-tools/wc/wc.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 implement-shell-tools/wc/wc.py diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..727527094 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,13 @@ +import sys + +args = sys.argv[1:] +path = args[0] + +with open(path, "r") as file: + content = file.read() + +lines = content.split("\n") +words = content.split(" ") +chars = content + +print(len(lines), len(words), len(chars)) \ No newline at end of file From 65425b49acd85f9e7cc80c005b4011dd55b6438b Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Tue, 19 May 2026 23:30:56 +0100 Subject: [PATCH 7/8] Support multiple files in wc --- implement-shell-tools/wc/wc.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index 727527094..befbd2186 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -1,13 +1,13 @@ import sys args = sys.argv[1:] -path = args[0] -with open(path, "r") as file: - content = file.read() +for path in args: + with open(path, "r") as file: + content = file.read() -lines = content.split("\n") -words = content.split(" ") -chars = content + lines = content.split("\n") + words = content.split(" ") + chars = content -print(len(lines), len(words), len(chars)) \ No newline at end of file + print(len(lines), len(words), len(chars), path) \ No newline at end of file From f2ad18acb68bef7163c23360317814224412c487 Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Tue, 19 May 2026 23:32:59 +0100 Subject: [PATCH 8/8] Add -l -w -c flags to wc --- implement-shell-tools/wc/wc.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index befbd2186..e7c7fdad5 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -2,12 +2,32 @@ args = sys.argv[1:] -for path in args: +option = "all" +paths = [] + +for arg in args: + if arg == "-l": + option = "l" + elif arg == "-w": + option = "w" + elif arg == "-c": + option = "c" + else: + paths.append(arg) + +for path in paths: with open(path, "r") as file: content = file.read() - lines = content.split("\n") - words = content.split(" ") - chars = content + lines = len(content.split("\n")) + words = len(content.split(" ")) + chars = len(content) - print(len(lines), len(words), len(chars), path) \ No newline at end of file + if option == "l": + print(lines, path) + elif option == "w": + print(words, path) + elif option == "c": + print(chars, path) + else: + print(lines, words, chars, path) \ No newline at end of file