-1

I'm trying to create an c file that lets you hide all files in a linux directory. I know I should use int rename (const char *oldname, const char *newname) and just add a "." in front of all the file names, but I am unsure how to make it reiterate through every single file in the directory.

Ideally I would want $ ./hidefiles ~/documents/folder , to make all contents in the folder invisible.

Any guidance?

  • Invisible to who/what? Files that start with "." are not really invisible. They can be seen on the command line for example with `ls -a`. And any code that iterates through the files in that directory will see it too. – kaylum Dec 05 '15 at 21:33
  • The only way to "hide" a file in Linux is to rename the file ".myfile". And the only reason this "works" is because "ls" (and similar tools) abide the convention "files that start with a leading dot should be 'hidden'". So yeah, you could write a C file or script that 1) looks up each file in the directory, then 2) renames the file ".file". But I can't imagine this is something you'd really want to do... – paulsm4 Dec 05 '15 at 21:34
  • Possible duplicate of [Listing files in Directory in Ubuntu](http://stackoverflow.com/questions/12989228/listing-files-in-directory-in-ubuntu) – Ken White Dec 05 '15 at 21:35
  • @kaylum invisible in the sense that if you try to find the files, they wouldn't be publically shown. Like if you went through the downloads folder and it just looks empty. Is this possible? – stealingbikes Dec 05 '15 at 21:49
  • @paulsm4 I'm trying to think of fun uses of directory related applications, something like ls, cp, or mv. Unfortunately I'm not very good at coding, but let me know if you have any suggestions for me to try – stealingbikes Dec 05 '15 at 21:49
  • @stealingbikes Who/what is "you". All programs? Just `ls`? A particular application? `ls` by default doesn't show files starting with `.`. But that's only what `ls` does. No other program has to follow that behaviour at all. So are you looking for superficial "hiding"? Or are you really asking to enforce that at the OS level? If the latter then please clarify what OS. – kaylum Dec 05 '15 at 21:53
  • @kaylum Sorry for not being more specific. I am looking for superficial hiding, like you could still find the files through command line but they would not display in the folder, if that makes sense. – stealingbikes Dec 05 '15 at 21:58
  • Then have a read of [How can I get the list of files in a directory using C or C++?](http://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c). That shows you how to iterate over the files in a directory. Then you just need to call [`rename`](http://linux.die.net/man/2/rename) for each file. – kaylum Dec 05 '15 at 22:01

1 Answers1

1

This will superficially hide your files. (Takes directory path as argument)

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
   struct dirent *dirpent;
   DIR *dirp;
   char temp[1024] = {'\0'};

   if(argc!=2)
   {
      printf("Run program again and specify path as argument\n");
      return 0;
   }

   /// Open specified directory
   dirp = opendir(argv[1]);

   /// Change current working directory to specified directory 
   chdir(argv[1]);
   if(dirp)
   {
       /// Keep retrieving the next directory entry
       while( (dirpent=readdir(dirp)) !=NULL)
       {
           /// Checks for regular files and makes sure they aren't already hidden
           if(dirpent->d_type == DT_REG && dirpent->d_name[0] != '.')
           {
               sprintf(temp, ".%s", dirpent->d_name);
               rename(&temp[1], temp);
               temp[0] = '\0';
           }
       }
   /// Close directory
   closedir(dirp);
   }
   return 0;
}