ZMan007
ZMan007

Reputation: 21

Trying to get linux command to work in Ruby

I'm trying to run this Ruby Script I made on a Linux server... However, I'm having some issues understanding why it's failing. I thought this syntax would just be able to execute my command properly...

Any help would be greatly appreciated! Thank you in advance!

#!/opt/chef/embedded/bin/ruby
system("find . -size +20G -exec ls -l {} \;")

Script output - find: missing argument to `-exec' (edited)

#!/opt/chef/embedded/bin/ruby
system("find . -size +20G -exec ls -l {} \;")

Script output - find: missing argument to `-exec' (edited)

Upvotes: 0

Views: 73

Answers (1)

Dennis Williamson
Dennis Williamson

Reputation: 359875

Use single quotes:

system('find . -size +20G -exec ls -l {} \;')

or double the backslash:

system("find . -size +20G -exec ls -l {} \\;")

or single quote the escaped semicolon:

system("find . -size +20G -exec ls -l {} '\;'")

or separate the arguments (thanks to @tadman):

system('find', '.', '-size', '+20G', '-exec', 'ls', '-l' ,'{}', ';')

or do that but start with a string:

system(*"find . -size +20G -exec ls -l {} ;".split)

or here that is with a variable holding the string:

cmd = "find . -size +20G -exec ls -l {} ;"
system(*cmd.split)

These last two split the string into an array then the * turns the array into a list of arguments.

Ruby thinks you're escaping the semicolon and you need to prevent that by escaping the backslash because find needs the semicolon and since it's a shell special character it needs to be escaped. Hence, double escaping.

Or do it directly in Ruby:

require 'find'
Find.find(".") do |path|
  if FileTest.size(path) > 20 * 2 ** 9
    puts path
  end
end

You can add additional tests such as whether an entry is a directory and output additional information about the file.

Upvotes: 3

Related Questions