I've seen many questions and answers here using a construction along the lines of
list_dir=`ls -t /path/to/dir/`
for i in $list_dir; do
or
ls -t | while read i; do
Now, I know that you shouldn't use ls in scripts because it breaks easily; but I can't find a better way of operating on files in order from last-modified to most-recently-modified (or vice versa).
I can use something like:
find . -type f -printf '%T@ %p\n' | sort -n | cut -d ' ' -f 2- | while read i; do...
...but this will still break with any files that have newlines in their names, and is much uglier to boot. Is there a better way?
--zero-terminated
- thank you for that, I wish I could give you even more points! – evilsoup May 27 '13 at 16:38info ls
: if I wanted a bulletproof script with ls, which--quoting-style
should I use? – evilsoup May 27 '13 at 16:48--quoting-style=escape
is the way to choose. – Hauke Laging May 27 '13 at 16:57touch filename
. You can simply press Enter after " or ' (and close the quotes in one of the following lines). Or do this:touch a$'\n'b
(in bash) – Hauke Laging May 27 '13 at 17:10ls
: you can also use-Q
to double quote filenames that can have spaces. – DavAlPi Jun 20 '13 at 09:16ls
--quoting-style
fail (for parsing purposea) because it quote filenames only: when you have a space in a group name. Ok, its a very strage case, but it appen in cygwin bash shell, here you have group names likeDomain Users
. – DavAlPi Jun 20 '13 at 09:23