LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script to timestamp files with timestamp from directory (https://www.linuxquestions.org/questions/programming-9/script-to-timestamp-files-with-timestamp-from-directory-4175482386/)

eRJe 10-27-2013 02:42 PM

script to timestamp files with timestamp from directory
 
If I want to change the timestamp for files and use the timestamp from the directory that contains these files. For this I can use:

Code:

touch -r ./<directory-XYZ> ./<directory-XYZ/*>
Now I need to do this for a few hundred directories...

I'm trying to create a little script but I got stuck very quickly :-) I'm having trouble reading the directories because they contain spaces and special characters ie "[" and "(".

Something very basic like this doesn't work because the "\" in front of each special character is missing.

Code:

ls -d1 */ | while read DIRNAME
do
    ls ${DIRNAME}
done

I'm not a frequent script writer and probably because of that I always get stuck between the syntax of bash, csh, ksh etc. I'm always confusing the " ' ` Maybe I'm using the wrong syntax here or should I go with a different approach? I started with find . -type d -mindepth 1 -maxdepth 1 but it gave me the same troubles.

Firerat 10-27-2013 03:16 PM

unstested, but
Code:

ls "${DIRNAME}"
and you don't need ls, at all
or even while read

just at straight for loop will do it
Code:

for dir in */;do
    ls "${dir}"
done

the important part is the double quotes


edit:
http://mywiki.wooledge.org/ParsingLs

why ls is bad in scripts

eRJe 10-28-2013 04:58 PM

@Firerat,

Thanks for the hint. And thanks for the link you added. I will take a closer look at it later. For now the quick fix I needed was done by the following µ script. I had to use a slightly different approach as the directories were also containing subdirectories and the touch command didn't have a recursive option.


Quote:

for dir in */;do
find "${dir}" -exec touch -r "${dir}" "${dir}"/* {} \;
done

exit
I'm sure this could have been done in several ways, perhaps better then in this example. I'm open for comments so that I can learn...

SAbhi 10-28-2013 11:20 PM

Code:

find "${dir}" -exec touch -r "${dir}" "${dir}"/* {} \;
You don't have to use "find" when you are already inside the parent directory and have variable $dir containing the directory name you need, all you need to do is what you specified after -exec.

cheers

eRJe 11-13-2013 06:52 PM

Hi SAhbi,

Thanks for answering, sorry I took so long to respond.

I'm not sure if I understand. I used the find cmd to find any files in $dir (possibly in multiple subdirectories) and then execute the touch cmd on those files. I use $dir as date reference.

How do you suggest I should envoke the cmd string without find?

Regards,
Robbert


All times are GMT -5. The time now is 09:22 AM.