When I run this command from php

sudo -u db2inst1 ls -t /var/lib/edumate/bdrs/*/*/daily/*NODE* | sort -r

I receive sudo: unable to execute /bin/ls: Argument list too long

ls lists all daily backup files we have for each customer. Customer has their directory under the bdrs directory. So I guess the number of files got bigger than allowed because it used to work correctly.

Is there any way how to get list of all daily backup files using one shell command?

  • 1
    find and -print0? – jasonwryan Jul 18 '12 at 1:27
  • Do you mean sudo -u db2inst1 find /var/lib/edumate/bdrs/*/*/daily/*NODE* -type f -name '*' -print0| sort -r? - this doesn't help. – Radek Jul 18 '12 at 1:36

It's rather easier with zsh. Its On glob qualifier changes the sorting order to in reverse lexicographic name order.

sudo -u db2inst1 zsh -c 'print -l /var/lib/edumate/bdrs/*/*/daily/*NODE*(On)'

There's no point in passing -t to ls, by the way, since you don't care about its sorting order.

| improve this answer | |
  • I do care about sorting :-) I need the files to be sorted desc by creation time. – Radek Jul 19 '12 at 1:02
  • @Radek Your sample command pipes into sort -r, which sorts by name (descending): the order of the ls output is irrelevant. You can't sort by file creation time because most unix systems do not store file creation times. You can easily sort by modification time in zsh: replace (On) by (om) (this is difficult to do without zsh). But for backup files, the name may be the right way to sort, if it encodes the date. – Gilles 'SO- stop being evil' Jul 19 '12 at 1:05
  • The filenames contain a timestamp. You're right ... I started with -t and then added sort -r and left -t in place doing nothing. Anyway, your code works and it is sorted as I need. Thank you. – Radek Jul 19 '12 at 1:07

Convert it to find so that the expanded file names won't be in the actual command (right now the shell is "globbing" which you don't want.

sudo -u db2inst1 find /var/lib/edumate/bdrs/ -ipath '/var/lib/edumate/bdrs/*/*/daily/*NODE*' | sort -r

| improve this answer | |
  • It's taking too long. It looks like it will never finishes. – Radek Jul 18 '12 at 1:53
  • It was running for an hour and still hasn't finished .... – Radek Jul 18 '12 at 2:47
  • 2
    that's the sort. it's not going to output anything until the entire find command is complete, and can pass it's full argument list to sort. cut out the | sort -r, and you may see more visible progress. – Tim Kennedy Jul 18 '12 at 4:00
  • How come it worked before much much faster? – Radek Jul 19 '12 at 1:09

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.