use rm linux order in execl() function ic C language

327 views Asked by At

hope to be fine... i'm trying write code that use execl within execl in C language but it's not working when i put the directory of file. the code is:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
 int main(int argc,char* argv[]){
  int n,m,x;
  n=fork();
  if(n==0){
     execl("/bin/rm","rm /home/mazenas/Desktop/folder/","f.txt",NULL);
          }
  else{
     wait(&m);
     printf("end of Programm\n");
      }
  return 0;
  }

help me plaese!

2

There are 2 answers

0
MikeCAT On

The arguments of execl() is specifying the command-line arguments of the program to be executed.

execl("/bin/rm","rm /home/mazenas/Desktop/folder/","f.txt",NULL);

means to execute this:

"rm /home/mazenas/Desktop/folder/" "f.txt"

If you want to execute this command:

rm "/home/mazenas/Desktop/folder/f.txt"

You should write:

execl("/bin/rm", "rm", "/home/mazenas/Desktop/folder/f.txt", NULL);
0
Joshua On

I'm pretty sure you mean

 execl("/bin/rm","rm", "/home/mazenas/Desktop/folder/f.txt",NULL);

If rm checks its invocation name, then f.txt makes no sense and it complains; otherwise it's going to try to rm f.txt in the current directory rather than the given path.