how can I copy a file with full path and rename it in C program using execl? can I use this instruction:


2 Answers 2


The code you posted looks like it's intended to mimic the following shell command:

cat path_source >path_destination

But, in that scenario, > isn't a parameter to cat; instead, it's interpreted by your shell, which will run cat with a single parameter path_source, and its standard output connected to path_destination. (So your C code really does cat path_source '>' path_destination.)

One way to imitate what the cat command above does is to construct a string containing your shell command ("cat ... >..."), and then pass that, using exec, as an argument to /bin/sh -c, but it's hard to make it reliable and safe if you don't know for sure that the file names verify some constraints.

If you are going to simply rename a file, it might be best to use standard function rename from stdio.h instead. If not, please clarify your question.

  • the probleme is that i renamed my file in a gtk message box and return it into a string variable then concatenate it with the destination folder, i tried with cp command but it doesn't work, can I do that: execl("/bin/cat","command",NULL); and i put all the concatenated command i string variable "command"???
    – Faceopace
    Nov 30, 2015 at 18:11
  • Please edit your question to include the call to cp you tried, and the error messages you got, if any.
    – dhag
    Nov 30, 2015 at 18:14
  • i posted my problem with cp in here : unix.stackexchange.com/questions/245023/…
    – Faceopace
    Nov 30, 2015 at 18:16
  • I posted an answer your other question.
    – dhag
    Nov 30, 2015 at 18:38

This sounds like a homework assignment, so I'm being intentionally vague.

It sounds like the requirement is to mimic the behavior of the shell when using the output redirection operator. You'd need to manipulate the standard file descriptors before calling execl() such that standard output of the calling process was an open file descriptor for the target output file. That way when cat writes to standard output, the content will go to the target file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.