sorting files with find command before sending to text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sorting files with find command before sending to text file
# 1  
Old 07-19-2008
sorting files with find command before sending to text file

i need help with my script....
i am suppose to grab files within a certain date range
now i have done that already using the touch and find command (found them in other threads)

Code:
touch -d "$date_start" ./tmp1    
touch -d "$date_end" ./tmp2

find "$data_location" -maxdepth 1 -newer ./tmp1 ! -newer ./tmp2 -type f -exec basename {} \;>tmpfile

but i want the files that go to tmpfile to be sorted according to the date created and not by file name...
p.s. i tried changing

-exec basename to ls -ltr and it still didn't sort it

please help!!!!!!!!!!!!

thank you in advance....Smilie
# 2  
Old 07-20-2008
This should gives the files sorted by the modified time:

Code:
touch -d "$date_start" ./tmp1    
touch -d "$date_end" ./tmp2

find "$data_location" -maxdepth 1 -newer ./tmp1 ! -newer ./tmp2 -type f \
| xargs -i ls -l {} | sort -k 6 | awk 'NF > 5 {print $8}' > tmpfile

We sort the long list of ls by date (column 6) and use awk to print the filenames (column 8) and skip the unnecessary lines.

Regards
# 3  
Old 07-20-2008
Thank you so much!!!!! this is a useful forum
# 4  
Old 07-20-2008
i still have a small problem .... the 6th column is the year (i.e. 2007) so the files are still scattered... i want the file ordered like

(i.e. these are the names of the data files)

Tue_Nov_06_2007
Thu_Nov_22_2007
Wed_Jan_09_2008
Sat_Feb_23_2008

etc

instead i get this

Sun_Mar_16_2008
Thu_Nov_29_2007
Sun_Jan_13_2008
Mon_Dec_03_2007
Fri_Jan_04_2008
Mon_Mar_10_2008

because the time stamp goes

2007
01:56
06:21
10:20
11:12
16:07
or something like that

THANK YOU so much again for all your time and help

p.s. can i sort the month and day using the sort command as well

Last edited by deking; 07-20-2008 at 05:54 PM..
# 5  
Old 07-20-2008
The solution I gave was based on a newer version of ls which always shows the date as "2008-07-20".
To get the time you can use a perl solution I founded here: https://www.unix.com/unix-advanced-ex...ed-format.html

Now the command should looks like:

Code:
find "$data_location" -maxdepth 1 -newer ./tmp1 ! -newer ./tmp2 -type f > | \
xargs -i echo `perl -e '@d=localtime ((stat(shift))[9]); printf "%02d-%02d-%04d %02d:%02d:%02d\n", $d[3],$d[4]+1,$d[5]+1900,$d[2],$d[1],$d[0]' {}` {} | sort | awk '{print $3}' > tmpfile

Regards
# 6  
Old 07-20-2008
thank you for all your time franklin52
# 7  
Old 07-20-2008
you will not believe this simple line worked perfectly

Code:
touch -d "$date_start" ./tmp1    
touch -d "$date_end" ./tmp2

ls -lrt | awk '$9 ~ /tmp1/ , $9 ~ /tmp2/' | awk '{print $9}'>tmpfile

but i really appreciate all your time and help...if not anything, i learned more about perl scripting Smilie...so thank you so much


p.s. here is the link form where i found the solution

https://www.unix.com/shell-programmin...ate-range.html

Last edited by deking; 07-20-2008 at 08:06 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting indented text files

Hello, I'm trying to find a solution or a proper tool for the following job: I need to sort a text document with indented sections, so all levels of indentation are sorted independently for each section. Particularly, I need this for Cisco routers' running config files to compare them with... (2 Replies)
Discussion started by: kobel
2 Replies

2. Linux

Search only text files with 'find' command?

I've been using this to search an entire directory recursively for a specific phrase in my code (html, css, php, javascript, etc.): find dir_name -type f -exec grep -l "phrase" {} \; The problem is that it searches ALL files in the directory 'dir_name', even binary ones such as large JPEG... (2 Replies)
Discussion started by: Collider
2 Replies

3. Solaris

Monitoring log file for entries - Find command & sorting

hi, I would like to monitor a log file, which rolls over, everytime a server is restarted. I would like to grep for a string, and to be more efficient i'd like to grep only newly appended data. so something like a 'tail -f' would do, however, as the log rolls over i think a 'tail -F' is... (2 Replies)
Discussion started by: horhif
2 Replies

4. Shell Programming and Scripting

Complex data sorting in excel files or text files

Dear all, I have a complex data file shown below,,,,, A_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 C_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 A_ABCDEF715 52410.9 18598.2 10611 10754.7 122535 252426 36631.4 C_DBCDI_1353 0... (19 Replies)
Discussion started by: AAWT
19 Replies

5. UNIX for Dummies Questions & Answers

How to pass file text into find command

Hi all, I have a large text file and also a smaller list of program names. I want to find out how many of those programs exist in the large text file. Can someone help me with the command/script please. The program list is along the lines of tranwe2 tranwe3 tranye5 etc so basically I... (5 Replies)
Discussion started by: Grueben
5 Replies

6. Shell Programming and Scripting

How to redirect a input of find command into a text file

Hello friends, I want a command to print the reult files from find command into a text file.:) Iam looking from forum memebers. PLZ help me.ASAP Thanks in Advance, Siva Ranganath CH (5 Replies)
Discussion started by: sivaranga001
5 Replies

7. Shell Programming and Scripting

faster command than find for sorting?

I'm sorting files from a source directory by size into 4 categories then copying them into 4 corresponding folders, just wondering if there's a faster/better/more_elegant way to do this: find /home/user/sourcefiles -type f -size -400000k -exec /bin/cp -uv {} /home/user/medfiles/ \; find... (0 Replies)
Discussion started by: unclecameron
0 Replies

8. Shell Programming and Scripting

Attaching file and sending text in body using sendmail command!!

Hello, I need to send with text in the body and 2 files as attachments using sendmail command. i can send only one at a time either attachment or text in body of email. Can any one please help me how to get that? I will be great for any help. Thanks, Sparan (1 Reply)
Discussion started by: sparan_peddu
1 Replies

9. Shell Programming and Scripting

Sending text files as attachments

Hi, I have used the following command to send a text file "archiver.log" as attachment. (cat mail.log; uuencode archiver.log archiver.log; ) | mailx -s "Failure" asd@a.com : But in the mail i am not getting new lines in the text file.New Lines are getting replaced with some weird... (2 Replies)
Discussion started by: radhika03
2 Replies

10. Shell Programming and Scripting

Looking for command(s)/ script to find a text string within a file

I need to search through all files with different file suffixes in a directory structure to locate any files containing a specific string (5 Replies)
Discussion started by: wrwelden
5 Replies
Login or Register to Ask a Question