3

I have two Ubuntu installations:

A: 14.04.3 LTS (GNU/Linux 3.13.0-77-generic x86_64)

B: 14.04.2 LTS (GNU/Linux 3.13.0-61-generic x86_64)

I'm running the following command:

find . -name "nosuch.file" -print0 | xargs -0 ls

(the real command is more complex, but I managed to narrow it down to this). There is no file nosuch.file in the current directory or subdirectories.

On server A the command produces empty output as I expected.

On server B the command produces the listing of the current directory (where I run the command).

On both servers running just find . -name "nosuch.file" -print0 produces empty output.

On both servers the same version of find, xargs and ls is installed:

$ find --version
find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0)

$ xargs --version
xargs (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b

$ ls --version
ls (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.

Why would the command behave differently? What can I do to achieve consistent behaviour as on server A (empty output for no found files)?

  • 1
    Does xargs have an alias defined which includes the -r / --no-run-if-empty option? If so, that would result in no output. – Anthony Geoghegan Mar 14 '16 at 16:52
  • 1
    @AnthonyGeoghegan - there is no alias for xargs – Adam Michalik Mar 14 '16 at 16:55
  • 2
    Sorry to ask the obvious question, but is the directory where you run the command on Server A empty (or contains only hidden files/directories)? – steeldriver Mar 14 '16 at 17:19
  • 1
    It might be useful to run edit the question to include the output of type -a xargs on both servers as there's a chance that a function or a script with that name that acts as a wrapper for the actual xargs command. – Anthony Geoghegan Mar 14 '16 at 21:38
  • 1
    @steeldriver @Gilles - thanks for hitting the nail of my stupidity on the head... I was so focused on the commands that I ignored the simple fact that on server B I was calling it in a directory with some files in it and on server A - in a newly created, empty one. Using xargs -r presents the desired behaviour. – Adam Michalik Mar 15 '16 at 8:04
2

“What can I do to achieve consistent behaviour as on server A (empty output for no found files)?“

To achieve this behaviour with xargs, you can use the -r / --no-run-if-empty option so that the specified command is not run if no filenames are passed to it. From the man page:

If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input.

Note that this option is a feature of GNU xargs and is not standard, i.e., specified by POSIX.

| improve this answer | |

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.