LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2013, 02:42 PM   #1
eRJe
Member
 
Registered: May 2005
Location: Netherlands
Distribution: Slackware 14.1 Kernel 3.12.1
Posts: 103

Rep: Reputation: 16
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.
 
Old 10-27-2013, 03:16 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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

Last edited by Firerat; 10-27-2013 at 03:21 PM. Reason: why ls is bad in scripts
 
1 members found this post helpful.
Old 10-28-2013, 04:58 PM   #3
eRJe
Member
 
Registered: May 2005
Location: Netherlands
Distribution: Slackware 14.1 Kernel 3.12.1
Posts: 103

Original Poster
Rep: Reputation: 16
@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...

Last edited by eRJe; 10-28-2013 at 05:00 PM.
 
Old 10-28-2013, 11:20 PM   #4
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
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
 
Old 11-13-2013, 06:52 PM   #5
eRJe
Member
 
Registered: May 2005
Location: Netherlands
Distribution: Slackware 14.1 Kernel 3.12.1
Posts: 103

Original Poster
Rep: Reputation: 16
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
directory timestamp qrange Linux - Newbie 4 09-08-2012 01:21 PM
[SOLVED] Retaining timestamp of a directory. sysmicuser Linux - Newbie 6 03-15-2012 11:13 PM
[SOLVED] Updating timestamp in files in directory tuxtutorials Programming 5 08-21-2010 09:03 AM
sort oldest 5 files in a directory tree recursively based on timestamp Linux abhelp Programming 1 06-04-2010 04:58 AM
script to sort files according to timestamp aeby Linux - Newbie 1 05-16-2007 08:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration