-4

My effort is to use a piece of code in my program to find and copy certain files in my system.

The code I wrote meant to do that is:

void scan(int i)
{
pid_t parent = getpid();
pid_t pid = fork();

if (pid == -1)
{
    cout << "failed to fork" << endl;
    return;
}
else
{
    if(pid > 0)
    {
        int status;
        waitpid(pid, &status, 0);
    }
    else
    {
        if(i == 0)
        {
            execl("/usr/bin/find","find", "/", "-name", "'*.mp3'","-exec","cp","{}","/home/pi/Music","\\;",(char *)0);
            _exit(EXIT_FAILURE);
        }
    }
}

return; }

Nevertheless, the system returns this:

find: missing argument to `-exec'

How exactly can I correct my execl command to execute this shell command:

find / -name '*.mp3' -exec cp {} /home/pi/Music \;
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Note that you're not executing a "Bash command" here; `find` exists independently of Bash. – Oliver Charlesworth May 14 '17 at 21:18
  • 1
    `cout << "failed to fork" << endl` a string literal is not a valid operand for `<<` in C. – wildplasser May 14 '17 at 21:20
  • 1
    1) That's not C code 2) Your question is not related to C or C++. 3) Not related to programming. – too honest for this site May 14 '17 at 21:20
  • @FreudianSlip - The first param to `execl` is the executable path. The rest are the args; `argv[0]` is, by convention, the name of the executable. – Oliver Charlesworth May 14 '17 at 21:21
  • @Olaf - In what way is not related to programming? – Oliver Charlesworth May 14 '17 at 21:21
  • You don't need to quote the string `'*.mp3'` with the `'` – Andrew Henle May 14 '17 at 21:23
  • @OliverCharlesworth: It is about the Unix `find` command, which is not related to programming in the context of this site. – too honest for this site May 14 '17 at 21:23
  • @AndrewHenle - If you omit one of the \ then it won't compile... – Oliver Charlesworth May 14 '17 at 21:24
  • 2
    @Olaf - if I use only the find command written above, it works fine. Only problem is to convert that to execl format. So I would personally say this is related to pragramming. – Zbyněk Jakš May 14 '17 at 21:25
  • @OliverCharlesworth True. I forgot that. I was having way too much trouble trying to get one backslash in code ticks to show up properly. – Andrew Henle May 14 '17 at 21:26
  • I'll delete my wrong suggestion above so as not to confuse - sorry for the bum steer. Found this - it may help : http://stackoverflow.com/questions/12596839/how-to-call-execl-in-c-with-the-proper-arguments – FreudianSlip May 14 '17 at 21:27
  • Consider using the new [fileystem](http://en.cppreference.com/w/cpp/header/experimental/filesystem) library if your compiler is modern enough. – rwols May 14 '17 at 21:34
  • @ZbyněkJakš: There is nothing about "execl format". You seems to have understood arguments are given one per parameter. So it **is** a problem with the syntax of `find` arguments you need to understand. "if I use only the find command written above" does not say anything else than you just used a pattern you don't really understand. – too honest for this site May 14 '17 at 21:40
  • @Olaf true. Since this is my very first question here. Would you please recommend what should've be done differently when asking this question for future purposes? Thank you :) – Zbyněk Jakš May 14 '17 at 21:47
  • 1
    How about RTF(ine)M? – too honest for this site May 14 '17 at 21:49
  • 1
    @Olaf Don't get borderline (rude), I've been badly experiencing that ya know ;-) – πάντα ῥεῖ May 14 '17 at 22:19
  • @πάνταῥεῖ: OP asked for a tip what to do to ask better questions. Doing research before asking is the best way. The first address of research would be the manuals/documentation. So what's rude about this? I intentionally did not just write "RTFM" which some people consider being rude. If in doubt, assume the worst is a really bad attitude (I don't mean you). – too honest for this site May 14 '17 at 22:33

2 Answers2

2

TL;DR - Remove the \\ before the ;.

In the context of a shell, a command is interpreted before the individual arguments being passed to the find executable. ; is a special character, so needs escaping in order to be interpreted as a literal (as it's a special character) - find actually only needs ;.

However, you're not passing the arguments on the shell - execl passes the arguments directly to the target executable. So there's no need to escape it.


As pointed out by @AndrewHenle in comments below, you'll also need to remove both the ' from '*.mp3' for similar reasons.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

There is no Bash in sight, despite your question title. Yet you are using Bash-style quoting.

"'*.mp3'"

Your find will find nothing unless you have files with single quotation marks in their names. Use "*.mp3".

"\\;"

find requires a single semicolon to terminate the -exec arguments. \; is the Bash way to use a literal semicolon. Normally ; is a reserved character in Bash. Use a simple ";".

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243