[SOLVED] Bash Script to Copy Modification Date from a file to his folder
Hi There,
I need this script but I don't know how to do it :( I have one folder with several folders inside. On each folder a have one MKV or AVI file inside... What I need is a script to change the "modification date" of each folder to the "modification date" of each MKV or AVI that the folder has inside. Cheers PJGM PS: My system: NAS with Linux 2.6.24.4 |
Code:
while read file |
You can use the "touch" command to change dates including modifications dates (-m option to change only modification date).
Type "man touch" for more detail on the touch command. Generally doing "touch -m CCYYMMDD <filename>" would do what you want for the filename (including a directory filename). e.g. touch -m 20110630 testdir Would put the date Jun 30 2011 on the directory named testir. touch -m 20060512 test.avi Would put the date May 12 2006 on the file test.avi. You can use the "ls -l -time-style +%Y%m%d <filename>" to list a file with its date in the same format as above. (Type "man ls" for more detail on the ls command. You'd want to do a for loop to do what you're asking (or what I think you're asking). Something like: Code:
cd <mainfolder> The awk command above just gets the 6th position from the ls output and that is the position that has the date of the file. (You can type "man awk" for more detail - awk is a full text/programming utility so it is often best to find a good tutorial for it for more advanced usage - there are many on the internet.) Prior to trying this in your real directory you might want to make a test directory as your main folder then copy a couple of the subdirectories and their contents to it to be sure the loop works the way you want. |
Well, here's another one: ;)
Code:
#!/bin/bash |
Quote:
|
Quote:
|
wooow Thank you guys !!!
I'm trying but when I run this: Quote:
Quote:
pjgm |
It should be --time-style (double hyphen). In any case there are valid reasons why using ls in a for loop is not a good idea.
|
sorry both doesn't work for me...
just to be more clear... Giving the same modification date of Movies.mkv to their parent directory -Movies folder # script should work on every folders on this directory ---- Movie name A folder # This folder should get... -------- Movie name A.mkv # ...this modification date -------- movie name A.nfo ---- Movie name B folder # This folder should get... -------- Movie name B.mkv # ...this modification date -------- movie name B.nfo ---- Movie name C folder # This folder should get... -------- Movie name C.mkv # ...this modification date -------- movie name C.nfo and just a small note: 'movie A.mkv' can have a different name than the 'Movie A folder' |
pjgm, my apologies for misreading your question and posting the opposite solution. I believe this script does what you require:
Code:
#!/bin/bash |
I've tried my code on your directory structure and it works now, with a minor modification to manage the blank spaces in the directory names:
Code:
while read file |
I thank you all
A friend of mine gave me this... credits to him... Vpeter Quote:
Pjgm |
This should do the trick, if you use Bash:
Code:
find ./ -depth -mindepth 1 -type f '(' -name '*.avi' -o -name '*.mkv' ')' -print0 \ The second line uses touch to set the modification time on the parent directory to match each file found. If you do not use Bash, or you have a non-GNU find, or old find, use Code:
/bin/sh -c 'find ./ -type f | while read FILE ; do |
All times are GMT -5. The time now is 03:55 AM. |