Posted by : Unknown Sabtu, 17 Oktober 2015

Morning! If any of you want to have a folder with specific file extension and you so lazy to filter it. Here's the answer. Using daemon and C programming it works for me in my ubuntu.
Here's are the code. The red one is daemon code, the blue one is the C code to move the file.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <dirent.h>
int main(void)
{
  pid_t pid, sid; 
pid = fork();
if (pid < 0){
exit(EXIT_FAILURE); }  
if (pid > 0){
exit(EXIT_SUCCESS); }
umask(0);
sid = setsid();  
if (sid < 0){
exit(EXIT_FAILURE); }
  if ((chdir("/")) < 0){
  printf("test");
  exit(EXIT_FAILURE); } 
close(STDIN_FILENO);
close(STDERR_FILENO); ////////////////////////////////////// 
while (1){
sleep(2);
DIR *dir;
char src[]="home/kevin/tes";
char dest[]="home/kevin/tester";
struct dirent *ent;
if ((dir = opendir ("home/kevin/tes")) != NULL)
{
while ((ent = readdir (dir)) != NULL) 
{  
int len_file;  
len_file=strlen(ent->d_name);  
if(len_file>2) {
char ext[3];ext[0]=ent->d_name[len_file-4];  
ext[1]=ent->d_name[len_file-3];
ext[2]=int->d_name[len_file-2];  
ext[3]=ent->d_name[len_file-1];
if(strcmp(ext,".txt")==0)   {   }  
else  {
char buf_src[100]; snprintf(buf_src,100,"%s/%s",src,ent->d_name);  
char buf_dest[100]; snprintf(buf_dest,100,"%s/%s",dest,ent->d_name); rename(buf_src,buf_dest);
  }   }   }  
closedir (dir); /* could not open directory */ perror (""); }   ///////////////////////////////////// }
exit(EXIT_SUCCESS);
}


How it works :

1. Compile it using gcc *namefile* -o *outputfile*


2. Run your *outputfile*


3. Move the file to folder "tes"


4. You see 2 files with c extension and txt extension in tes folder


5. Well about 1 second system will move the c into tester folder


6. Voila! appears in tester folder


7. Using command ps aux you can see ./movfile still working.


8. You can kill it using pkill *namefile*


Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Naga Lapar -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -