0
$ ls -lU a.txt test.sh badtest
ls: cannot access 'badtest': No such file or directory
-rw-r--r-- 1 gqqnbig gqqnbig 11 Apr 15 17:17 a.txt
-rw-r--r-- 1 gqqnbig gqqnbig 11 Apr 16 17:22 test.sh

Although the order of the arguments is a.txt test.sh badtest, ls reports badtest first.

Is it possible for ls to honor the order of the files? ie. output like

$ ls -lU a.txt test.sh badtest
-rw-r--r-- 1 gqqnbig gqqnbig 11 Apr 15 17:17 a.txt
-rw-r--r-- 1 gqqnbig gqqnbig 11 Apr 16 17:22 test.sh
ls: cannot access 'badtest': No such file or directory
1

You can't in one ls invocation. Also note that the errors are output on a different stream, stderr where the buffering works differently than on stdout.

Also note that -U is non-standard (a GNU extension), and that if you want ls to list the files given as argument, you need the -d option, otherwise for files of type directory, ls would list their content instead.

Alternatively, you could run ls on each file:

for file in a.txt test.sh badtest
  ls -ld -- "$file"
done

That would address all those problems above.

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.