Implementation of ls | wc command Last Updated : 30 Apr, 2018 Comments Improve Suggest changes Like Article Like Report ls | wc command : Using ls|wc, we can count new lines, words and letters of all files of current directory. We can see from the following after execution of the code we get same results. Prerequisites :ls command | wc command | piping in linux Approach : First, we have to use pipe for inter process communication over an array where a[0] is used for reading and a[1] is used for writing. We can replicate the process using fork. In the parent process, standard output is closed so that output of ls command will go to a[0] and similarly standard input is closed in children process. Now, if we run the program output will be as same as command of linux ls|wc. Below is the implementation of above approach : C // C code to implement ls | wc command #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include<errno.h> #include<sys/wait.h> #include <unistd.h> int main(){ // array of 2 size a[0] is for // reading and a[1] is for // writing over a pipe int a[2]; // using pipe for inter // process communication pipe(a); if(!fork()) { // closing normal stdout close(1); // making stdout same as a[1] dup(a[1]); // closing reading part of pipe // we don't need of it at this time close(a[0]); // executing ls execlp("ls","ls",NULL); } else { // closing normal stdin close(0); // making stdin same as a[0] dup(a[0]); // closing writing part in parent, // we don't need of it at this time close(a[1]); // executing wc execlp("wc","wc",NULL); } } Comment More infoAdvertise with us Next Article Implementation of ls | wc command B bhonesh chawla Follow Improve Article Tags : Linux-Unix Similar Reads ls Command in Linux The ls command is one of the most used commands in the Linux terminal to display the files and directories or path in the terminal. So, using the ls command is a basic skill for navigating the Linux file system, handling files, and managing directories.What is the ls Command in LinuxThe ls command i 10 min read wc command in Linux with examples wc stands for word count. As the name implies, it is mainly used for counting purpose.It is used to find out number of lines, word count, byte and characters count in the files specified in the file arguments.By default it displays four-columnar output.First column shows number of lines present in a 6 min read ln command in Linux with Examples The 'ln' command in Linux is a powerful utility that allows you to create links between files. These links can either be hard links or soft (symbolic) links. If you're unfamiliar with these concepts, check out our detailed guide on Hard and Soft Links in Linux to understand their differences, use ca 3 min read slocate command in Linux with Examples slocate command in Linux is used to find the files in your system. This is a secure version of the Linux command locate which is also used for file searching, similar to locate command it creates a database of the file locations for faster searching. But it is much more secure than locate command be 3 min read dir command in Linux with examples The dir command in Linux is used to list the contents of a directory, providing an overview of the files and folders within it. How is the dir command different from ls?dir command differs from the ls command in the format of listing contents that is in default listing options. By default, dir comma 5 min read Like