Cd to Folder based on TimeStamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cd to Folder based on TimeStamp
# 1  
Old 01-23-2014
Cd to Folder based on TimeStamp

Hi,
Code:
ls -ltr 
-rw-rw----   1 user1   admins   5000032 Jan 20 17:11 M1120252_P004640.csv

Now i wish to cd to that folder amongst the below folders that would have log files for the date of the .csv file i.e. 20 Jan 17:11

Code:
ls -ltr
total 53616
drwxrwx---   2  user1   admins   20840448 Jan 19 02:10 archive_51
drwxrwx---   2  user1   admins   6553600 Jan 22 02:23 archive_52
drwxrwx---   2  user1   admins   8853600 Jan 24 02:23 archive_53

Desired folder i need to cd in this case would be "archive_52"

The above is just a specific query. I need this mechanism to work for all csv files i.e with different date / times

OS: SunOS mymac15.10 Generic_148888-04 sun4v sparc SUNW,SPARC-Enterprise-T5220

Can you please help how to get it to work ?

Last edited by mohtashims; 01-23-2014 at 11:02 PM..
# 2  
Old 01-23-2014
This makes no sense. The timestamp on a directory will change when the contents of the directory changes (such as when a file is created inside that directory or when a file is removed from the directory); not when the contents of a file in the directory changes.

Assuming that you have determined the name of the file that you want, saved that file's name in a variable named filename, and that that file name only appears in one directory, you could try something like:

Code:
pathname=$(ls */$filename)
cd ${pathname%/*}

# 3  
Old 01-24-2014
Quote:
Originally Posted by Don Cragun
This makes no sense. The timestamp on a directory will change when the contents of the directory changes (such as when a file is created inside that directory or when a file is removed from the directory); not when the contents of a file in the directory changes.

Assuming that you have determined the name of the file that you want, saved that file's name in a variable named filename, and that that file name only appears in one directory, you could try something like:

Code:
pathname=$(ls */$filename)
cd ${pathname%/*}

There is a file called check.log in each of the three folders.

Using the timestamp of each of the three folders we can determine & fire a grep command only on that folder's check.log [archive_52/check.log]. because the time stamp of folders archive_51, archive_53 are pre and post the time of the M1120252_P004640.csv file i.e. Jan 20 17:11

So please help me cd to the folder [ archive_52 ] automatically based on the timestamp of the given .csv file.

I need your help automate this.
# 4  
Old 01-24-2014
Quote:
Originally Posted by mohtashims
There is a file called check.log in each of the three folders.

Using the timestamp of each of the three folders we can determine & fire a grep command only on that folder's check.log [archive_52/check.log]. because the time stamp of folders archive_51, archive_53 are pre and post the time of the M1120252_P004640.csv file i.e. Jan 20 17:11

So please help me cd to the folder [ archive_52 ] automatically based on the timestamp of the given .csv file.

I need your help automate this.
Given the ls output you posted earlier:
Code:
ls -ltr
total 53616
drwxrwx---   2  user1   admins   20840448 Jan 19 02:10 archive_51
drwxrwx---   2  user1   admins   6553600 Jan 22 02:23 archive_52
drwxrwx---   2  user1   admins   8853600 Jan 24 02:23 archive_53

we have absolutely no reason to believe that archive_52 is the correct directory. If you're basing your decision of which directory to choose based on the timestamps of three directories that might contain the file, all we know is that two of these directories have had files added to or removed from them after M1120252_P004640.csv was last modified at 17:11 on January 20. We don't know that M1120252_P004640.csv wasn't created on January 19 in archive_51 and modified on the 20th. We know that some file was added to or deleted from archive_52 and archive_53 after M1120252_P004640.csv was last updated, but that gives us no indication that M1120252_P004640.csv exists in either of these directories.

Furthermore, if you're saying that M1120252_P004640.csv doesn't exist in any of these directories, but only that check.log was updated (not created) when M1120252_P004640.csv was created, you have absolutely no indication from the timestamp on the directory, which check.log will contain an entry indicating when M1120252_P004640.csv was created (or last updated) based on the timestamp of the directory containing the log file. The timestamp of a directory says nothing about when a file (check.log) was last updated. If you add an entry to the end of check.log in any of these three directories, the time stamp on the containing directory will not change.
# 5  
Old 01-24-2014
Thank you !!

Last edited by mohtashims; 01-24-2014 at 10:08 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

2. Shell Programming and Scripting

Extract based on timestamp

Hi, I want take the files whose timestamp greater than "2015-12-23 22:30" using awk .Please advise os version :Linux ip-172-31-19-240 4.1.10-17.31.amzn1.x86_64 #1 SMP Sat Oct 24 01:31:37 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 2015-12-23 20:43:17 312116 file1 2015-12-23 21:44:46 314375... (11 Replies)
Discussion started by: mohan705
11 Replies

3. UNIX for Dummies Questions & Answers

Script to keep todays files based on Timestamp

Hi i need to keep todays files based on timestamp and archive the remaining files ex: Managerial_Country_PRD_20150907.csv Managerial_Country_PRD_20150906.csv Managerial_Country_PRD_20150905.csv (2 Replies)
Discussion started by: ram1228
2 Replies

4. UNIX for Dummies Questions & Answers

Move to New Timestamp Folder

Hi All, I have a cron job that has worked wonders: zip /home/myHostedFolder/public_html/images/_backups/front/$(date +\%Y\%m\%d).zip /home/myHostedFolder/public_html/images/front/* && rm /home/myHostedFolder/public_html/images/front/* But now work, needs me to modify it. Should be easy but... (5 Replies)
Discussion started by: MrDude
5 Replies

5. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

6. Shell Programming and Scripting

Finding the maximum timestamp in a folder

I've the files in a directory in the following format having date +%Y%m%d%H YR_MNTH_2013061205 YR_MNTH_2013060107 and i need the latest file i.e; YR_MNTH_2013061205 to be moved to another folder #!/bin/ksh # Ksh 88 Version for test_time in YR* do --- done How can i achieve that !... (2 Replies)
Discussion started by: smile689
2 Replies

7. UNIX for Dummies Questions & Answers

Order based on timestamp in a single file

Hi All, I have a large text file which is a combination of multiple files. This is what I used and it worked. for i in /home/docs/text/* do cat $i >> Single_File done Now wondering, if there is a way to sort that single large file based on timestamps in ascending order. Text file... (11 Replies)
Discussion started by: prrampalli
11 Replies

8. Shell Programming and Scripting

copy files based on creation timestamp

Dear friends.. I have the below listing of files under a directory in unix -rw-r--r-- 1 abc abc 263349631 Jun 1 11:18 CDLD_20110603032055.xml -rw-r--r-- 1 abc abc 267918241 Jun 1 11:21 CDLD_20110603032104.xml -rw-r--r-- 1 abc abc 257672513 Jun 3 10:41... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

9. UNIX for Dummies Questions & Answers

timestamp in the end of a folder

hello everybody, im new here... can u tell me how can i add a timestamp in a bottom of a file which will say for example..it was written on that date and time. is tha possible?? thanxxxxxx (7 Replies)
Discussion started by: psychobeauty
7 Replies

10. UNIX for Dummies Questions & Answers

Using find command for timestamp based search

Hi, I am writing a script where i need to list all the files that have a scecific timestamp. I get this time stamp from another file. Is it possible to do this with 'find' command? Please let me know your valuable inputs. Thanks Sunny. (1 Reply)
Discussion started by: sunny_03
1 Replies
Login or Register to Ask a Question