Here is the C program to implement the LS command Used in Linux
Even though the GUI in the Linux has come a long way over the past few years, the terminal always beats the GUI in terms of its raw power and usability. One of the most commonly used Linux commands is the ls command. The “ls” is a Linus shell command is which is used to print all the files and directories in the present directory in the form of a list.
# ls
0001.pcap Desktop Downloads index.html install.log.syslog Pictures Templates
anaconda-ks.cfg Documents fbcmd_update.php install.log Music Public Videos
This command can also be used in many ways by just tweaking the letters and symbols with it. Some examples are:
-> ls -l (-l is a character, not one) shows file or directory, modified date and time, size, file or folder name and also owner of file and it’s permission.
-> To see the list including hidden files, use ls -a.
-> Use -lh option to see sizes in human readable format.
Now that we know the usefulness of this command, its time for us to know what we came here for:
C program to implement the LS command
- #include
- #include
- #include
- #include
- int main(int argc,char **argv)
- {
- struct dirent **namelist;
- int n;
- if(argc < 1)
- {
- exit(EXIT_FAILURE);
- }
- else if (argc == 1)
- {
- n=scandir(“.”,&namelist,NULL,alphasort);
- }
- else
- {
- n = scandir(argv[1], &namelist, NULL,alphasort);
- }
- if(n < 0)
- {
- perror(“scandir”);
- exit(EXIT_FAILURE);
- }
- else
- {
- while (n–)
- {
- printf(“%s\n”,namelist[n]->d_name);
- free(namelist[n]);
- }
- free(namelist);
- }
- exit(EXIT_SUCCESS);
- }
We hope you learnt something new today.
No comments:
Post a Comment