Touch Challenge


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Touch Challenge
# 8  
Old 08-05-2012
Quote:
Originally Posted by Cludgie
Does find, find files in any particular order?
No, just the order they happen to be in the filesystem, which may have nothing to do with folder or file names or even their time of creation.
# 9  
Old 08-05-2012
You'll need to forgive my lack of knowledge. Only been doing this for a few months.

My understanding of

Code:
touch -t $(date -d "2012-07-24 + $i day" "+%Y%m%d%H%M%S") filename

touch updates the date +1 for each loop. However, I can't figure out the logic to include a loop that updates the 48 daily files by 30 mins for each day as well.

Thanks
Cludgie

Last edited by Scott; 08-05-2012 at 06:33 PM.. Reason: Code tags
# 10  
Old 08-05-2012
Quote:
Originally Posted by Corona688
I've given you all the pieces. Have you tried anything with them?
you didn't give him the time-of-day, only the date...

Code:
#!/bin/bash
for i in {1..14}; do
        for j in {1..48}; do
                ((h = (j-1) / 2, m = (j-1) % 2 * 30))
                ts=$(date +'%Y%m%d%H%M' -d "2012-07-24 + $i day $h:$m")
                echo touch -t "$ts" "Day$i/File$j/BlockedNodes.csv"
        done
done

remove the echo if it looks correct.

Code:
touch -d 201207250000 Day1/File1/BlockedNodes.csv
touch -d 201207250030 Day1/File2/BlockedNodes.csv
...
touch -d 201207252330 Day1/File48/BlockedNodes.csv
touch -d 201207260000 Day2/File1/BlockedNodes.csv


Last edited by neutronscott; 08-05-2012 at 07:33 PM.. Reason: touch -t, not -d...
# 11  
Old 08-05-2012
Yes, you can include the option(s) to find that allow you to sort alphabetically, numerically, chronologically, etc. Example, you may be looking for files between 45 days and 90 days and you want them sorted chronologically;
Code:
find . -mtime +45 -mtime -90 -type f -exec ls -lart {} \;

(assuming you are already in the cwd and you do not have too many files that forces you to use xargs)

The 'ls -lart' will sort chronologically (date) in ascending order.

From the list that is generated you can execute the 'touch' command, or the 'cp' command, or the 'mv' command, or the 'tr' command, etc. Using the example from above;

Code:
find . -mtime +45 -mtime -90 -type f -exec ls -rt {} \; | while read line
do
<your commands>
done

Find type files that are between 45 and 90 days old (that would be mtime - when the file was last modified) and sort them by date in ascending order (if you leave off the 'la' from 'ls -lart' you are left with 'ls -rt' - you will not see the perms and dates but it is still sorting). The resulting files are then piped through a while loop and each line is read individually and that allows you to act on each line depending on some condition, i.e., the date. The <your commands> can be your touch command, acting on each line that is produced from the output of the find command.

As I mentioned above, if you have too many files you will need to use the xargs with your find command.

The find command by itself will do just that, find what you are looking for. Options added to the find command allow you to find files/directories based on some conditional, the "-mtime" or "-type" for example. Using the "-depth" option with find will allow you to find . files (hidden) as well.

The find command can be very simple or it can be very complex; you could have it sort, convert upper to lower, reverse the file names, etc. It's what you include with the find command that allows you to be choosy.

Almost always, there is more than 1 way to do things in UNIX/Linux. You may find another way to do what you want that we did not list here. And that is usually good, you are thinking instead of just copying what we said.

You may choose to use a perl 1 liner to identify and select the files you want.
Code:
find . -depth | perl -lne 'print if /1st date/ && /2nd date/ &&

and so on ...

Last edited by raggmopp; 08-05-2012 at 07:31 PM..
# 12  
Old 08-05-2012
with find, and bash regular expression matching:

Code:
#!/bin/bash

startday='2012-07-24'
re='/Day([0-9]+)/File([0-9]+)/'

find . -type f -name '*.csv' -print0 |
while read -rd '' file; do
        [[ $file =~ $re ]] || continue
        day=${BASH_REMATCH[1]}
        time=${BASH_REMATCH[2]}
        ((
                hr  = (time - 1) / 2,
                min = (time - 1) % 2 * 30
        ))
        ts=$(date -d "$startday + $day day $hr:$min" +%Y%m%d%H%M)
        echo touch -t "$ts" "$file"
done

# 13  
Old 08-05-2012
Thanks very much NeutronScott, that's brilliant. This seems to have the logic I'm looking for, only it's returning "touch: invalid date format `201207292000'." Yet, if I enter in this date for 1 file, it accepts it fine. Any thoughts?

Thanks again
Cludgie

---------- Post updated at 12:24 AM ---------- Previous update was at 12:10 AM ----------

Thank you also Raggmopp. I should refresh my page more often.

My only challenge to using the mtime, is that they were all files were modified in the same day, so would have lost their original creation and modified dates. Would that affect it?

Thanks
Cludgie
# 14  
Old 08-05-2012
Creation date - take that with a grain of salt. A file will have the creation date (if you want to call it that) if the file has never been modified. Example, create a file with something in it. Now, leave that file alone for some time. Somebody else will do 'ls -lart' and see a date/time stamp. To that somebody else that is the modified time. You created the file and you know when, so you know that is also the create date.

Something I did notice a couple of weeks ago. I am running Ubuntu 12.04 on my desktop which has a newer version of the 'stat' command. In the output of the stat command is a field called 'Birth Date:' Hmmm, could this be?

In my mind, the date/time stamp on a file is the modified time. This is the date/time stamp you will see associated with a file when you do 'ls -la'. It is this date/time stamp that is sorted when you run the 'ls -lart' command.

Here is something that may be useful with your task.

Show files which are modified after the specified file. The following find command displays all the files that are created/modified after ordinary_file.

Quote:
# ls -lrt
total 0
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
---------- 1 root root 0 2009-02-19 20:31 no_for_all
Note the 'ls -lrt' : the files (and dirs if there are any) are being sorted by date/time, Oldest to youngest.

Code:
# find -newer ordinary_file

Quote:
.
./everybody_read
./all_for_all
./no_for_all
Looks like you're using a combination of proposed solutions. Good job!

---------- Post updated at 06:20 PM ---------- Previous update was at 06:14 PM ----------

Creation date - take that with a grain of salt. A file will have the creation date (if you want to call it that) if the file has never been modified. Example, create a file with something in it. Now, leave that file alone for some time. Somebody else will do 'ls -lart' and see a date/time stamp. To that somebody else that is the modified time. You created the file and you know when, so you know that is also the create date.

Something I did notice a couple of weeks ago. I am running Ubuntu 12.04 on my desktop which has a newer version of the 'stat' command. In the output of the stat command is a field called 'Birth Date:' Hmmm, could this be?

In my mind, the date/time stamp on a file is the modified time. This is the date/time stamp you will see associated with a file when you do 'ls -la'. It is this date/time stamp that is sorted when you run the 'ls -lart' command.

Here is something that may be useful with your task.

Show files which are modified after the specified file. The following find command displays all the files that are created/modified after ordinary_file.

Quote:
# ls -lrt
total 0
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
---------- 1 root root 0 2009-02-19 20:31 no_for_all
Note the 'ls -lrt' : the files (and dirs if there are any) are being sorted by date/time, Oldest to youngest.

Code:
# find -newer ordinary_file

Quote:
.
./everybody_read
./all_for_all
./no_for_all
Looks like you're using a combination of proposed solutions. Good job!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Anyone like a challenge?

I have searched through google, and this forum to try and find the answer, but alas, nothing quite hits the whole answer. I am trying to read the last line (or lines) of some log files. I do this often. The files are named sequentially, using the date as part of the file name, and appending... (18 Replies)
Discussion started by: BatterBits
18 Replies

2. Shell Programming and Scripting

PS1 challenge

Ok then i Have a challenge for you : Give me PS1 so that it always display the least 2 levels of directory (except if i am above of course) I want it this way : so if i go to / /home/ /home/user /home/user/whatever /home/user/whatever1/whatever2 my PS1 should respectively... (12 Replies)
Discussion started by: ctsgnb
12 Replies

3. UNIX for Dummies Questions & Answers

Touch all files and subdirectories (recursive touch)

I have a folder with many subdirectories and i need to set the modified date to today for everything in it. Please help, thanks! I tried something i found online, find . -print0 | xargs -r0 touch but I got the error: xargs: illegal option -- r (5 Replies)
Discussion started by: glev2005
5 Replies

4. Shell Programming and Scripting

Need complex script, anyone up for a challenge?

Default shell is /usr/bin/zsh Script will be running #!/bin/bash Need to pull information from database while using other scripts already made (not by me). Ok, so i need a script pulling certain information about a customer's router interfaces. I am using a ROUTER-DNS-NAME as variable $1 I... (3 Replies)
Discussion started by: ///NNM
3 Replies

5. Shell Programming and Scripting

regex challenge

Here's a regex substitution operation that has stumped me with sed: How do you convert lines like this: first.key ?{x.y.z} second.key ?{xa.ys.zz.s} third.key ?{xa.k} to: first.key ?{x_y_z} second.key ?{xa_ys_zz_s} third.key ?{xa_k} So i'm basically converting all the... (11 Replies)
Discussion started by: neked
11 Replies

6. Shell Programming and Scripting

sed xml challenge

I have a web xml file that looks like this: <allinfo> <info> <a>Name1<\a> <b>address1<\b> <c>phone1<c> <\info> <info> <a>Name2<\a> <b>address2<\b> <c>phone2<c> <\info> <\allinfo> I want to use sed to... (2 Replies)
Discussion started by: katrvu
2 Replies

7. Shell Programming and Scripting

sed replacement, challenge one!!!!

Hi all, Thanks in advanced. This question really bothered me much. What i want is to replace any times of repeated 'TB' to 'T', below is example. It can be fullfil by AWK and perl, but my desire is using SED to realize it. So here means we treat TB as a whole part, which means 's/TB*/T/'... (4 Replies)
Discussion started by: summer_cherry
4 Replies

8. Shell Programming and Scripting

AWK Challenge

I have the following text Microsoft iSCSI Initiator version 2.0 Build 3497 Targets List: iqn.2001-05.com.equallogic:0-8a0906-daef43402-138000002a4477ba-grsrv12-extra iqn.2001-05.com.equallogic:0-8a0906-986f43402-520000002b447951-exchange ... (9 Replies)
Discussion started by: netmedic
9 Replies

9. Shell Programming and Scripting

camma challenge (in ksh)

Hi Gurus, I am a starter in ksh scripting I have a requirment where i need to ucertain that each line contain only 5 occurances of "," (comma) in a CSV file ; I don't want to use perl,is it possible in ksh;; Kindly help I am going to read CSV file and ; redirect all those lines which are... (3 Replies)
Discussion started by: Amresh Dubey
3 Replies

10. UNIX for Advanced & Expert Users

safeword challenge

Hi, there are some servers here at work which issue a Safeword challenge after I login. Can anyone tell me exactly how the challenge/response system works? In particular, how are the valid keys decided? (2 Replies)
Discussion started by: blowtorch
2 Replies
Login or Register to Ask a Question