Skip to content
Open
54 changes: 54 additions & 0 deletions implement-shell-tools/cat/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import process from "node:process";
import { promises as fs } from "node:fs";

const args = process.argv.slice(2);

let option = "none";
const paths = [];


for (const arg of args) {
if (arg === "-n") {
option = "n";
} else if (arg === "-b") {
option = "b";
} else {
paths.push(arg);
}
}

if (paths.length === 0) {
console.error("Expected at least one file path.");
process.exit(1);
}

for (const path of paths) {
const content = await fs.readFile(path, "utf-8");

const lines = content.split("\n");

let lineNumber = 1;

for (const line of lines) {

if (option === "n") {
console.log(`${lineNumber} ${line}`);
lineNumber++;
}

else if (option === "b") {

if (line !== "") {
console.log(`${lineNumber} ${line}`);
lineNumber++;
} else {
console.log("");
}

}

else {
console.log(line);
}
}
}
32 changes: 32 additions & 0 deletions implement-shell-tools/ls/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import process from "node:process";
import { promises as fs } from "node:fs";

const args = process.argv.slice(2);

let showAll = false;
let path = ".";


for (const arg of args) {
if (arg === "-a") {
showAll = true;
} else if (arg === "-1") {
// do nothing: already printing one file per line
path = arg;
}
}


const files = await fs.readdir(path);

for (const file of files) {

if (!showAll && file.startsWith(".")) {
continue;
}

console.log(file);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you correctly handling the -1 argument?

@Zobeir-Rigi Zobeir-Rigi Jun 18, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve fixed it. -1 is handled now. When it’s used, nothing changes since console.log already prints one per line.

}

// We already print one file per line because
// console.log(file) automatically puts each file on its own line.
44 changes: 44 additions & 0 deletions implement-shell-tools/wc/wc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import process from "node:process";
import { promises as fs } from "node:fs";

const args = process.argv.slice(2);

let countLines = false;
let countWords = false;
let countChars = false;
const paths = [];

// parse args
for (const arg of args) {
if (arg === "-l") countLines = true;
else if (arg === "-w") countWords = true;
else if (arg === "-c") countChars = true;
else paths.push(arg);
}

if (paths.length === 0) {
console.error("Please provide at least one file");
process.exit(1);
}

for (const path of paths) {
const content = await fs.readFile(path, "utf-8");

const lines = content.split("\n").length;
const words = content.trim().split(/\s+/).length;
const chars = content.length;

// if no flags → show all
if (!countLines && !countWords && !countChars) {
console.log(lines, words, chars, path);
continue;
}

const output = [];

if (countLines) output.push(lines);
if (countWords) output.push(words);
if (countChars) output.push(chars);

console.log(...output, path);
}