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..d62d766a4 --- /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); \ No newline at end of file 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(" ")); 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");