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)?

11
  • 1
    Does xargs have an alias defined which includes the -r / --no-run-if-empty option? If so, that would result in no output. Commented Mar 14, 2016 at 16:52
  • 1
    @AnthonyGeoghegan - there is no alias for xargs Commented Mar 14, 2016 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)? Commented Mar 14, 2016 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. Commented Mar 14, 2016 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. Commented Mar 15, 2016 at 8:04

1 Answer 1

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.