Find and Sort Files Based on Modification Date and Time in Linux
People comfortable with GUI’s can find it using File Manager, which lists files in long listing format, making it easy to figure out what we wanted, but those users having habit of black screens, or even anyone working on servers which are devoid of GUI’s would want a simple command or set of commands that could ease out their search.
Real beauty of Linux shows here, as Linux has a collection of commands which if used separately or together can help to search for a file, or sort a collection of files according to their name, date of modification, time of creation, or even any filter you could think of applying to get your result.
Here, we will unveil the real strength of Linux by examining a set of commands which can help sorting a file or even a list of files by Date and Time.
Linux Utilities to Sort Files in Linux
Here, we will unveil the real strength of Linux by examining a set of commands which can help sorting a file or even a list of files by Date and Time.
Linux Utilities to Sort Files in Linux
Some basic Linux command line utilities that are just sufficient for sorting a directory based on Date and Time are:
ls command
ls – Listing contents of directory, this utility can list the files and directories and can even list all the status information about them including: date and time of modification or access, permissions, size, owner, group etc.
sort command
sort – This command can be used to sort the output of any search just by any field or any particular column of the field.
These commands are in themselves very powerful commands to master if you work on black screens and have to deal with lots of files, just to get the one you want.
Some Ways to Sort Files using Date and Time
Below are the list of commands to sort based on Date and Time.
1. List Files Based on Modification Time
These commands are in themselves very powerful commands to master if you work on black screens and have to deal with lots of files, just to get the one you want.
Some Ways to Sort Files using Date and Time
Below are the list of commands to sort based on Date and Time.
1. List Files Based on Modification Time
The below command lists files in long listing format, and sorts files based on modification time, newest first. To sort in reverse order, use
'-r'
switch with this command.
2. List Files Based on Last Access Time
Listing of files in directory based on last access time, i.e. based on time the file was last accessed, not modified.
Listing of files in directory based on last access time, i.e. based on time the file was last accessed, not modified.
3. List Files Based on Last Modification Time
Listing of files in directory based on last modification time of file’s status information, or the
Listing of files in directory based on last modification time of file’s status information, or the
'ctime'
. This command would list that file first whose any status information like: owner, group, permissions, size etc has been recently changed.
If
'-a'
switch is used with above commands, they can list and sort even the hidden files in current directory, and. '-r'
switch lists the output in reverse order.
For more in-depth sorting, like sorting on Output of find command, however
ls
can also be used, but there 'sort'
proves more helpful as the output may not have only file name but any fields desired by user.
Below commands show usage of
4. Sorting Files based on Month
sort
with find
command to sort the list of files based on Date and Time.4. Sorting Files based on Month
Here, we use
find
command to find all files in root (‘/’)
and then print the result as: Month in which file was accessed and then filename. Of that complete result, here we list out top 11 entries.
The below command sorts the output using key as first field, specified by
'-k1'
and then it sorts on Month as specified by 'M'
ahead of it.
5. Sort Files Based on Date
Here, again we use
Here, again we use
find
command to find all the files in root directory, but now we will print the result as: last date the file was accessed, last time the file was accessed and then filename. Of that we take out top 11 entries.
The below sort command first sorts on basis of last digit of the year, then sorts on basis of last digit of month in reverse order and finally sorts on basis of first field. Here, '1.8' means 8th column of first field and ‘n’ ahead of it means numerical sort, while 'r' indicates reverse order sorting.
Here,again we use
find
command to list out top 11 files in root directory and print the result in format: last time file was accessed and then filename.
The below command sorts the output based on first column of the first field of the output which is first digit of hour.
7. Sorting Ouptut of ls -l based on Date
This command sorts the output of
This command sorts the output of
'ls -l'
command based on 6th field month wise, then based on 7th field which is date, numerically.
Conclusion
Likewise, by having some knowledge of sort command, you can sort almost any listing based on any field and even its any column you desire. These were some of tricks to help you sort files based on Date or Time. You can have your own tricks build based on these. However, if you have any other interesting trick, you can always mention that in your comments.
Likewise, by having some knowledge of sort command, you can sort almost any listing based on any field and even its any column you desire. These were some of tricks to help you sort files based on Date or Time. You can have your own tricks build based on these. However, if you have any other interesting trick, you can always mention that in your comments.
0 comments:
Post a Comment