1

I am using Mobax and it has Busybox installed in it. I can successfully get the capacity, available, used and used percentage of the directory and its sub-directories in Linux by typing:

clear && date && pwd && du --max-depth=1 -k | sort -nr | cut -f2 | xargs -d '\n' du -sh

and I can get the oldest/earliest file in Linux which displays the date, time and the filename by typing:

find -type f -printf '%T+ %p\n' | sort | head -n 1

However, some arguments in the commands cannot be translated by Busybox directly. How do you translate or what is the translation of these commands that can be read by Busybox?

1

GNU du --max-depth=1 directly translates to BusyBox du -d 1. There's no equivalent of xargs -d; you can translate newlines to null bytes if xargs -0 is supported.

du -d 1 -k | sort -nr | cut -f2 | tr '\n' '\0' | xargs -0 du -sh

BusyBox has a lot of compilations to tune the compromise between size and features. If you don't have du -d, you can use du | grep -v '/.*/' instead. If you don't have du -h, tough it up and use kilobytes all over.

Without xargs -0, you can use a shell loop.

tab="$(printf \\t)"  # or a literal tab character
du -d 1 -k | sort -nr | while IFS= read -r line; do
  line="${line#*$tab}"
  du -sh -- "$line"
done

There's no good way to find the oldest file in a directory tree without advanced tools such as zsh or GNU find. Parsing the output of ls -l is one option, but it's painful.

One approach is to let ls do the sorting. This requires the timestamp feature to be included. If it is, getting the oldest file in the current directory is relatively easy:

ls -t | tail -n 1

When find is involved, this gets more complicated because ls may be invoked multiple times and the sorting only applies to each invocation. What you can do is collect all the oldest files per run of ls, and then run ls one more time to sort those. Hopefully you won't need more than two levels.

set -f; IFS='
'
set -- $(find -type f -exec sh -c 'ls -t | tail -n 1' {} +)
ls -t -- "$@" | tail -n 1

Alternatively, if your ls has the -e option to display timestamps in a uniform format and your sort has the -M option to sort by month name, you can sort its output:

LC_ALL=C find -type f -exec ls -lne {} + |
sort -k4,4n -k 1,1M -k 2,2n -k 3,3 |
head -n 1

Note that if multiple files have a timestamp within the same second, which one you get is a matter of chance. This is unavoidable if the filesystem's granularity doesn't go beyond seconds anyway.

Without sort -M and especially without ls -e, you need a lot more massaging. Furthermore, without ls -e, the resolution of timestamps is poor: one minute for files modified in the last 6 months, one day for older files. What you get is a random-ish file among those with the oldest timestamp, not necessarily the oldest file.

LC_ALL=C find -type f -exec ls -lne {} + |
sed -e 's/^[^ ]* [ 0-9]*//' |
sed -e 's/^Jan/01/' -e 's/^Feb/02/' -e 's/^Mar/03/' -e 's/^Apr/04/' -e 's/^May/05/' -e 's/^Jun/06/' -e 's/^Jul/07/' -e 's/^Aug/08/' -e 's/^Sep/09/' -e 's/^Oct/10/' -e 's/^Nov/11/' -e 's/^Dec/12/' |
sed -e 's/^\(.. \) /\10/' -e 's/^\(.. .. \) \([0-9][0-9]* \)/\2\1/' |
awk -v y="$(date +%Y)" -v m="$(date +%m)" '$3 ~ /:/ {sub(/^.. .. ..:../, ($1 <= m ? y : y+1) " " $1 " " $2)} 1' |
sort | cut -d ' ' -f 4- |
head -n 1

Note that in this whole answer, I assume that file names don't contain newlines or unprintable characters.

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.