Pages

Showing posts with label files. Show all posts
Showing posts with label files. Show all posts

Sunday, June 25, 2017

Displaying sorted list size wise of Files Folder on Linux

Displaying sorted list size wise of Files Folder on Linux


Even though i have a 120 GB HDD on my laptop and have dedicated a significant portion of hard disk space to Ubuntu still somehow every three or four months i run out of disk space . Also i am particularly bad at organizing stuff and well files are cluttered over my entire file system this makes tracking large files i dont need very difficult . Lots of application create a number of temporary files and these files also take significant amount of disk space and could be hard to track down if they are not created in /tmp .


The command I am discussing below helps you solve precisely this problem by giving you an ordered list (ordered in terms of size) of files and folders taking up space in your computer thus helping you find files/folder which are taking up significant amount of space :

du -sm *


Above command would display all the files and folder as well as their size as a list, however this is not a ordered list . To order the list we would need to pipe (that is send output of this command to another ) the output of the "du" command to "sort" command giving out list of files and folders sorted in ascending order according to the size.
du -sm * | sort -nr

Still there is a problem this is an entire list of files and folder in your file system , so if you have number of files and folders it will take number of screens to display . A better way would be to pipe the output of above command to "head" command to display only specified number of lines of output generated by "du -sm * | sort -nr " command .

du -sm *| sort -nr | head -15
Now the above command would display the top 15 files and folder according to their size .


Article Written by : Ambuj Varshney (blogambuj@gmail.com)
For Desktop on Linux Blog , http://linuxondesktop.blogspot.com
(C) 2008 , Ambuj Varshney
Read more »

Monday, May 15, 2017

Display All The Text Files From A Directory Using C

Display All The Text Files From A Directory Using C


DESCRIPTION:
  • This program get the directory path from the user and display all the text files available in the directory.
  • We should add dirent.h header file to accomplish this program.
  •  
PROGRAM:

#include <string.h>
#include <dirent.h>
#include <string.h>
const char *only_txt (const char *);

int main (int argc, char *argv[])
{
    DIR *directory;
    struct dirent *file;    
    directory = opendir (argv[1]);
    int directory_length = strlen(argv[1]);
    if (directory != NULL){
        while (file = readdir (directory)){
            if( !strcmp((only_txt (file->d_name)), ".txt") )
                      puts (file->d_name);
        }
        (void) closedir (directory);
      }
      else
        perror ("Not able to open the directory ");
    return 0;
}

/**load only .txt file**/

const char *only_txt (const char *filespec)
{
    char *file = strrchr (filespec, .);
        if (file == NULL)
            file = "";
    return file;
}

COMPILE:

    gcc -o out scan.c

RUN:
  • Pass the directory name with the path as argument to the program
        ./out /home/sujin/directory

OUTPUT:

      test.txt
   README.txt
      file.txt
 
CONCLUSION:
 
   Changing ".txt" to any type file extension, you can customize this program for file type you like.

 

Read more »

Tuesday, April 18, 2017

Display All The Files From A Directory Using C

Display All The Files From A Directory Using C


DESCRIPTION:
  • This program get the directory path from the user and display all the filed from the directory.
  • We should add dirent.h header file to accomplish this program.


PROGRAM:

#include <stdio.h>

#include <string.h>

#include <dirent.h>
int main (int argc, char *argv[])
{
    DIR *directory;
    struct dirent *file;    
    directory = opendir (argv[1]);
    int directory_length = strlen(argv[1]);
    if (directory != NULL){
        while (file = readdir (directory))
                  printf("FILE : %s ",file->d_name);

        (void) closedir (directory);
      }
      else
        perror ("Not able to open the directory ");
    return 0;
}



COMPILE

gcc -o out scan.c

RUN:
  • Pass the directory name with the path as argument to the program
./out /home/sujin/directory

OUTPUT:

FILE : favicon.ico
FILE : README.txt
FILE : monkey.jpg 
FILE : index.html



Read more »