Visit The New, Modern Unix Linux Community

How to sort a set of files by date in a directory?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to sort a set of files by date in a directory?
# 1  
How to sort a set of files by date in a directory?

hi there,
I have a directory which contents I can parse dynamically. I end up with a file list. I then want to display those files sorted by date, oldest files first. I have very very little PERL experience...Would anyone know how to do that ? Thanks in advance.
# 2  
One idea, in Perl, might be to create a hash from the filelist, using the mtime (obtained via stat) as the key. Afterwards it's only a foreach $time (sort keys %hash)
Or, on the shell:
ls -tr1 | sed -e '1d'

Last edited by pludi; 03-12-2009 at 06:16 AM.. Reason: code tags
# 3  
what would this look like in PERL though ?
# 4  
Something like this (for all the .pl files in the current directory):
perl -e 'print join "\n", map { split /\t/; $_ = $_[1]; } sort map { @s = stat; $_ = "$s[9]\t$_"; } <*.pl>;'

Though I'm sure there are a few Perl gurus around who could do this with even less code. That last join is only for pretty printing, if you want to do something with your list you can omit it.
# 5  
ok this works fine from a Unix shell. But when I integrate this into my script, I get this compilation error: "Use of implicit split to @_ is deprecated at line 19.". My Perl book also says that using @_ in this context is now deprecated. Would you know the workaround here ?
# 6  
Change the line so that it splits to an array and use that (Hint: it's in the leftmost map)
# 7  
here's what I end up with:
my @temp_array;
my @s;
my @sortedlist = map {  
                    @temp_array = split /\t/; 
                    @temp_array = $temp_array[1];}
                 map { @s = stat; $_ = "$s[9]\t$_"; }

print "@sortedlist\n";

now, I'm getting this error "Use of uninitialized value in concatenation (.) or string at line 20.", line 20 being the "map { @s = stat; $_ = "$s[9]\t$_"; }" statement. Looks like it's not liking $_, right ?

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #442
Difficulty: Easy
84% of the women surveyed by Cosmopolitan Magazine in 2018 indicated they would prefer to date a computer programmer than a rock star.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to sort files in directory numerically?

Trying to sort a bunch of files numerically but can't seem to get the command just right. This is in a IBM AIX machine. I have a directory that has... backup.bk1 backup.bk100 backup.bk2 backup.bk200 backup.bk3 backup.bk300 There are a lot more files but this is shortened for the... (5 Replies)
Discussion started by: c3rb3rus
5 Replies

2. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some some_name_2016-01-17-22-40-30_some some_name_2016-01-17-22-48-50_some and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

3. AIX

How to set owner and permission for files/directory in directory in this case?

Hi. My example: I have a filesystem /log. Everyday, log files are copied to /log. I'd like to set owner and permission for files and directories in /log like that chown -R log_adm /log/* chmod -R 544 /log/*It's OK, but just at that time. When a new log file or new directory is created in /log,... (8 Replies)
Discussion started by: bobochacha29
8 Replies

4. Shell Programming and Scripting

To get the files in a directory for the given date (User entered date)

Need a ksh script to get the files that were created or modified in a directory on a particular date entered by the user. For example if a directory contains files as below : > ll total 41 -rw-rw-r-- 1 psn psn 199 Aug 23 07:06 -rw-rw-r-- 1 psn psn ... (10 Replies)
Discussion started by: ramprabhum
10 Replies

5. Shell Programming and Scripting

Sort files by date in filename

Hi, I am a newbie to shell programming and I need some help in sorting a list of files in ascending order of date in the filenames. The file format is always : IGL01_AC_D_<YYYYMMDD>_N01_01 For example, in a directory MyDirectory I have the following files: IGL01_AC_D_20110712_N01_01.dat... (11 Replies)
Discussion started by: Yuggy
11 Replies

6. Shell Programming and Scripting

Creating date directory and moving files into that directory

I have list of files named file_username_051208_025233.log. Here 051208 is the date and 025233 is the time.I have to run thousands of files daily.I want to put all the files depending on the date of running into a date directory.Suppose if we run files today they should put into 05:Dec:08... (3 Replies)
Discussion started by: ravi030
3 Replies

7. Shell Programming and Scripting

How to Sort files on date field

:cool: Hi all, I have a pecular issue in sorting these files in Solaris environment. All the below files are modified on November 4th, but I want to sort these files as per date column (eg: 01May07_1623 = ddmmmyy_hhmm) Nov 4 18:27 SONYELEC00.GI22973.01May07_1623.gpg Nov 4 18:27... (4 Replies)
Discussion started by: shivaastrogun
4 Replies

8. 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

9. Linux

sort files by date

Hi All, Sorry to throw this frequent question but I lost my notes on it. How do you list the files by date? I'm on red hat. Thanks in advance, itik (1 Reply)
Discussion started by: itik
1 Replies

10. AIX

loop through the directory for files and sort by date and process the first file

hello i have a requirement where i have a direcotry in which i get files in the format STOCKS.20080114.dat STOCKS.20080115.dat STOCKS.20080117.dat STOCKS.20080118.dat i need to loop through the directory and sort by create date descending order and i need to process the first file. ... (1 Reply)
Discussion started by: dsdev_123
1 Replies

Featured Tech Videos