In order to use a pipe with touch, you'd need to move the pipe
contents over to the argument list. This is where xargs comes in.

ls | sort | xargs touch

Tip: you can omit the '-1' as that's the default when you're redirecting output.

Other options as suggested are:

touch `ls`
touch $(ls)

But none of this guarantees any order. touch is free to process
arguments however it sees fit.

You can however tell xargs to process one item at a time.

ls | sort | xargs -n1 touch

That will at least get you the items touched in order by name.
However, touch is fast so you might still end up with items touched
with the same date. And now you're getting in to bigger shell
scripting territory.

find . -name '*.mp[34]' -print | sort | while read F ; do touch "$F" ;
sleep 1 ; done

That'll touch one file every second. Certainly, if you have a lot of
files this could take a lot of time.

The ideal situation at this point is to provide a time that you want
the file touched with. You could even start with the current date and
iterate over the files in reverse, decrementing one second each file.

The following should do the trick.

# save the current date in seconds
CURRENT_DATE=$(date +%s)
# find any mp3/mp4 files, sort alphanumerically, reverse the sort,
process each line
# if you don't care about case, use: sort -fr
find . -name '*.mp[34]' | sort -r | while read CURRENT_FILE ;
    # decrement one second
    # convert the date to the format required by touch
   TOUCH_DATE=$(date -r $CURRENT_DATE +%Y%m%d%H%m.%S)
    # touch the file
    touch -t $TOUCH_DATE "$CURRENT_FILE"

On Sun, Oct 16, 2016 at 11:35 AM, David Patrick Henderson
<> wrote:
> Try  touch `ls -1`
> On 15 Oct 2016, at 19:33, Carl Hoefs <> wrote:
>> Yeah, that's what I thought at first, too. But touch begs to differ:
>> $ ls -1 | sort | touch
>> usage:
>> touch [-A [-][[hh]mm]SS] [-acfhm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file 
>> ...
>> -Carl
>>> On Oct 15, 2016, at 7:31 PM, Macs R We <> wrote:
>>> Right off the bat, I think you made a mistake in putting the * at the end 
>>> of the command. The whole idea of piping the output from LS is to input it 
>>> to touch, and you just provided touch with another argument first. I think 
>>> if you just let LS do its job and don't provide touch any arguments, you 
>>> will get what you want, including all of the necessary requoting.
>>>> On Oct 15, 2016, at 7:24 PM, Carl Hoefs <> 
>>>> wrote:
>>>> I'm trying to arrange the files in a directory to have modification dates 
>>>> according to alphabetical sort order. (Sounds weird, but Alpine car units 
>>>> use modification date as the ordering of music tracks on a USB stick.)
>>>> In bash I'm using:
>>>> ls -1 | sort | touch *
>>>> ...but it doesn't work, there is no change in the modification date of the 
>>>> files. I'm guessing the problem is that the filenames have spaces, so the 
>>>> 'touch *' is only touching the first word of the filename.
>>>> Is there a way to do this?
>>>> -Carl
>>>> _______________________________________________
>>>> MacOSX-talk mailing list
>> _______________________________________________
>> MacOSX-talk mailing list
> David P Henderson
> c: 757.286.3212
> --
> "Human beings are unable to be honest with themselves about themselves. They 
> cannot talk about themselves without embellishing."
>     -- Akira Kurosawa
> _______________________________________________
> MacOSX-talk mailing list

arno  s  hautala    /-|

pgp b2c9d448
MacOSX-talk mailing list

Reply via email to