2

I'm trying to use a backslash on the command bellow at groovy syntax:

find /path/folder-* -type f -iname "file*" -exec rm -f {} \;

When I try to build this command on a Jenkins pipeline give me an error about this syntax. Even before I do this command have a warning with red syntax on Jenkins form field saying unexpected char: '\'.

What could I do for replace or fix error with this backslash ?

Groovy commands:

node ("instance") {
    stage ("cleaning folders"){
        sh '''        
        find /root/logfiles/instance* -type f -iname "file*" -exec rm -f {} \;
        '''
    }
    stage ("instance1"){
        sh '''
        rm -f /root/logfiles/instance1/*
        echo instance1; 
        scp 100.0.0.50:/var/log/file1.log /root/logfiles/instance1/file1.log;
        scp 100.0.0.50:/var/log/file2.log /root/logfiles/instance1/file2.log;
        '''
    }
    stage ("instance1"){
        sh '''
        rm -f /root/logfiles/instance2/*
        echo instance2; 
        scp 100.0.0.51:/var/log/file1.log /root/logfiles/instance2/file1.log;
        scp 100.0.0.51:/var/log/file2.log /root/logfiles/instance2/file2.log;
        '''
    }
}

Notice: I have rm -f for all instances at this moment. Will substitute all rm -f to the find command on the stage cleaning folders.

Tks in advance

5
  • this hybrid slash wont help you? ` ∖ `
    – gamer0
    May 26, 2018 at 13:43
  • 1
    Could you please post your Groovy script (or Pipeline, or whatever it is that you're using to run that command)?
    – jayhendren
    May 29, 2018 at 15:57
  • Pasted on question body the groovy script. @jayhendren
    – Marlon
    May 30, 2018 at 9:27
  • Since the slash character is simply escaping the following one, it may be that you just need to remove ir.
    – davidgo
    May 30, 2018 at 9:35
  • 1
    @davidgo in this particular case the backslash is definitely necessary to ensure the semicolon is parsed by find and not the shell.
    – jayhendren
    May 30, 2018 at 17:19

5 Answers 5

6

It might help to escape your escape character, as funny as this might sound. Just put another backslash in front of your backslash:

stage ("cleaning folders"){
    sh '''        
    find /root/logfiles/instance* -type f -iname "file*" -exec rm -f {} \\;
    '''
}

At least IntelliJ does not mark this as syntactically wrong.

1
  • It sound that worked fine. Tks!
    – Marlon
    Jun 7, 2018 at 3:57
2

Actually in your case I would not even bother to figure out proper escaping:

stage ("cleaning folders"){
    sh '''        
    find /root/logfiles/instance* -type f -iname "file*" -exec rm -f {} +
    '''
}

When you pass a semicolon to -exec, find constructs multiple commands, one for each result of the find operation (e.g. rm -f /root/logfiles/instance/file1.log, rm -f /root/logfiles/instance/file2.log, ...), but when you use a plus, find constructs a single command with multiple arguments, which is much more efficient and fast (e.g. rm -f /root/logfiles/instance/file1.log /root/logfiles/instance/file2.log ...). See the man page for find for more detail (sorry I can't quote the man page or provide more detail right now; I'm on mobile).

1
  • There is a limit to how long a command line can be. So be careful if you have a lot of files.
    – anon
    Aug 15, 2019 at 14:25
1

One solution would be to use dollar slashy which disables string interpolation and changes escape char to $.

stage ("cleaning folders"){
    sh script: $/
    find /root/logfiles/instance* -type f -iname "file*" -exec rm -f {} \;
    /$
}
1
  • A small addition: "dollar slashy" strings do indeed support string interpolation see section 4.8. I'm very new to Groovy and so this is still foreign, but this seems over complicated. However, it is what I needed for Jenkins and the sh command in my Pipeline. Nov 30, 2021 at 19:31
0

You can use something like following $/ yor backscalsh file path /$. For example:

document root = $/home\test\new/$
0

The best way to do this particular task with find is to use its -delete action, which also avoids the need to even use a backslash in this case: https://www.gnu.org/software/findutils/manual/html_mono/find.html#Using-the-_002ddelete-action

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .