LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-01-2011, 09:38 AM   #1
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Rep: Reputation: 1
Hiding a file using unlink command


While googling for soft link and hard link i found this part
Code:
{
   FILE *fp;
 
   fp = fopen("some.hidden.file","w");
   unlink("some.hidden.file"); /* deletes the filename part */
 
   /* some.hidden.file no longer has a filename and is truely hidden */
   fprintf(fp,"This data won't be found\n"); /* access the data part */
   /*etc*/
   fclose(fp); /* finally release the data part */
}
It says " You can hide a file using above" , but no explanation. Can anybody tell me

1. is this a script ?
2. How can i execute this ?

Last edited by Soji Antony; 02-01-2011 at 09:40 AM.
 
Old 02-01-2011, 10:02 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
1. no, it's c,
2. why would you want to?? unlinking is just normal deleting as opposed to overwriting the disk sectors. I don't see why you are interested in this. this piece of code is of no use to anyone by itself, and does nothing interesting at all. It just deletes the file whilst it's still open, so you temporarily can read from a file that doesn't appear in the filesystem.

Last edited by acid_kewpie; 02-01-2011 at 10:03 AM.
 
1 members found this post helpful.
Old 02-01-2011, 10:06 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
Quote:
Originally Posted by Soji Antony View Post
While googling for soft link and hard link i found this part
Code:
{
   FILE *fp;
 
   fp = fopen("some.hidden.file","w");
   unlink("some.hidden.file"); /* deletes the filename part */
 
   /* some.hidden.file no longer has a filename and is truely hidden */
   fprintf(fp,"This data won't be found\n"); /* access the data part */
   /*etc*/
   fclose(fp); /* finally release the data part */
}
It says " You can hide a file using above" , but no explanation. Can anybody tell me

1. is this a script ?
2. How can i execute this ?
It's a little snippet of C source code. You can't use it as such, it would need to be integrated in a proper program.

In any case, it won't help you to do anything useful. If there's no other file name tied to that file you won't be able to access it using regular mechanisms, but only by reading the concrete disk position where the data is, which of course you don't know.

For more info on what the unlink() function does, you can read its man page:

Code:
$ man 2 unlink
 
1 members found this post helpful.
Old 02-01-2011, 10:11 AM   #4
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 584

Rep: Reputation: 59
Quote:
Originally Posted by Soji Antony View Post
It says " You can hide a file using above" , but no explanation. Can anybody tell me

1. is this a script ?
2. How can i execute this ?


[1] That is a C program.
[2] To execute a C program, you first need to compile it by following this (simple) step:

Code:
gcc sourceProgramName.c
or

Code:
gcc -o outPutFileNameOfProgram sourceProgramName.c
The -o option says that the output file which is generated by gcc is to be named as per your choice and not to use the default output file name: a.out

The output file should have x permission already set for it. If not, then do this:

Code:
chmod +x outPutFileNameOfProgram
or

Code:
chmod +x a.out
Now, you can execute the C program by simply typing:

Code:
./outPutFileNameOfProgram
or
Code:
./a.out
If your current directory is included in the PATH variable or you are placing your source program files in a bin directory which is included in the PATH variable then you can directly type the name of your output program name without the leading ./ as done earlier.

IMPORTANT:

You need to prefix a file / directory name with a dot (.) to make it hidden under a Unix / Linux OS.

You would not need to open a file in order to delete it in C. Check your C program and experiment with it, however.
 
1 members found this post helpful.
Old 02-01-2011, 10:15 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
Quote:
Originally Posted by devUnix View Post
[1] That is a C program.
No its not. It's an excerpt of c code, it can't be run by itself, so compiling it will be pointless / impossible.
 
1 members found this post helpful.
Old 02-01-2011, 10:22 AM   #6
Samotnik
Member
 
Registered: Jun 2006
Location: Belarus
Distribution: Debian GNU/Linux testing/unstable
Posts: 471

Rep: Reputation: 40
It's C code. How to execute this, read e.g. K&R, or any other book of C language.

By the way, how are you going to access this hidden data? This code simply deletes file, as rm does. So if any other program will eventually need this files' space... All your "hidden" data will be lost.
 
1 members found this post helpful.
Old 02-01-2011, 10:25 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
It could be compiled if you modify the beginning this way:

Code:
#include<stdio.h>
void foo(void) {
With "gcc -o foo.o -c foo.c", the resulting object file (foo.o) will be useless for you, nonetheless.
 
1 members found this post helpful.
Old 02-01-2011, 10:30 AM   #8
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Original Poster
Rep: Reputation: 1
Thank you all for posting the solution.
 
Old 02-03-2011, 01:04 PM   #9
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 584

Rep: Reputation: 59
A C Program to Rename File Names in Unix

Antony,


I thought I would write a complete C program for you. Here it is:

The source file name as I have used is:

Code:
-bash-2.05b# cat hideIt.c
Here are the contents of the above C program:

Code:
#include <stdio.h>
int main(int argc, char *argv[]){

        int count = 1;
        char cmd[255];

        if(argc < 2){
                exit(1);
        }
        else{
                for(;count < argc; count++){
                        sprintf(cmd,"mv %s .%s",argv[count],argv[count]);
                        printf("Executing:\t%s\n",cmd);
                        system(cmd);
                }
        }
        return 0;
}
Here is how we create three (or as many as you wish) empty files:

Code:
-bash-2.05b# touch a b c
Let's see them in the current directory listing:

Code:
-bash-2.05b# ls -ltr | tail -n 3
-rw-r--r--  1 root root    0 Feb  3 18:24 c
-rw-r--r--  1 root root    0 Feb  3 18:24 b
-rw-r--r--  1 root root    0 Feb  3 18:24 a
Let's execute our C program and pass it the three file names:

Code:
-bash-2.05b# hideIt a b c
Executing:      mv a .a
Executing:      mv b .b
Executing:      mv c .c
Once again, let's verify the file listing:

Code:
-bash-2.05b# ls -ald .*
drwxr-xr-x  2 root root 4096 Feb  3 18:24 .
drwxr-xr-x  8 root root 4096 Feb  3 18:21 ..
-rw-r--r--  1 root root    0 Feb  3 18:24 .a
-rw-r--r--  1 root root    0 Feb  3 18:24 .b
-rw-r--r--  1 root root    0 Feb  3 18:24 .c
Those files are not hidden because they have been renamed by prefixing them with a single dot (.) as I explained earlier in my post.

By the way, once again I show you how to compile the above program:

Code:
gcc -o hideIt hideIt.c
That's it!

Well, if you execute the program without passing any file name then the program will exit with an error code of "1":

Code:
-bash-2.05b# hideIt
-bash-2.05b# echo $?
1
So, you can guess why I have written this block:

Code:
        if(argc < 2){
                exit(1);
        }
You can put a user friendly message there just before issuing the exit() function.

I have not insterted any comments in the above program because I was attending a conference while writing the program some minutes ago. So, you can figure out what each statement means.

I would, however, like to tell you that sprintf() function is something which I have used to translate the entire command line into a single string because we cannot pass a C variable's value in the system() function along with the command name.

If you do this:

system("mv fileName .fileName");

then it will work. But then you have hard-coded the file name in the program itself making it a static stuff in this dynamic world!

So, let me know what you think of it and how I could help you more.

One more thing, we could use C functions to rename a file instead of calling the Unix command "mv". But I have shown you how we can make use of existing programs / commands.

Cheers!

Last edited by devUnix; 02-03-2011 at 01:07 PM.
 
1 members found this post helpful.
Old 02-05-2011, 01:36 AM   #10
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Original Poster
Rep: Reputation: 1
Hi devUnix

Thank you so much for your reply.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] zenity - hiding hidden files from file selections josephj Programming 5 10-15-2010 02:53 PM
Prevent editing/truncating file but not deletion (unlink) ruisselet Linux - Software 1 11-18-2009 11:40 AM
Hiding password in Java command line program simon_w Programming 2 05-02-2005 07:01 AM
Shell Programming: Hiding the o/p of the Kill command funkymunky Programming 5 06-10-2004 07:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration