Linux sort by creation time

Sorting ls output by date in filename, in reverse order

On most unices, ls has a -t option. From the man page of my Debian box:

Try the following command:

@ChamindaBandara we’re 10 years on from this answer, so you’re probably better asking a new question. Reference this one, and provide not only the command you’re using but also example input and results showing how it’s not working for you

Is that a text file you’re trying to sort, or are you trying to view a directory listing by date? For the latter, use the -t flag to ls . For the former, see in particular the -M flag to GNU sort, which sorts by three-letter month name abbreviations. The following command sorts by the 3rd, 1st, 2nd and 4th columns, treating the second sort key as a month name.

sort -k 3 -k 1M -k2 -k4 in.txt 

Do consider always using Japanese/ISO style dates, always in the order YYYY-MM-DD-HH-MM-SS (i.e. most significant first, constant column width). This way sorting by date is identical to a lexicographic sort.

Don’t forget the leading zeroes on that date format suggestion. This is the format I use whenever I format the string myself.

@stolenmoment You can use spaces instead of zeros if you don’t mind spaces, the important point is a constant column with. 2018 5 26 works just as well as 2018 05 26 (as long as you don’t go through markup that merges consecutive spaces…), what doesn’t work well is 2018 5 26 .

I’m not sure if the functionality was in the ls command when the original question was asked, but now you can use the option —full-time .

will sort with oldest time at the bottom; or if you use:

then it will reverse the order.

The question is about how to sort file by modification date/time. The -t option answers that question completely; —full-time isn’t really relevant.

The only thing -t option doesn’t give you the year for files modified within six months where as —full-time gives you exactly that. You can also use time-style to format a date format to you liking. e.g ls -ltr —time-style+%Y-%m-%d\ %H:%M:%S

I’m not sure what your point is. ls -t doesn’t give you mode and owner of the files, the host name and operating system version, or the price of tea in China, either. ls -t doesn’t even display the modification date at all. But the question isn’t about displaying file information, it’s about sorting files by modification date. You are not contributing to the answer of the question; you are adding nice-to-know supplementary information. When you get 50 reputation points, you can post that sort of information as a comment.

Читайте также:  Mysql linux ubuntu server

Even though ls -t does not show other info(mode/owner/group), adding -l will do. I use ll -t and ll is a common alias ls -l which solves the problem. For me, it is confusing at first that for changes older than 6 months, the year columns shows year, while when it’s

I like the approach of using sort posted in the answer by Gilles ‘SO- stop being evil’, but it’s not quite all the way there. First, technically the OP wants reverse chronological order, not forward, and so r needs to be addd to the sort criterial.
So starting with sort -k 3r -k 1Mr -k 2r -k 4r se.txt that would give you

Feb 27 2011 23:05 SOMETHING 2011.02.24.avi Feb 11 2011 20:06 SOMETHING 2011.02.10.avi Jan 23 2011 10:42 SOMETHING 2007.12.20.avi Jan 29 2011 09:17 SOMETHING 2011.01.27.avi Feb 12 2010 SOMETHING 2010.02.11.avi Jun 26 2009 SOMETHING 2009.06.25.avi 

But look closely and you’ll note Jan 23, 2011 and Jan 29, 2011 are in the wrong order. This is due to a subtle bug, or perhaps unclear documentation in sort . An individual sort KEYDEF such as -k2 means starting from field 2, not only on field 2. It is best to also specify the stopping field as well (e.g. -k2,2 means sorting from field 2 to field 2, i.e. only field 2).

sort -k 3,3r -k 1,1Mr -k 2,2r -k 4,4r 

yields the correct results. (I was able to get this to work by setting only some of the stop fields in the KEYDEFs, but I don’t know which are necessary and why, so it seems best to specify them all. Well, except maybe the last one, but then in the OP input file there are some records with 4 fields and some with 5 so the 4th field is not always the final field)

Источник

How can I sort a directory by creation date in BASH?

I’m trying to create a simple script to list the 16 most recent folders created in a directory on my nas machine as a way to display the most recent movies added to my collection. the script i am using at the moment is:

#!/bin/bash rm -f /volume1/new-movies/* IFS=$'\x0A' fresh=$(ls -1ct /volume1/movies | head -16) for folder in $fresh do file=$(find "/volume1/movies/$folder" -maxdepth 1 -type f) movie=$(basename "$file") ln -s "$file" "/volume1/new-movies/$movie" done ls -1 /volume1/new-movies 

which is OK (the movies folder will only ever contain folders). My problem is this is sorted by the file/folders modification time rather than the creation time. the filesystem is ext4 and should support a birth time but i have had no luck accessing it.

scott@pandora scripts $ stat /volume1/movies/example/ File: '/volume1/movies/example/' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 902h/2306d Inode: 373800961 Links: 2 Access: (0755/drwxr-xr-x) Uid: ( 1028/ scott) Gid: ( 100/ users) Access: 2013-04-09 13:39:53.243991684 +1000 Modify: 2013-04-06 13:26:00.965998952 +1100 Change: 2013-04-09 11:46:23.280991727 +1000 Birth: - 

however, samba seems to have no issue displaying the correct creation date/time. is there a way to access the same information from bash? or am i going to have to program something in python/other to do what i need by accessing smb directly and listing each folder with the creation date?

scott@pandora scripts $ smbclient \\\\localhost\\movies\\ Enter scott's password: Domain=[EXAMPLENET] OS=[Unix] Server=[Samba 3.6.9] smb: \> allinfo "example" altname: E06KNE~A create_time: Fri Jun 18 17:23:49 2010 EST access_time: Tue Apr 9 13:39:53 2013 EST write_time: Sat Apr 6 13:26:01 2013 EST change_time: Sat Apr 6 13:26:01 2013 EST attributes: DA (30) smb: \> quit 

Источник

Читайте также:  Linux отключить звук приложения

How do I do a ls and then sort the results by date created?

In what order are the dated ordered by? Certainly not alphanumeric order. ls -lt sorts by modification time. But I need creation time.

There does seem to be a way to get the file creation time in Linux if the file is on a ext4 file system. See: unix.stackexchange.com/a/131347/182996

7 Answers 7

Most unices do not have a concept of file creation time. You can’t make ls print it because the information is not recorded. If you need creation time, use a version control system: define creation time as the check-in time.

If your unix variant has a creation time, look at its documentation. For example, on Mac OS X (the only example I know of), use ls -tU . Windows also stores a creation time, but it’s not always exposed to ports of unix utilities, for example Cygwin ls doesn’t have an option to show it. The stat utility can show the creation time, called “birth time” in GNU utilities, so under Cygwin you can show files sorted by birth time with stat -c ‘%W %n’ * | sort -k1n .

Note that the ctime ( ls -lc ) is not the file creation time, it’s the inode change time. The inode change time is updated whenever anything about the file changes (contents or metadata) except that the ctime isn’t updated when the file is merely read (even if the atime is updated). In particular, the ctime is always more recent than the mtime (file content modification time) unless the mtime has been explicitly set to a date in the future.

Читайте также:  Dwg linux viewer free

Источник

Linux combine sort files by date created and given file name

I need to combine these to commands in order to have a sorted list by date created with the specified «filename». I know that sorting files by date can be achieved with:

enter image description here

I don’t know how to combine these two. I tried with a pipeline but I don’t get the right result. [EDIT] Not sorted

4 Answers 4

find . -name "filename" -printf '%TY:%Tm:%Td %TH:%Tm %h/%f\n' | sort 

Forget xargs. «Find» and «sort» are all the tools you need.

Well, you’re here to learn things, aren’t you? I didn’t even remember that find has a -printf option until I checked «man find» and noticed it.

My best guess would be to use xargs :

find . -name 'filename*' -print0 | xargs -0 /bin/ls -ltr 

There’s an upper limit on the number of arguments, but it shouldn’t be a problem unless they occupy more than 32kB (read more here), in which case you will get blocks of sorted files 🙂

+1: I’m curious how you arrive at ‘more than 32k of them’. I suspect the limit is smaller than that. IIRC, on Linux, the total size of environment + command line arguments has to be 128 KiB or less, in which case the limit on the file names of length 8 characters each is 16k or so. However, that’s cavilling at details, not a major objection.

@JonathanLeffler You’re right, it should be 32kB (depends on the system but seems a reasonable minimum), not 32000 arguments 🙂

find . -name "filename" -exec ls --full-time \ \; | cut -d' ' -f7- | sort 

You might have to adjust the cut command depending on what your version of ls outputs.

Check the below-shared command:

1) List Files directory with Last Modified Date/Time To list files and shows the last modified files at top, we will use -lt options with ls command.

$ ls -lt /run output total 24 -rw-rw-r--. 1 root utmp 2304 Sep 8 14:58 utmp -rw-r--r--. 1 root root 4 Sep 8 12:41 dhclient-eth0.pid drwxr-xr-x. 4 root root 100 Sep 8 03:31 lock drwxr-xr-x. 3 root root 60 Sep 7 23:11 user drwxr-xr-x. 7 root root 160 Aug 26 14:59 udev drwxr-xr-x. 2 root root 60 Aug 21 13:18 tuned 

Источник

Оцените статью
Adblock
detector