Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

In my media center directory all modified times of my folders are somehow not the desired date.

How can I change all modified times of the main folders to the modified time of the newest media-file inside each folder?

for example:

cd /tmp
mkdir test
cd test
mkdir d1
mkdir d2
touch d1/text1.txt
touch d1/movie1.mov
touch d2/text2.txt
touch d2/movie2.mov
touch notthis.file

now I tried:

ls -td *| head -n 1

but that gives out:

notthis.file
share|improve this question

marked as duplicate by Anthon, enzotib, Stéphane Chazelas, jasonwryan, Renan Aug 14 '13 at 13:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
seems like the other duplicate really explains it already –  rubo77 Aug 14 '13 at 15:10
    
It's closed as a duplicate, you don't need to do anything else –  Michael Mrozek Aug 14 '13 at 15:44
    
I added my final solution there in that duplicate as an answer: unix.stackexchange.com/a/86730/20661 –  rubo77 Sep 11 '13 at 22:55

3 Answers 3

With zsh:

cd that-dir
touch -r *(D.om[1]) .

If you want to consider files in subdirectories as well:

touch -r **/*(D.om[1]) .

And if you want to do it for every directory:

for d (**/*(D/)) touch -r $d/*(D.om[1]) -- $d
share|improve this answer
    
Can you add some explanation of the syntax? –  rubo77 Oct 11 at 17:05
    
@rubo77, see Rercusive globbing and Glob qualifiers in the zsh documentation. –  Stéphane Chazelas Oct 11 at 19:42

Using ls(1) to order a directory by modification time, head(1) to get the first of the files, and touch(1) to change the modification time of your target directory, it's pretty easy.

Usually it is not advisable to parse the output of ls since it is rarely necessary and easy to be caught up on special characters, however in this case I cannot think of another tool that will as easily give you the file with the latest timestamp in a directory.

touch_dir() {
    dir="$1"
    touch --reference="$(ls -td "${dir}"/* | head -n 1)" "${dir}"
}

Run that with an argument that is a directory, and it should set the modification time of the directory to the latest modification time of any file in the directory.

It will fail if there are no files in the directory (easy to test a number of ways) or if the latest filename has an embedded newline - head will only get the part before the new line.

To update many directories, just run it in a loop.

share|improve this answer
1  
Note that it will not consider dot files. –  Stéphane Chazelas Aug 14 '13 at 9:44
    
to be POSIX, better to use -r file instead of --reference=file –  enzotib Aug 14 '13 at 10:40
    
this sounds promising, but It seems not correct. I added an example to my question –  rubo77 Aug 14 '13 at 14:19
    
maybe with find *.mov -maxdepth 0 -type f -printf '%T+=%p\n' we can get the newest file and use it somehow –  rubo77 Aug 14 '13 at 14:59
    
You should never parse the output of ls. ;) –  Alexios Aug 15 '13 at 7:55

You can set a directory your_dir's timestamp to that of its most recently modified file with this monstrosity:

touch -t `ls -ltr --time-style='+%Y%m%d%H%M.%S your_dir' | grep '^-' | tail -1 | awk '{print $6}'` your_dir

It's unclear what you mean by 'main folders', so I haven't included any recursion.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.