Probably a simple one, but I can't figure it...
I want to do some conversions on a set of files in a directory, such as
for file in * do do_something_with $file done
However, I want to do the files in date order, not alphabetically, so I tried
for file in $(ls -tr) etc.
But this only works if the files don't have spaces in their names; "for" breaks them up into space-delimited tokens, which is no use at all. How do I do this (apparently) simple thing?
-- GT
On Sun, Dec 12, 2004 at 06:10:30PM +0000, Graham wrote:
Probably a simple one, but I can't figure it...
I want to do some conversions on a set of files in a directory, such as
for file in * do do_something_with $file done
However, I want to do the files in date order, not alphabetically, so I tried
for file in $(ls -tr) etc.
But this only works if the files don't have spaces in their names; "for" breaks them up into space-delimited tokens, which is no use at all. How do I do this (apparently) simple thing?
I'd solve it with find (and printf), sort, cut, and then xargs. I'd use find to printf the modified date as YYYYMMDDHHMMSS filename, then pipe that output through sort -g, then use xargs to run the command, use --replace and {} if your command can only work on one file at a time.
Hope that gives you a good pointer.
Thanks,
On 2004-12-12 22:32:06 +0000 Brett Parker iDunno@sommitrealweird.co.uk wrote:
On Sun, Dec 12, 2004 at 06:10:30PM +0000, Graham wrote:
[...] only works if the files don't have spaces in their names; "for" breaks them up into space-delimited tokens, which is no use at all. How do I do this (apparently) simple thing?
I'd solve it with find (and printf), sort, cut, and then xargs. [...]
Brett's solution is probably better/safer, but my first thought was that it's not "for" splitting them on spaces, but bash (or whatever your shell is). You could probably stop it by changing the value of the IFS environment variable from its default of space tab newline to just newline. Of course, this will break if a filename has a newline in it, but Brett's won't if you use \0 (the null character) as the delimiter.
On Sunday 12 December 2004 22:32, Brett Parker wrote:
On Sun, Dec 12, 2004 at 06:10:30PM +0000, Graham wrote:
Probably a simple one, but I can't figure it...
I want to do some conversions on a set of files in a directory, such as
for file in * do do_something_with $file done
However, I want to do the files in date order, not alphabetically, so I tried
for file in $(ls -tr) etc.
But this only works if the files don't have spaces in their names; "for" breaks them up into space-delimited tokens, which is no use at all. How do I do this (apparently) simple thing?
I'd solve it with find (and printf), sort, cut, and then xargs. I'd use find to printf the modified date as YYYYMMDDHHMMSS filename, then pipe that output through sort -g, then use xargs to run the command, use --replace and {} if your command can only work on one file at a time.
Hope that gives you a good pointer.
Thanks,
Thanks Brett (and others). It looked too complex for me, but it started me looking at pipes. What I ended up with is
for name in $(ls -tr | tr ' ' '/') do name2=$(echo $name | tr '/' ' ') do_something_with $name2 done
which converts the spaces into the slash character (that can't possibly occur in a filename), then converts back again for use.
-- GT
Hello
I want to do some conversions on a set of files in a directory, such as: for file in * ; do do_something_with $file ; done
However, I want to do the files in date order, not alphabetically, so I tried
for file in $(ls -tr) . . . etc.
But this only works if the files don't have spaces in their names; "for" breaks them up into space-delimited tokens, which is no use at all. How do I do this (apparently) simple thing? Unsubscribe? See message headers or the web site above!
I tried this:
ls -tr | while read file do do_something_with "$file" done
It seemed to work as long as the file variable is double quoted wherever it is used.
Alan
On Thursday 16 December 2004 14:18, Alan Williams wrote:
I tried this:
ls -tr | while read file do do_something_with "$file" done
It seemed to work as long as the file variable is double quoted wherever it is used.
Alan
Neat. I checked - it works. Ta very much. Always good value for money on this list.
-- GT