From 64e23bdf2b13476976d5e95915c0554460184ab0 Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Thu, 16 Jul 2026 14:08:41 +0100 Subject: [PATCH 1/5] Implement cat command with -n and -b support --- implement-shell-tools/cat/cat.js | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) 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..60048bf84 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,50 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +let flag = ""; +let lineNumber = 1; + +for (const arg of args) { + + // Check for flags + if (arg === "-n" || arg === "-b") { + flag = arg; + continue; + } + + // Read the file + const content = fs.readFileSync(arg, "utf8"); + + // split into lines + const lines = content.split("\n"); + + // Remove the extra empty line if the file ends with a new line + if (lines[lines.length - 1] === "") { + lines.pop(); + } + + + for (const line of lines) { + + if (flag === "-n") { + console.log(`${lineNumber}\t${line}`); + lineNumber++; + } + + else if (flag === "-b") { + + if (line === "") { + console.log(""); + } else { + console.log(`${String(lineNumber).padStart(6)} ${line}`); + lineNumber++; + } + + } + + else { + console.log(line); + } + } +} \ No newline at end of file From 47fe6338a1eb1d7a7b675eabdc694b97f4beac7c Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Sun, 19 Jul 2026 11:56:34 +0100 Subject: [PATCH 2/5] implementing ls and wc commands --- implement-shell-tools/ls/ls.js | 41 ++++++++++++++++++++++++++++++ implement-shell-tools/wc/wc.js | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 implement-shell-tools/ls/ls.js create mode 100644 implement-shell-tools/wc/wc.js diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..6fc7559b2 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,41 @@ +const fs = require("fs"); +const path = require("path"); +const args = process.argv.slice(2); + +let onePerLine = false; +let showAll = false; +let target = "."; + +for (const arg of args) { + + if (arg === "-1") { + onePerLine = true; + } + else if (arg === "-a") { + showAll = true; + } + else { + target = arg; + } +} + +const stats = fs.statSync(target); +if (stats.isFile()) { + console.log(path.basename(target)); +} else { + + let files = fs.readdirSync(target); + + if (!showAll) { + files = files.filter(file => !file.startsWith(".")); + } + if (onePerLine) { + + for (const file of files) { + console.log(file); + } + + } else { + console.log(files.join(" ")); + } +} \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..2369d0a97 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,46 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +let showWords = false; +let showChars = false; +let showLines = false; +let fileNames = []; + +// Check flags and file names +for (let item of args) { + if (item === "-l") { + showLines = true; + } else if (item === "-w") { + showWords = true; + } else if (item === "-c") { + showChars = true; + } else { + fileNames.push(item); + } +} + +// If no flags are given, show everything +if (!showLines && !showWords && !showChars) { + showLines = true; + showWords = true; + showChars = true; +} + +for (let fileName of fileNames) { + let text = fs.readFileSync(fileName, "utf8"); + + let totalLines = text.split("\n").length - 1; + let totalWords = text.trim().split(/\s+/).length; + let totalChars = Buffer.byteLength(text); + + let output = ""; + + if (showLines) output += totalLines + " "; + if (showWords) output += totalWords + " "; + if (showChars) output += totalChars + " "; + + output += fileName; + + console.log(output); +} \ No newline at end of file From acb688351a0b029b0e2c1652a64b61f08184528a Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Fri, 7 Aug 2026 19:04:23 +0100 Subject: [PATCH 3/5] refactoring the cat.js file --- implement-shell-tools/cat/cat.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index 60048bf84..39bc28e15 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -12,7 +12,7 @@ for (const arg of args) { flag = arg; continue; } - + let lineNumber = 1; // Read the file const content = fs.readFileSync(arg, "utf8"); @@ -27,21 +27,17 @@ for (const arg of args) { for (const line of lines) { - if (flag === "-n") { - console.log(`${lineNumber}\t${line}`); - lineNumber++; - } - - else if (flag === "-b") { - - if (line === "") { - console.log(""); - } else { - console.log(`${String(lineNumber).padStart(6)} ${line}`); - lineNumber++; - } - - } + if (flag === "-n") { + console.log(`${String(lineNumber).padStart(6)} ${line}`);; + lineNumber++; +} else if (flag === "-b") { + if (line === "") { + console.log(""); + } else { + console.log(`${String(lineNumber).padStart(6)} ${line}`);; + lineNumber++; + } +} else { console.log(line); From d66bde8d6d603588b9c0ae4c02a2b2f67a51f428 Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Fri, 7 Aug 2026 19:31:02 +0100 Subject: [PATCH 4/5] Implementing ls behavior for files, -1, and -a --- implement-shell-tools/ls/ls.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js index 6fc7559b2..27f44fa1f 100644 --- a/implement-shell-tools/ls/ls.js +++ b/implement-shell-tools/ls/ls.js @@ -21,21 +21,22 @@ for (const arg of args) { const stats = fs.statSync(target); if (stats.isFile()) { - console.log(path.basename(target)); + console.log(target); } else { let files = fs.readdirSync(target); - if (!showAll) { - files = files.filter(file => !file.startsWith(".")); - } - if (onePerLine) { - - for (const file of files) { - console.log(file); - } +if (showAll) { + files = [".", "..", ...files]; +} else { + files = files.filter(file => !file.startsWith(".")); +} - } else { - console.log(files.join(" ")); +if (onePerLine) { + for (const file of files) { + console.log(file); } +} else { + console.log(files.join(" ")); +} } \ No newline at end of file From bc953668e85a9505868fcbbabc9672d4afb5d27d Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Fri, 7 Aug 2026 19:49:05 +0100 Subject: [PATCH 5/5] Match wc output formatting and support total counts --- implement-shell-tools/wc/wc.js | 43 ++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js index 2369d0a97..9bc0a151e 100644 --- a/implement-shell-tools/wc/wc.js +++ b/implement-shell-tools/wc/wc.js @@ -8,7 +8,7 @@ let showLines = false; let fileNames = []; // Check flags and file names -for (let item of args) { +for (const item of args) { if (item === "-l") { showLines = true; } else if (item === "-w") { @@ -27,20 +27,43 @@ if (!showLines && !showWords && !showChars) { showChars = true; } -for (let fileName of fileNames) { - let text = fs.readFileSync(fileName, "utf8"); +let grandLines = 0; +let grandWords = 0; +let grandChars = 0; - let totalLines = text.split("\n").length - 1; - let totalWords = text.trim().split(/\s+/).length; - let totalChars = Buffer.byteLength(text); +for (const fileName of fileNames) { + const text = fs.readFileSync(fileName, "utf8"); + const totalLines = text.split("\n").length - 1; + + const trimmed = text.trim(); + const totalWords = trimmed ? trimmed.split(/\s+/).length : 0; + + const totalChars = Buffer.byteLength(text); + + grandLines += totalLines; + grandWords += totalWords; + grandChars += totalChars; + + let output = ""; + + if (showLines) output += String(totalLines).padStart(8); + if (showWords) output += String(totalWords).padStart(8); + if (showChars) output += String(totalChars).padStart(8); + + output += " " + fileName; + + console.log(output); +} + +if (fileNames.length > 1) { let output = ""; - if (showLines) output += totalLines + " "; - if (showWords) output += totalWords + " "; - if (showChars) output += totalChars + " "; + if (showLines) output += String(grandLines).padStart(8); + if (showWords) output += String(grandWords).padStart(8); + if (showChars) output += String(grandChars).padStart(8); - output += fileName; + output += " total"; console.log(output); } \ No newline at end of file