Sort files to the split second

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Sort files to the split second
# 1  
Old 01-04-2018
Sort files to the split second

I need a similar sort, I need to sort the directory by time, but time needs to be to the second or smaller.

Using ls -lt but that is only to the minute.

Last edited by rbatte1; 01-04-2018 at 11:58 AM.. Reason: Added ICODE tags
# 2  
Old 01-04-2018
Quote:
Originally Posted by mwlaursen
I need a similar sort, I need to sort the directory by time, but time needs to be to the second or smaller.

Using ls -lt but that is only to the minute.
If you have stat on your system, try:
Code:
stat --print='%Y %n\n' *

This will give the last modification time in seconds since the epoch and the name of the file in question. You should be able to do a numerical sort on the first field.

Andrew
# 3  
Old 01-04-2018
Do you mean that you want the seconds displayed in the listing? Using ls -lt will sort by seconds, but they won't display by default.

Code:
$ touch -mt 201801010000.00 /tmp/unix/third
$ touch -mt 201801010000.01 /tmp/unix/first
$ touch -mt 201801010000.02 /tmp/unix/second
$ touch -mt 201801010000.03 /tmp/unix/fourth
$
$ ls -l /tmp/unix
total 0
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 first
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 fourth
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 second
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 third
$ ls -lt /tmp/unix
total 0
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 fourth
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 second
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 first
-rw-rw-r-- 1 rbatte1 rbatte1 0 Jan  1 00:00 third
$

If you need to know the value of seconds, you would be better using the stat command for each file and extracting the part you want, e.g. stat -t /tmp/unix/* | sort -nk 13

This assumes that there are no spaces in the files/paths but might help. The thirteenth column is the file modification time (i.e. edit of the file content) which I'm guessing you might want more than other details. The value is in seconds from Epoch (1970-01-01 00:00:00) but you can use stat options to force the output into a format suitable for you.


I hope that this helps. If I've got it all wrong, please describe what you need.


Kind regards,
Robin
# 4  
Old 01-04-2018
Moderator's Comments:
Mod Comment Please DON'T hijack others' threads! (I created a new one for you)
Please use CODE tags as required by forum rules!
Please post your OS, shell, and tools' versions!


Quote:
Originally Posted by mwlaursen
. . .
Using ls -lt but that is only to the minute.
It is not; the sorting is done to the split second although mayhap the time stamp listed is only to the minute. I touched a few files within a second and used the --time-style long option to ls (which might not be available on your system):
Code:
ls -lat --time-style="+%F %T %N" ?
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 023427295 g
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 023427295 d
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 023427295 b
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 027427049 c
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 027427049 a
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 031426802 i
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 031426802 e
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 035426556 j
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 035426556 h
-rw-rw-r-- 1 user group 0 2018-01-04 17:32:49 035426556 f

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

2. UNIX for Beginners Questions & Answers

Split and Rename Split Files

Hello, I need to split a file by number of records and rename each split file with actual filename pre-pended with 3 digit split number. What I have tried is the below command with 2 digit numeric value split -l 3 -d abc.txt F (# Will Produce split Files as F00 F01 F02) How to produce... (19 Replies)
Discussion started by: techedipro
19 Replies

3. Shell Programming and Scripting

Sort and Split file with header and custom name

Hi, I am using SUN SOLARIS (SunOS sun4v sparc SUNW, T5240). I have a huge data file with header and trailer. This file gets used into an ETL process. ETL skips the header record (which is the first record of the file) and loads the rest of the record. The file can be delimited (comma,... (5 Replies)
Discussion started by: Saanvi1
5 Replies

4. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

5. Shell Programming and Scripting

sort split merge -u unique

Hi, this is about sorting a very large file (like 10 gb) to keep lines with unique entries across SOME of the columns. The line originally looked like this: sort -u -k2,2 -k3,3n -k4,4n -k5,5n -k6,6n file_unsorted > file_sorted please note the -u flag. The problem is that this single... (4 Replies)
Discussion started by: jbr950
4 Replies

6. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

7. Shell Programming and Scripting

sort and split file by 2 cols (1 col after the other)

Dear All, I am a newbie to shell scripting so this one is really over my head. I have a text file with five fields as below: 76576.867188 6232.454102 2.008904 55.000000 3 76576.867188 6232.454102 3.607231 55.000000 4 76576.867188 6232.454102 1.555146 65.000000 3 76576.867188 6232.454102... (19 Replies)
Discussion started by: Ghetz
19 Replies

8. Shell Programming and Scripting

Sort & Split records in a file

Hi, I am new to scripting. I need a script to sort and the records in a file and then split them into different files. For example, the file is: H1...................... H2...................... D2.................... D2.................... H1........................... (15 Replies)
Discussion started by: Sunitha_edi82
15 Replies

9. UNIX for Dummies Questions & Answers

Sort files by date, not showing files from today

Hi all, i'm new here in this forum. I really like the helpful answers in this forum. Here a short question. For a script i have to sort files by date and exclude the files of the actual date. Sorting the files by date and preparing the output for awk is done by this line: ls -l... (3 Replies)
Discussion started by: carlosdivega
3 Replies

10. UNIX for Dummies Questions & Answers

split files into specified number of output files

Hi everyone, I have some large text files that I need to split into a specific number of files of equal size. As far as I know (and I don't really know that much :)) the split command only lets you specify the number of lines or bytes. The files are all of a different size, so the number of... (4 Replies)
Discussion started by: Migrainegirl
4 Replies
Login or Register to Ask a Question