From 6f7c430390354f13a1fa1cc2075a1de793afeaa4 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Thu, 16 Jul 2026 21:05:10 +0100 Subject: [PATCH 01/10] Implement cat tool with -n and -b flags --- implement-shell-tools/cat/cat*.js | 6 ++++++ implement-shell-tools/cat/cat-b.js | 12 ++++++++++++ implement-shell-tools/cat/cat-n*txt.js | 9 +++++++++ implement-shell-tools/cat/cat-n.js | 8 ++++++++ implement-shell-tools/cat/cat.js | 6 ++++++ 5 files changed, 41 insertions(+) create mode 100644 implement-shell-tools/cat/cat*.js create mode 100644 implement-shell-tools/cat/cat-b.js create mode 100644 implement-shell-tools/cat/cat-n*txt.js create mode 100644 implement-shell-tools/cat/cat-n.js create mode 100644 implement-shell-tools/cat/cat.js diff --git a/implement-shell-tools/cat/cat*.js b/implement-shell-tools/cat/cat*.js new file mode 100644 index 000000000..ab543e2ff --- /dev/null +++ b/implement-shell-tools/cat/cat*.js @@ -0,0 +1,6 @@ +const fs = require("fs"); +const folderPath = "./sample-files"; +const content = fs.readdirSync(folderPath); + +const txtFiles = content.filter((file) => file.endsWith(".txt")); +process.stdout.write(txtFiles.join("\n")); diff --git a/implement-shell-tools/cat/cat-b.js b/implement-shell-tools/cat/cat-b.js new file mode 100644 index 000000000..690914cad --- /dev/null +++ b/implement-shell-tools/cat/cat-b.js @@ -0,0 +1,12 @@ +const fs = require("fs"); +const filePath = "./sample-files/3.txt"; +const content = fs.readFileSync(filePath, "utf-8"); + +let count = 1; +const numberLines = content.split("\n").map((line) => { + if (line.trim() !== "") { + return `${count++} ${line.trim()}`; + } + return ""; +}); +process.stdout.write(numberLines.join("\n")); diff --git a/implement-shell-tools/cat/cat-n*txt.js b/implement-shell-tools/cat/cat-n*txt.js new file mode 100644 index 000000000..1602ce95f --- /dev/null +++ b/implement-shell-tools/cat/cat-n*txt.js @@ -0,0 +1,9 @@ +const fs = require("fs"); +const folderPath = "./sample-files"; +const content = fs.readdirSync(folderPath); + +const txtFiles = content.filter((file) => file.endsWith(".txt")); +const numberedLine = txtFiles.map((line, index) => { + return `${index + 1} ${line}`; +}); +process.stdout.write(numberedLine.join("\n")); diff --git a/implement-shell-tools/cat/cat-n.js b/implement-shell-tools/cat/cat-n.js new file mode 100644 index 000000000..e6ba5a309 --- /dev/null +++ b/implement-shell-tools/cat/cat-n.js @@ -0,0 +1,8 @@ +const fs = require("fs"); +const filePath = "./sample-files/1.txt"; +const content = fs.readFileSync(filePath, "utf-8"); + +const lineNumber = content.split("\n").map((line, index) => { + return `${index + 1} ${line}`; +}); +process.stdout.write(lineNumber.join("\n")); diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..29ba335be --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,6 @@ +const fs = require("fs"); +const filePath = "./sample-files/1.txt"; + +const content = fs.readFileSync(filePath, "utf-8"); + +process.stdout.write(content); From 5aebf47da2455a6477c81b079c831a95fda78695 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Mon, 20 Jul 2026 13:06:35 +0100 Subject: [PATCH 02/10] updated cat.js --- implement-shell-tools/cat/cat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index 29ba335be..d62d766a4 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -3,4 +3,4 @@ const filePath = "./sample-files/1.txt"; const content = fs.readFileSync(filePath, "utf-8"); -process.stdout.write(content); +process.stdout.write(content); \ No newline at end of file From 006540bc4f5a0a3fddc29e2f249d65920cfe2dd3 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Mon, 20 Jul 2026 20:16:09 +0100 Subject: [PATCH 03/10] Implement ls tool examples --- implement-shell-tools/ls/ls-1-a.js | 8 ++++++++ implement-shell-tools/ls/ls-1-sample-files.js | 5 +++++ implement-shell-tools/ls/ls-1.js | 4 ++++ implement-shell-tools/ls/ls-sample-1txt.js | 4 ++++ implement-shell-tools/ls/ls-sample-files*.js | 12 ++++++++++++ implement-shell-tools/ls/ls-sample-files.js | 5 +++++ 6 files changed, 38 insertions(+) create mode 100644 implement-shell-tools/ls/ls-1-a.js create mode 100644 implement-shell-tools/ls/ls-1-sample-files.js create mode 100644 implement-shell-tools/ls/ls-1.js create mode 100644 implement-shell-tools/ls/ls-sample-1txt.js create mode 100644 implement-shell-tools/ls/ls-sample-files*.js create mode 100644 implement-shell-tools/ls/ls-sample-files.js diff --git a/implement-shell-tools/ls/ls-1-a.js b/implement-shell-tools/ls/ls-1-a.js new file mode 100644 index 000000000..e9d91bc7e --- /dev/null +++ b/implement-shell-tools/ls/ls-1-a.js @@ -0,0 +1,8 @@ +const fs = require("fs"); +const path = "./sample-files"; +const content = fs.readdirSync(path); +console.log("."); +console.log(".."); + +const files = content.map((item) => item); +console.log(files.join("\n")); diff --git a/implement-shell-tools/ls/ls-1-sample-files.js b/implement-shell-tools/ls/ls-1-sample-files.js new file mode 100644 index 000000000..c50bb052e --- /dev/null +++ b/implement-shell-tools/ls/ls-1-sample-files.js @@ -0,0 +1,5 @@ +const fs = require("fs"); +const path = "./sample-files"; +const contents = fs.readdirSync(path).filter((file) => !file.startsWith(".")); + +console.log(contents.join("\n")); diff --git a/implement-shell-tools/ls/ls-1.js b/implement-shell-tools/ls/ls-1.js new file mode 100644 index 000000000..7ee332ad6 --- /dev/null +++ b/implement-shell-tools/ls/ls-1.js @@ -0,0 +1,4 @@ +const fs = require("fs"); +const path = "../ls"; +const files = fs.readdirSync(path); +console.log(files.join("\n")); diff --git a/implement-shell-tools/ls/ls-sample-1txt.js b/implement-shell-tools/ls/ls-sample-1txt.js new file mode 100644 index 000000000..ec9387f7c --- /dev/null +++ b/implement-shell-tools/ls/ls-sample-1txt.js @@ -0,0 +1,4 @@ +const fs = require("fs"); +const path = "./sample-files/1.txt"; + +console.log(path); diff --git a/implement-shell-tools/ls/ls-sample-files*.js b/implement-shell-tools/ls/ls-sample-files*.js new file mode 100644 index 000000000..1d7113895 --- /dev/null +++ b/implement-shell-tools/ls/ls-sample-files*.js @@ -0,0 +1,12 @@ +const fs = require("fs"); +const path = "./sample-files"; +const directories = fs.readdirSync(path); + +directories.forEach((file) => { + if (file === "dir") { + console.log("dir:"); + console.log(fs.readdirSync("./sample-files/dir").join(" ")); + } else { + console.log(file); + } +}); diff --git a/implement-shell-tools/ls/ls-sample-files.js b/implement-shell-tools/ls/ls-sample-files.js new file mode 100644 index 000000000..3b087d116 --- /dev/null +++ b/implement-shell-tools/ls/ls-sample-files.js @@ -0,0 +1,5 @@ +const fs = require("fs"); +const folderPath = "./sample-files"; +const contents = fs.readdirSync(folderPath); + +console.log(contents.join(" ")); From ddcf9aedc3b35f8af9e72f7dc5e63983b0e3fffa Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 21 Jul 2026 17:50:55 +0100 Subject: [PATCH 04/10] Implement wc tool with line word and character counts --- implement-shell-tools/wc/wc-3txt.js | 9 ++++++ .../wc/wc-character-count.js | 9 ++++++ .../wc/wc-count-lines-words-total.js | 19 +++++++++++++ .../wc/wc-count-lines-words.js | 8 ++++++ implement-shell-tools/wc/wc-lines-files.js | 16 +++++++++++ implement-shell-tools/wc/wc-word-count.js | 7 +++++ implement-shell-tools/wc/wc.js | 28 +++++++++++++++++++ 7 files changed, 96 insertions(+) create mode 100644 implement-shell-tools/wc/wc-3txt.js create mode 100644 implement-shell-tools/wc/wc-character-count.js create mode 100644 implement-shell-tools/wc/wc-count-lines-words-total.js create mode 100644 implement-shell-tools/wc/wc-count-lines-words.js create mode 100644 implement-shell-tools/wc/wc-lines-files.js create mode 100644 implement-shell-tools/wc/wc-word-count.js create mode 100644 implement-shell-tools/wc/wc.js diff --git a/implement-shell-tools/wc/wc-3txt.js b/implement-shell-tools/wc/wc-3txt.js new file mode 100644 index 000000000..a89bf8649 --- /dev/null +++ b/implement-shell-tools/wc/wc-3txt.js @@ -0,0 +1,9 @@ +const fs = require("fs"); +const file = "./sample-files/3.txt"; +const content = fs.readFileSync(file, "utf-8"); + +let lines = 0; + +const line = content.split("\n").length - 1; +lines += line; +console.log(lines, file); diff --git a/implement-shell-tools/wc/wc-character-count.js b/implement-shell-tools/wc/wc-character-count.js new file mode 100644 index 000000000..e10289bef --- /dev/null +++ b/implement-shell-tools/wc/wc-character-count.js @@ -0,0 +1,9 @@ +const fs = require("fs"); +const file = "./sample-files/3.txt"; +const content = fs.readFileSync(file, "utf-8"); + +let characters = 0; + +const char = content.length; +characters += char; +console.log(characters, file); diff --git a/implement-shell-tools/wc/wc-count-lines-words-total.js b/implement-shell-tools/wc/wc-count-lines-words-total.js new file mode 100644 index 000000000..8ecd4fb32 --- /dev/null +++ b/implement-shell-tools/wc/wc-count-lines-words-total.js @@ -0,0 +1,19 @@ +const fs = require("fs"); +const folder = "./sample-files"; +const content = fs.readdirSync(folder); + +let totalLines = 0; +let totalWords = 0; +content.forEach((file) => { + const path = `${folder}/${file}`; + const filePath = fs.readFileSync(path, "utf-8"); + + const line = filePath.split("\n").length - 1; + const word = filePath.split(/\s+/).length - 1; + + totalLines += line; + totalWords += word; + console.log(line, word, path); +}); + +console.log(totalLines, totalWords, "total"); diff --git a/implement-shell-tools/wc/wc-count-lines-words.js b/implement-shell-tools/wc/wc-count-lines-words.js new file mode 100644 index 000000000..a4a5e8d7d --- /dev/null +++ b/implement-shell-tools/wc/wc-count-lines-words.js @@ -0,0 +1,8 @@ +const fs = require("fs"); +const file = "./sample-files/3.txt"; +const content = fs.readFileSync(file, "utf-8"); + +const line = content.split("\n").length - 1; +const word = content.split(/\s+/).length - 1; + +console.log(line, word, file); diff --git a/implement-shell-tools/wc/wc-lines-files.js b/implement-shell-tools/wc/wc-lines-files.js new file mode 100644 index 000000000..a4067b08d --- /dev/null +++ b/implement-shell-tools/wc/wc-lines-files.js @@ -0,0 +1,16 @@ +const fs = require("fs"); +const folder = "./sample-files"; +const filesPath = fs.readdirSync(folder); + +let totalLines = 0; + +filesPath.forEach((file) => { + const path = `${folder}/${file}`; + const files = fs.readFileSync(path, "utf-8"); + + const lines = files.split("\n").length - 1; + console.log(lines, path); + + totalLines += lines; +}); +console.log(totalLines, "total"); diff --git a/implement-shell-tools/wc/wc-word-count.js b/implement-shell-tools/wc/wc-word-count.js new file mode 100644 index 000000000..ea619eaa8 --- /dev/null +++ b/implement-shell-tools/wc/wc-word-count.js @@ -0,0 +1,7 @@ +const fs = require("fs"); +const file = "./sample-files/3.txt"; +const content = fs.readFileSync(file, "utf-8"); + +const word = content.split(/\s+/).length - 1; + +console.log(word, file); diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..0c116983a --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,28 @@ +// Power shell commands =======> Vs <===== Js codes + +// wc sample-files/* + +const fs = require("fs"); +const filePath = "./sample-files"; +const contents = fs.readdirSync(filePath); + +let totalLines = 0; +let totalWords = 0; +let totalCharacters = 0; + +contents.forEach((file) => { + const path = `${filePath}/${file}`; + + const files = fs.readFileSync(path, "utf-8"); + + const lines = files.split("\n").length - 1; + const words = files.trim().split(/\s+/).length; + const chars = files.length; + + console.log(lines, words, chars, path); + + totalLines += lines; + totalWords += words; + totalCharacters += chars; +}); +console.log(totalLines, totalWords, totalCharacters, "total"); From ac900ffdfa1439ec6d79abba0f157166c57130f7 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Thu, 30 Jul 2026 00:52:54 +0100 Subject: [PATCH 05/10] Implement cat tool with Nodejs --- implement-shell-tools/cat/README.md | 10 +++++----- implement-shell-tools/cat/cat.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/implement-shell-tools/cat/README.md b/implement-shell-tools/cat/README.md index 7284a5e67..71a08ca9b 100644 --- a/implement-shell-tools/cat/README.md +++ b/implement-shell-tools/cat/README.md @@ -6,11 +6,11 @@ Your task is to implement your own version of `cat`. It must act the same as `cat` would, if run from the directory containing this README.md file, for the following command lines: -* `cat sample-files/1.txt` -* `cat -n sample-files/1.txt` -* `cat sample-files/*.txt` -* `cat -n sample-files/*.txt` -* `cat -b sample-files/3.txt` +- `cat sample-files/1.txt` +- `cat -n sample-files/1.txt` +- `cat sample-files/*.txt` +- `cat -n sample-files/*.txt` +- `cat -b sample-files/3.txt` Matching any additional behaviours or flags are optional stretch goals. diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index d62d766a4..d3461319f 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -1,6 +1,6 @@ const fs = require("fs"); const filePath = "./sample-files/1.txt"; -const content = fs.readFileSync(filePath, "utf-8"); +const content = fs.readdirSync(filePath, "utf-8"); -process.stdout.write(content); \ No newline at end of file +process.stdout.write(content); From b450b0613635bb0188b71423b8fccef8e50c25ff Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 28 Jul 2026 20:58:45 +0100 Subject: [PATCH 06/10] Implement cat command with argument handling --- implement-shell-tools/cat/cat-b.js | 8 +++++-- implement-shell-tools/cat/cat-n.js | 6 ++++- .../cat/cat-readAllFiles-withLines.js | 23 +++++++++++++++++++ implement-shell-tools/cat/cat-readAllFiles.js | 7 ++++++ implement-shell-tools/cat/cat.js | 6 +++-- 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 implement-shell-tools/cat/cat-readAllFiles-withLines.js create mode 100644 implement-shell-tools/cat/cat-readAllFiles.js diff --git a/implement-shell-tools/cat/cat-b.js b/implement-shell-tools/cat/cat-b.js index 690914cad..ea322a8be 100644 --- a/implement-shell-tools/cat/cat-b.js +++ b/implement-shell-tools/cat/cat-b.js @@ -1,6 +1,10 @@ const fs = require("fs"); -const filePath = "./sample-files/3.txt"; -const content = fs.readFileSync(filePath, "utf-8"); +const args = process.argv.slice(2); + +const flag = args[0]; +const file = args[1]; + +const content = fs.readFileSync(file, "utf-8"); let count = 1; const numberLines = content.split("\n").map((line) => { diff --git a/implement-shell-tools/cat/cat-n.js b/implement-shell-tools/cat/cat-n.js index e6ba5a309..b05540404 100644 --- a/implement-shell-tools/cat/cat-n.js +++ b/implement-shell-tools/cat/cat-n.js @@ -1,5 +1,9 @@ const fs = require("fs"); -const filePath = "./sample-files/1.txt"; +const args = process.argv.slice(2); + +const flag = args[0]; +const filePath = args[1]; + const content = fs.readFileSync(filePath, "utf-8"); const lineNumber = content.split("\n").map((line, index) => { diff --git a/implement-shell-tools/cat/cat-readAllFiles-withLines.js b/implement-shell-tools/cat/cat-readAllFiles-withLines.js new file mode 100644 index 000000000..08dff4e9a --- /dev/null +++ b/implement-shell-tools/cat/cat-readAllFiles-withLines.js @@ -0,0 +1,23 @@ +const fs = require("fs"); + +const args = process.argv; + +const flag = args[2]; +const filePaths = args.slice(3); + +let lineNumber = 1; + +const content = filePaths + .map((file) => fs.readFileSync(file, "utf-8")) + .join(""); + +const lines = content.split(/\r?\n/); +if (lines[lines.length - 1] === "") { + lines.pop(); +} + +const numberedLines = lines.map((line) => { + return `${lineNumber++} ${line}`; +}); + +process.stdout.write(numberedLines.join("\n")); diff --git a/implement-shell-tools/cat/cat-readAllFiles.js b/implement-shell-tools/cat/cat-readAllFiles.js new file mode 100644 index 000000000..2aeffb4cf --- /dev/null +++ b/implement-shell-tools/cat/cat-readAllFiles.js @@ -0,0 +1,7 @@ +const fs = require("fs"); +const args = process.argv.slice(2); + +const fileContents = args.map((file) => { + return fs.readFileSync(file, "utf-8"); +}); +process.stdout.write(fileContents.join("")); diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index d3461319f..2eedb056b 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -1,6 +1,8 @@ const fs = require("fs"); -const filePath = "./sample-files/1.txt"; +const args = process.argv.slice(2); -const content = fs.readdirSync(filePath, "utf-8"); +const filePath = args[0]; + +const content = fs.readFileSync(filePath, "utf-8"); process.stdout.write(content); From 1ef8fc7899e84ef114540be5ef7fc675db110097 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 29 Jul 2026 15:53:46 +0100 Subject: [PATCH 07/10] Refactor cat into a single command-line tool --- implement-shell-tools/cat/cat.js | 45 +++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index 2eedb056b..5961a6c39 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -1,8 +1,47 @@ const fs = require("fs"); + const args = process.argv.slice(2); -const filePath = args[0]; +let flag = ""; +let filePaths = []; + +if (args[0] && args[0].startsWith("-")) { + flag = args[0]; + filePaths = args.slice(1); +} else { + filePaths = args; +} + +// Read all files into one string +let content = filePaths.map((file) => fs.readFileSync(file, "utf-8")).join(""); + +// Split into lines +let lines = content.split(/\r?\n/); + +// Remove the last empty line if the file ends with a newline +if (lines[lines.length - 1] === "") { + lines.pop(); +} + +// Handle flag ==> -n adding number to the line; +if (flag === "-n") { + lines = lines.map((line, index) => { + return `${index + 1} ${line}`; + }); +} + +// Handle flag ==> -b avoids adding number to empty line +if (flag === "-b") { + let lineNumber = 1; + + lines = lines.map((line) => { + if (line.trim() === "") { + return line; + } + + return `${lineNumber++} ${line}`; + }); +} -const content = fs.readFileSync(filePath, "utf-8"); +process.stdout.write(lines.join("\n")); -process.stdout.write(content); From e366e2001912d519ab49a461cf0ba64574b2d5dc Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 29 Jul 2026 21:13:25 +0100 Subject: [PATCH 08/10] refactored ls implementation in a single file --- implement-shell-tools/ls/ls-1-a.js | 8 ---- implement-shell-tools/ls/ls-1-sample-files.js | 5 --- implement-shell-tools/ls/ls-1.js | 4 -- implement-shell-tools/ls/ls-sample-1txt.js | 4 -- implement-shell-tools/ls/ls-sample-files*.js | 12 ------ implement-shell-tools/ls/ls-sample-files.js | 5 --- implement-shell-tools/ls/ls.js | 40 +++++++++++++++++++ 7 files changed, 40 insertions(+), 38 deletions(-) delete mode 100644 implement-shell-tools/ls/ls-1-a.js delete mode 100644 implement-shell-tools/ls/ls-1-sample-files.js delete mode 100644 implement-shell-tools/ls/ls-1.js delete mode 100644 implement-shell-tools/ls/ls-sample-1txt.js delete mode 100644 implement-shell-tools/ls/ls-sample-files*.js delete mode 100644 implement-shell-tools/ls/ls-sample-files.js create mode 100644 implement-shell-tools/ls/ls.js diff --git a/implement-shell-tools/ls/ls-1-a.js b/implement-shell-tools/ls/ls-1-a.js deleted file mode 100644 index e9d91bc7e..000000000 --- a/implement-shell-tools/ls/ls-1-a.js +++ /dev/null @@ -1,8 +0,0 @@ -const fs = require("fs"); -const path = "./sample-files"; -const content = fs.readdirSync(path); -console.log("."); -console.log(".."); - -const files = content.map((item) => item); -console.log(files.join("\n")); diff --git a/implement-shell-tools/ls/ls-1-sample-files.js b/implement-shell-tools/ls/ls-1-sample-files.js deleted file mode 100644 index c50bb052e..000000000 --- a/implement-shell-tools/ls/ls-1-sample-files.js +++ /dev/null @@ -1,5 +0,0 @@ -const fs = require("fs"); -const path = "./sample-files"; -const contents = fs.readdirSync(path).filter((file) => !file.startsWith(".")); - -console.log(contents.join("\n")); diff --git a/implement-shell-tools/ls/ls-1.js b/implement-shell-tools/ls/ls-1.js deleted file mode 100644 index 7ee332ad6..000000000 --- a/implement-shell-tools/ls/ls-1.js +++ /dev/null @@ -1,4 +0,0 @@ -const fs = require("fs"); -const path = "../ls"; -const files = fs.readdirSync(path); -console.log(files.join("\n")); diff --git a/implement-shell-tools/ls/ls-sample-1txt.js b/implement-shell-tools/ls/ls-sample-1txt.js deleted file mode 100644 index ec9387f7c..000000000 --- a/implement-shell-tools/ls/ls-sample-1txt.js +++ /dev/null @@ -1,4 +0,0 @@ -const fs = require("fs"); -const path = "./sample-files/1.txt"; - -console.log(path); diff --git a/implement-shell-tools/ls/ls-sample-files*.js b/implement-shell-tools/ls/ls-sample-files*.js deleted file mode 100644 index 1d7113895..000000000 --- a/implement-shell-tools/ls/ls-sample-files*.js +++ /dev/null @@ -1,12 +0,0 @@ -const fs = require("fs"); -const path = "./sample-files"; -const directories = fs.readdirSync(path); - -directories.forEach((file) => { - if (file === "dir") { - console.log("dir:"); - console.log(fs.readdirSync("./sample-files/dir").join(" ")); - } else { - console.log(file); - } -}); diff --git a/implement-shell-tools/ls/ls-sample-files.js b/implement-shell-tools/ls/ls-sample-files.js deleted file mode 100644 index 3b087d116..000000000 --- a/implement-shell-tools/ls/ls-sample-files.js +++ /dev/null @@ -1,5 +0,0 @@ -const fs = require("fs"); -const folderPath = "./sample-files"; -const contents = fs.readdirSync(folderPath); - -console.log(contents.join(" ")); diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..eda1e0e14 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,40 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +let flags = []; +let paths = []; + +// Check arguments: separate flags and paths +args.forEach((arg) => { + if (arg.startsWith("-")) { + flags.push(arg); + } else { + paths.push(arg); + } +}); + +// If no path is provided, use the current directory +if (paths.length === 0) { + paths = ["."]; +} + +paths.forEach((path) => { + if (fs.statSync(path).isFile()) { + console.log(path); + } else { + let contents = fs.readdirSync(path); + + // Hide hidden files unless -a exists + if (!flags.includes("-a")) { + contents = contents.filter((file) => !file.startsWith(".")); + } + + // Check -1 flag + if (flags.includes("-1")) { + console.log(contents.join("\n")); + } else { + console.log(contents.join(" ")); + } + } +}); From 54fe1c03292fec0bdecbb46bd76c89a7e494fbfa Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 29 Jul 2026 21:14:15 +0100 Subject: [PATCH 09/10] nothing special --- implement-shell-tools/cat/cat*.js | 6 ----- implement-shell-tools/cat/cat-b.js | 16 ------------- implement-shell-tools/cat/cat-n*txt.js | 9 -------- implement-shell-tools/cat/cat-n.js | 12 ---------- .../cat/cat-readAllFiles-withLines.js | 23 ------------------- implement-shell-tools/cat/cat-readAllFiles.js | 7 ------ 6 files changed, 73 deletions(-) delete mode 100644 implement-shell-tools/cat/cat*.js delete mode 100644 implement-shell-tools/cat/cat-b.js delete mode 100644 implement-shell-tools/cat/cat-n*txt.js delete mode 100644 implement-shell-tools/cat/cat-n.js delete mode 100644 implement-shell-tools/cat/cat-readAllFiles-withLines.js delete mode 100644 implement-shell-tools/cat/cat-readAllFiles.js diff --git a/implement-shell-tools/cat/cat*.js b/implement-shell-tools/cat/cat*.js deleted file mode 100644 index ab543e2ff..000000000 --- a/implement-shell-tools/cat/cat*.js +++ /dev/null @@ -1,6 +0,0 @@ -const fs = require("fs"); -const folderPath = "./sample-files"; -const content = fs.readdirSync(folderPath); - -const txtFiles = content.filter((file) => file.endsWith(".txt")); -process.stdout.write(txtFiles.join("\n")); diff --git a/implement-shell-tools/cat/cat-b.js b/implement-shell-tools/cat/cat-b.js deleted file mode 100644 index ea322a8be..000000000 --- a/implement-shell-tools/cat/cat-b.js +++ /dev/null @@ -1,16 +0,0 @@ -const fs = require("fs"); -const args = process.argv.slice(2); - -const flag = args[0]; -const file = args[1]; - -const content = fs.readFileSync(file, "utf-8"); - -let count = 1; -const numberLines = content.split("\n").map((line) => { - if (line.trim() !== "") { - return `${count++} ${line.trim()}`; - } - return ""; -}); -process.stdout.write(numberLines.join("\n")); diff --git a/implement-shell-tools/cat/cat-n*txt.js b/implement-shell-tools/cat/cat-n*txt.js deleted file mode 100644 index 1602ce95f..000000000 --- a/implement-shell-tools/cat/cat-n*txt.js +++ /dev/null @@ -1,9 +0,0 @@ -const fs = require("fs"); -const folderPath = "./sample-files"; -const content = fs.readdirSync(folderPath); - -const txtFiles = content.filter((file) => file.endsWith(".txt")); -const numberedLine = txtFiles.map((line, index) => { - return `${index + 1} ${line}`; -}); -process.stdout.write(numberedLine.join("\n")); diff --git a/implement-shell-tools/cat/cat-n.js b/implement-shell-tools/cat/cat-n.js deleted file mode 100644 index b05540404..000000000 --- a/implement-shell-tools/cat/cat-n.js +++ /dev/null @@ -1,12 +0,0 @@ -const fs = require("fs"); -const args = process.argv.slice(2); - -const flag = args[0]; -const filePath = args[1]; - -const content = fs.readFileSync(filePath, "utf-8"); - -const lineNumber = content.split("\n").map((line, index) => { - return `${index + 1} ${line}`; -}); -process.stdout.write(lineNumber.join("\n")); diff --git a/implement-shell-tools/cat/cat-readAllFiles-withLines.js b/implement-shell-tools/cat/cat-readAllFiles-withLines.js deleted file mode 100644 index 08dff4e9a..000000000 --- a/implement-shell-tools/cat/cat-readAllFiles-withLines.js +++ /dev/null @@ -1,23 +0,0 @@ -const fs = require("fs"); - -const args = process.argv; - -const flag = args[2]; -const filePaths = args.slice(3); - -let lineNumber = 1; - -const content = filePaths - .map((file) => fs.readFileSync(file, "utf-8")) - .join(""); - -const lines = content.split(/\r?\n/); -if (lines[lines.length - 1] === "") { - lines.pop(); -} - -const numberedLines = lines.map((line) => { - return `${lineNumber++} ${line}`; -}); - -process.stdout.write(numberedLines.join("\n")); diff --git a/implement-shell-tools/cat/cat-readAllFiles.js b/implement-shell-tools/cat/cat-readAllFiles.js deleted file mode 100644 index 2aeffb4cf..000000000 --- a/implement-shell-tools/cat/cat-readAllFiles.js +++ /dev/null @@ -1,7 +0,0 @@ -const fs = require("fs"); -const args = process.argv.slice(2); - -const fileContents = args.map((file) => { - return fs.readFileSync(file, "utf-8"); -}); -process.stdout.write(fileContents.join("")); From 13b9c0e4f885a115b6d7d8342552832f51aa0eaa Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Thu, 30 Jul 2026 00:04:54 +0100 Subject: [PATCH 10/10] Refactor wc into single command line tool --- implement-shell-tools/wc/wc-3txt.js | 9 --- .../wc/wc-character-count.js | 9 --- .../wc/wc-count-lines-words-total.js | 19 ----- .../wc/wc-count-lines-words.js | 8 --- implement-shell-tools/wc/wc-lines-files.js | 16 ----- implement-shell-tools/wc/wc-word-count.js | 7 -- implement-shell-tools/wc/wc.js | 71 +++++++++++++++---- 7 files changed, 58 insertions(+), 81 deletions(-) delete mode 100644 implement-shell-tools/wc/wc-3txt.js delete mode 100644 implement-shell-tools/wc/wc-character-count.js delete mode 100644 implement-shell-tools/wc/wc-count-lines-words-total.js delete mode 100644 implement-shell-tools/wc/wc-count-lines-words.js delete mode 100644 implement-shell-tools/wc/wc-lines-files.js delete mode 100644 implement-shell-tools/wc/wc-word-count.js diff --git a/implement-shell-tools/wc/wc-3txt.js b/implement-shell-tools/wc/wc-3txt.js deleted file mode 100644 index a89bf8649..000000000 --- a/implement-shell-tools/wc/wc-3txt.js +++ /dev/null @@ -1,9 +0,0 @@ -const fs = require("fs"); -const file = "./sample-files/3.txt"; -const content = fs.readFileSync(file, "utf-8"); - -let lines = 0; - -const line = content.split("\n").length - 1; -lines += line; -console.log(lines, file); diff --git a/implement-shell-tools/wc/wc-character-count.js b/implement-shell-tools/wc/wc-character-count.js deleted file mode 100644 index e10289bef..000000000 --- a/implement-shell-tools/wc/wc-character-count.js +++ /dev/null @@ -1,9 +0,0 @@ -const fs = require("fs"); -const file = "./sample-files/3.txt"; -const content = fs.readFileSync(file, "utf-8"); - -let characters = 0; - -const char = content.length; -characters += char; -console.log(characters, file); diff --git a/implement-shell-tools/wc/wc-count-lines-words-total.js b/implement-shell-tools/wc/wc-count-lines-words-total.js deleted file mode 100644 index 8ecd4fb32..000000000 --- a/implement-shell-tools/wc/wc-count-lines-words-total.js +++ /dev/null @@ -1,19 +0,0 @@ -const fs = require("fs"); -const folder = "./sample-files"; -const content = fs.readdirSync(folder); - -let totalLines = 0; -let totalWords = 0; -content.forEach((file) => { - const path = `${folder}/${file}`; - const filePath = fs.readFileSync(path, "utf-8"); - - const line = filePath.split("\n").length - 1; - const word = filePath.split(/\s+/).length - 1; - - totalLines += line; - totalWords += word; - console.log(line, word, path); -}); - -console.log(totalLines, totalWords, "total"); diff --git a/implement-shell-tools/wc/wc-count-lines-words.js b/implement-shell-tools/wc/wc-count-lines-words.js deleted file mode 100644 index a4a5e8d7d..000000000 --- a/implement-shell-tools/wc/wc-count-lines-words.js +++ /dev/null @@ -1,8 +0,0 @@ -const fs = require("fs"); -const file = "./sample-files/3.txt"; -const content = fs.readFileSync(file, "utf-8"); - -const line = content.split("\n").length - 1; -const word = content.split(/\s+/).length - 1; - -console.log(line, word, file); diff --git a/implement-shell-tools/wc/wc-lines-files.js b/implement-shell-tools/wc/wc-lines-files.js deleted file mode 100644 index a4067b08d..000000000 --- a/implement-shell-tools/wc/wc-lines-files.js +++ /dev/null @@ -1,16 +0,0 @@ -const fs = require("fs"); -const folder = "./sample-files"; -const filesPath = fs.readdirSync(folder); - -let totalLines = 0; - -filesPath.forEach((file) => { - const path = `${folder}/${file}`; - const files = fs.readFileSync(path, "utf-8"); - - const lines = files.split("\n").length - 1; - console.log(lines, path); - - totalLines += lines; -}); -console.log(totalLines, "total"); diff --git a/implement-shell-tools/wc/wc-word-count.js b/implement-shell-tools/wc/wc-word-count.js deleted file mode 100644 index ea619eaa8..000000000 --- a/implement-shell-tools/wc/wc-word-count.js +++ /dev/null @@ -1,7 +0,0 @@ -const fs = require("fs"); -const file = "./sample-files/3.txt"; -const content = fs.readFileSync(file, "utf-8"); - -const word = content.split(/\s+/).length - 1; - -console.log(word, file); diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js index 0c116983a..7afd91897 100644 --- a/implement-shell-tools/wc/wc.js +++ b/implement-shell-tools/wc/wc.js @@ -1,28 +1,73 @@ -// Power shell commands =======> Vs <===== Js codes - -// wc sample-files/* +// Power shell commands with Js codes const fs = require("fs"); -const filePath = "./sample-files"; -const contents = fs.readdirSync(filePath); +const args = process.argv.slice(2); + +let flags = []; +let paths = []; + +// Check arguments: separate flags and paths +args.forEach((arg) => { + if (arg.startsWith("-")) { + flags.push(arg); + } else { + paths.push(arg); + } +}); let totalLines = 0; let totalWords = 0; let totalCharacters = 0; -contents.forEach((file) => { - const path = `${filePath}/${file}`; +paths.forEach((file) => { + const content = fs.readFileSync(file, "utf-8"); + + const lines = content.split("\n").length - 1; + const words = content.trim().split(/\s+/).length; + const chars = content.length; + + if (flags.length === 0) { + console.log(lines, words, chars, file); + } else { + let output = []; - const files = fs.readFileSync(path, "utf-8"); + if (flags.includes("-l")) { + output.push(lines); + } - const lines = files.split("\n").length - 1; - const words = files.trim().split(/\s+/).length; - const chars = files.length; + if (flags.includes("-w")) { + output.push(words); + } - console.log(lines, words, chars, path); + if (flags.includes("-c")) { + output.push(chars); + } + + console.log(...output, file); + } totalLines += lines; totalWords += words; totalCharacters += chars; }); -console.log(totalLines, totalWords, totalCharacters, "total"); +if (paths.length > 1) { + let totalOutput = []; + + if (flags.length === 0) { + totalOutput.push(totalLines, totalWords, totalCharacters); + } else { + if (flags.includes("-l")) { + totalOutput.push(totalLines); + } + + if (flags.includes("-w")) { + totalOutput.push(totalWords); + } + + if (flags.includes("-c")) { + totalOutput.push(totalCharacters); + } + } + + console.log(...totalOutput, "total"); +}