To remove file using rm inside c


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To remove file using rm inside c
# 1  
Old 01-17-2011
To remove file using rm inside c

Unixians,

I need one help,I have to remove a file from particular path.

see my code snippet,
Code:
 {
 char cmd[100]="";
 sprintf (cmd, "/bin/rm -f %s%s%s%s","/usr1/mydir/", 1.t,2.t,3.t);
 system(cmd);
 }

The problem is it read as a "/usr1/mydir/1.t2.t3.t" and no files is removed.
Can u plz provide the solution.
Thanks in advance.

Last edited by pludi; 01-17-2011 at 01:28 PM..
# 2  
Old 01-17-2011
Is there a reason you are not using the unlink system call? Also, I am unfamiliar with the 1.t, etc constructs, can you explain what this does, or what you are expecting it to do?
# 3  
Old 01-17-2011
Can't you give *.t or are there many files with .t as extension?
# 4  
Old 01-17-2011
Tools

Friends,

The problem in removing is the string read like this /usr1/mydir/1.t2.t3.t.
My requirement is to remove file 1.t,2.t and 3.t .
The path is same only file name is different.

The files removed like this
rm -f /usr1/mydir/1.t
rm -f /usr1/mydir/2.t
rm -f /usr1/mydir/3.t

Please help on this.
# 5  
Old 01-17-2011
Code:
char cmd[100]="";
    printf("%d\n", chdir("/usr1/mydir/"));
    sprintf (cmd, "/bin/rm -f %s,%s,%s","/usr1/mydir/", "1.t","2.t","3.t");
    system(cmd);

# 6  
Old 01-18-2011
Its still having same issue.
I just print the string ,now its looks /bin/rm -f /usr1/mydir/1.t,2.t,3.t.
No removal is done.
sprintf (cmd, "/bin/rm -f %s,%s,%s","/usr1/mydir/", "1.t","2.t","3.t");
# 7  
Old 01-18-2011
The contents of cmd, after the [man]sprintf[/man] would be: /bin/rm -f /usr1/mydir/,1.t,2.t, which is not what is needed to remove a file. This does what you want:
Code:
{
    char path[100];
    char *file[3] = { "1.t", "2.t", "3.t"; }

    for (int i = 0; i < 3; i++) {
        strcpy(path, "/usr1/mydir/");
        strcat(path, file[i]);
        unlink(path);
    }
}

Although there is no error checking what-so-ever.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove bracket including text inside with sed

Hello, I could not remove brackets with text contents myfile: Please remove the bracket with text I wish to remove: I tried: sed 's/\//' myfile It gives: Please remove the bracket with text A1 I expect: Please remove the bracket with text Many thanks Boris (2 Replies)
Discussion started by: baris35
2 Replies

2. UNIX for Dummies Questions & Answers

Remove all the subdirectories except latest 5 inside any given directory

I Want to remove all the sub-directories except latest five in any given TGTDIR. Is there a way to do so without making a cd to TGTDIR? I have tried the following but not worked. Thank you. rm -rf `ls -t $TGTDIR | awk 'NR>5'` (20 Replies)
Discussion started by: Devendra Hupri
20 Replies

3. Shell Programming and Scripting

How to remove the values inside the html tags?

Hi, I have a txt file which contain this: <a href="linux">Linux</a> <a href="unix">Unix</a> <a href="oracle">Oracle</a> <a href="perl">Perl</a> I'm trying to extract the text in between these anchor tag and ignoring everything else using grep. I managed to ignore the tags but unable to... (6 Replies)
Discussion started by: KCApple
6 Replies

4. Shell Programming and Scripting

Remove everything inside of brackets

I need to use something bash related to remove everything inside of brackets. For example. In the following: abc<def>ghi<jkl>mno the result should be: abcghimno (4 Replies)
Discussion started by: locoroco
4 Replies

5. Shell Programming and Scripting

Remove whitespace after pipe symbol but not inside words

I have a file that looks like this: 102| #2 X 1/4-INCH| 30188| EA| FTW| A| NOT SERIAL TRACKING| NOT LOT TRACKING| TRUE| #2 X 1/4-INCH 102| #2 X 1/4-INCH| 30188| EA| VPS| A| NOT SERIAL TRACKING| NOT LOT TRACKING| TRUE| #2 X 1/4-INCH 102| #6 X 1/2"| ... (2 Replies)
Discussion started by: djehresmann
2 Replies

6. Shell Programming and Scripting

How to remove a temporary file inside gawk

Hi- How can I make the temporary file 0 byte , created inside gawk. I am using system("rm -f temp_orders"); It seems system command is deleting file permanently and I am not able to execute below statement. print ORD_HEAD_FULL >> cFILE; (cFile is temp_orders) (2 Replies)
Discussion started by: ashish_kaithi
2 Replies

7. Shell Programming and Scripting

How to remove string inside html tag <a>

Does anybody know how i can remove string from <a> tag? There are several hundred posts in a few forums that need to be cleaned up. The precise situation is ---------- <a href="http://mydomain.com/cgi-bin/anyboard.cgi?fvp=/family/sexuality_and_spirituality/&cmd=rA&cG=43"> ------------- my... (6 Replies)
Discussion started by: georgi58
6 Replies

8. Shell Programming and Scripting

Remove \n <newline> character inside the records.

Hi, In my file, I have '\n' characters inside a single record. Because of this, a single records appears in many lines and looks like multiple records. In the below file. File 1 ==== 1,nmae,lctn,da\n t 2,ghjik,o\n ut,de\n fk Expected output after the \n removed File 2 =====... (5 Replies)
Discussion started by: machomaddy
5 Replies

9. Shell Programming and Scripting

remove a whole directory tree WITH files inside?

Assume I want to remove a whole directory tree beginning with /foo/bar/ The directory or sub-directories may contain files. The top directory /foo/bar/ itself should not be deleted. rm -f- r /foo/bar does not work because it requires a directory tree without files. How does it work... (3 Replies)
Discussion started by: pstein
3 Replies

10. Linux

How to remove only html tags inside a file?

Hi All, I have following example file i want to remove all html tags only, Input File: <html> <head> <title>Software Solutions Inc., </title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor=white leftmargin="0" topmargin="0"... (2 Replies)
Discussion started by: btech_raju
2 Replies
Login or Register to Ask a Question