- How to Find and Sort Files Based on Modification Date and Time in Linux
- Linux Utilities to Sort Files in Linux
- ls command
- sort command
- Some Ways to Sort Files using Date and Time
- 1. List Files Based on Modification Time
- 2. List Files Based on Last Access Time
- 3. List Files Based on Last Modification Time
- 4. Sorting Files based on Month
- 5. Sort Files Based on Date
- 6. Sorting Files Based on Time
- 7. Sorting Ouptut of ls -l based on Date
- Conclusion
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- Find And Sort Files Based On Access, Modification Date And Time In Linux
- Find and sort files based on access, modification date and time in Linux
- 1. Find and sort files based on last access time using ls command
- 2. Find and sort files based on modification time using ls command
- 3. Find and sort files based on last modification time using ls command
- 4. Find and sort files based on date
- How To Find Largest Top 10 Files and Directories On Linux / UNIX / BSD
- How to find out top 10 files and directories on Linux or Unix
- Steps to find Largest Directories in Linux
- How to find out top Directories and files in Linux
- Find the largest file in a directory and its subdirectories using the find command
- Hunt down disk space hogs with ducks
How to Find and Sort Files Based on Modification Date and Time in Linux
Usually, we are in habit of saving a lot of information in form of files on our system. Some, hidden files, some kept in a separate folder created for our ease of understanding, while some as it is. But, this whole stuff fills our directories; usually desktop, making it look like a mess. But, the problem arises when we need to search for a particular file modified on particular date and time in this huge collection.
Find and Sort Files by 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
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.
We’ve already covered many articles on Linux ls command and sort command, you can find them below:
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.
We’ve already covered two articles on Linux sort command, you can find them below:
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.
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 ‘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 sort with find command to sort the list of files based on Date and Time.
To learn more about find command, follow this link: 35 Practical Examples of ‘find’ Command in Linux
4. Sorting Files based on Month
Here, we use find command to find all files in root (‘/’) directory 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 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.
6. Sorting Files Based on Time
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 ‘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.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
Find And Sort Files Based On Access, Modification Date And Time In Linux
We already know how to organize the similar type of files in specific folders in Linux and Unix-like operating systems. In that method, we learned to organize the similar type of files in a folder. In other words, the files have been organized based on their extension. We don’t know whether the file is new or old one. We simply put the same type of files in a folder. Today, in this guide, we are going to find and sort files based on access and modification date and time, not extension. In case, if you want to find a particular file accessed or modified on a particular date or time, you’re on the right track. Read on.
Find and sort files based on access, modification date and time in Linux
You don’t need to install any extra applications to find and sort files. For the purpose of this guide, we are going to use three Linux commands namely ls, find and sort. These commands are the part of Linux Kernel, so don’t bother installing them.
As may already know, the ls command lists a directory contents with additional information such as date and time of modification or access, permissions, size, owner, group etc. It will sort the entries of the given directory, alphabetically. If you didn’t mention the directory, it will list the contents of current working directory.
The find command is used to search for files in a directory hierarchy.
The sort command sorts the lines of text files and writes sorted concatenation of all FILE(s) to standard output.
1. Find and sort files based on last access time using ls command
The following command will list files based on the last access time. The newest files will be listed first.
Sample output:
- -u — Sort by access time, newest first
2. Find and sort files based on modification time using ls command
To list files based on the modification time, run:
- -l — Indicates long listing format,
- -t — Sort by modification time, newest first.
Sample output:
The above command will sort the files of the current working directory based on the modification time. The newest files will be displayed first.
To list the files of a specific directory based on modification time, type:
To list the files in reverse order, i.e oldest files first, use ‘r’ flag like below:
3. Find and sort files based on last modification time using ls command
The command lists the files based on the last modification time. It means, this command lists the files first whose any status information (such as owner, group, permissions, size etc) have been recently changed.
Sample output:
- -c — Sort by last modification time
4. Find and sort files based on date
The following command lists the files based on last accessed date and time. Newest files will be listed first.
Sample output:
In the above output, the first column shows the last accessed date of the file, second column shows the last accessed time and third column shows the name of file. Here, ‘head -n 10’ is not the number files. It is number of entries in the output including the blank spaces.
Also, you can use «sort» command with «ls» command to list the files based on last accessed month, date, and time like below.
Sample output:
- -k6M — Lists files monthly wise (6th field) in the output.
- -k7n — Lists files date wise (7th field) in the output.
And, that’s all. You know now how to find and sort files based on the access, modification time and date in Linux. As you can see in the above examples, it is fairly easy to sort the files. For more details, refer man pages of each command.
Источник
How To Find Largest Top 10 Files and Directories On Linux / UNIX / BSD
How to find out top 10 files and directories on Linux or Unix
There is no simple command available to find out the largest files/directories on a Linux/UNIX/BSD filesystem. However, combination of following three commands (using pipes) you can easily find out list of largest files:
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | Linux and Unix-like OS |
Est. reading time | 3 minutes |
Steps to find Largest Directories in Linux
- du command : Estimate file space usage.
- sort command : Sort lines of text files or given input data.
- head command : Output the first part of files i.e. to display first 10 largest file.
- find command : Search file.
How to find out top Directories and files in Linux
Type the following command at the shell prompt to find out top 10 largest file/directories:
# du -a /var | sort -n -r | head -n 10
Sample outputs:
If you want more human readable output try (works with GNU/Linux du version/user only):
$ cd /path/to/some/where
$ du -hsx * | sort -rh | head -10
$ du -hsx — * | sort -rh | head -10
Where,
- du command -h option : display sizes in human readable format (e.g., 1K, 234M, 2G).
- du command -s option : show only a total for each argument (summary).
- du command -x option : skip directories on different file systems.
- sort command -r option : reverse the result of comparisons.
- sort command -h option : compare human readable numbers. This is GNU sort specific option only.
- head command -10 OR -n 10 option : show the first 10 lines.
The above command will only work of GNU/sort is installed. Other Unix like operating system should use the following version (see comments below):
Find the largest file in a directory and its subdirectories using the find command
Type the following GNU/find command:
You can skip directories and only display files, type:
Hunt down disk space hogs with ducks
Let us find out top directories and files using disk space in Linux or Unix with help of the following bash shell alias:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Run it as follows to get top 10 files/dirs eating your disk space:
$ ducks
Sample outputs:
Fig.01 Finding the largest files/directories on a Linux or Unix-like system
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Great, but what if I only want the largest files and not the directories?
To find out largest file only use command ls as follows in current directory:
ls -lSh . | head -5
Output:
-rw-r–r– 1 vivek vivek 267M 2004-08-04 15:37 WindowsXP-KB835935-SP2-ENU.exe
-rw-r–r– 1 vivek vivek 96M 2005-12-30 14:03 VMware-workstation-5.5.1-19175.tar.gz
ls -lSh /bin | head -5
You can also use find command but not du:
find /var -type f -ls | sort -k 7 -r -n | head -10
Hope this helps
And yes to find the smallest files use command:
ls -lSr /var
Or use find command with -size flag.
find / -type f -size +20000k -exec ls -lh <> ; | awk ‘< print $8 “: ” $5 >’
Read man page of find for more info.
“find / -type f -size +20000k -exec ls -lh <> ; | awk ‘< print $8 “: ” $5 >’”
needs to have the exec altered
find / -type f -size +20000k -exec ls -lh <> \; | awk ‘< print $8 “: ” $5 >’
Also, I find this output easier to read
find . -type f -size +20000k -exec ls -lh <> \; | awk ‘
How do I can list all the files in several directories and at the end write the totat of all the files and directories.I’m using the du command as fallow:
du -sh /public/base/sites/F*/*20070115*
this command give me the size of all the files but not the global total.
can somebody help me. please write me. john_fernandez@verizon.com.do
“If you want more human readable output try:
# du -ha /var | sort -n -r | head -n 10”
Im pretty sure that this will put 999kb above 1gb so I don’t think that this works.
This does not work.
# du -ha /var | sort -n -r | head -n 10″
as Joe says this ignores files over 1gb
You could try this, gives a human readable output in MB
find . -type f | xargs ls -s | sort -rn | awk ‘
Human readable version:
for X in $(du -s * | sort -nr | cut -f 2); do du -hs $X ; done
If you set du to human readable I think it will not sort the way you really want.
For the above problems. I would like to find a way to list only the last level directories’ sizes.
(I want to filter somehow this:
/home
/home/user
/home/user/mail
I just want to see the lasts of the tree!)
this is what i use.
for i in G M K; do du -ah | grep 5$i | sort -nr -k 1; done | head -n 11
Worked like charm
find . -type f -print0| xargs -0 ls -s | sort -rn | awk ‘
! -print0 for filenames with spaces …
(and xargs -0 combined)
No wonder windows rule
Yeah you just go ahead and wait the 3 hours this search would take on Windows.
How about the 3 hours it takes to read through a bunch of unexplained programming nonsense. I swear half of the time all Linux guys do is insult other users….Example, the first listing is great as it begins to explain what the flags do, but I have no idea were to put some of them, pipes are not explained…ect
This is why Winblows users should not try and use Linux, they are very unintelligent and lack the ability to look up simple things.
Fast forwarda few years, Linux won, bro 🙂
“Late last week, hell had apparently frozen over with the news that Microsoft had developed a Linux distribution of its own. The work was done as part of the company’s Azure cloud platform, which uses Linux-based network switches as part of its software-defined networking infrastructure.” — SOURCE HERE.
Winfan, I read this page wondering how to do these things in Windows — can you post instructions? Thanks!
This may vary depending on the version of Windows you’re using, but the basic procedure is: open the find/search window, go to the advanced options, and there will be an option there to enter in a size parameter. Simple.
In XP, press F3 or go Start->Search. Choose “All files and folders”, then “More advanced options”, then “What size is it?”, then specify a size.
I believe beez was being sarcastic towards Winfan as was his right after Winfans brainless comment. Most Linux distro GUI’s come with search function just like Windows GUI.
Now just try to do the search on command line on Windows (server)…
@winfan… how do you do this in windows? you don’t 😛
c:\>dir /S /O-S | more
The simple dir /S command from c:\ will give you all files and directories from c:\ all the way through the drive and will sort from largest to smallest running through each directory. You can filter using /A if you’d like to restrict by hidden, system, archive files, read only files etc. and passing the output to another windows command if you need to further restrict or search in the files for something like “show me all the files on my hard drive over 6MB that contain the word ‘log’ from largest to smallest.”
/O will Specify the sort order for the files with the S after it sorting by file size (smallest first) putting the – in front of the S reverses the order.
| more – you’re a unix dude, you should know what this means…
But if someone is doing some cleanup through their harddrive, this is the simple command I’d start with.
Just a note about the cockyness or us Unix admins (as I happen to be one now)
Not everyone that uses windows started using it with a mouse kid. Also not everyone who prefers windows is not cross-platform… We were running 64 bit clustered NT boxes on RISC processors at Digital Equipment Corporation with failover and failback in 1996 brother. Don’t believe me? Find a really old copy of Windows NT ver 3.51 open it and you’ll see two folders NT and Alpha.
The Department of Veterans Affairs had no problems with ever needing to reboot a “lousy unreliable windows box” because the Intel platform itself was the problem, not windows. We ran Alpha on 64 bit RISC processors and it was just as reliable as any Unix box or Mainframe we had. I had a Jensen Alpha running an exchange server for 5 years, and we only rebooted it every 6 months for giggles…
Windows machines are made to be used by the masses which means more dumbasses can kinda run one. A good Admin is a good Admin, no matter what platform. Be nice and be helpful or don’t post.
Notafan wrote:
@winfan… how do you do this in windows? you don’t 😛
Well, actually, there *is* cygwin (unix commands for Windows systems)
http://www.cygwin.com/
I find the following works rather well…
It lists all files or directories bigger than 50MB (just change size>50 to alter that) in the current directory (change the “.” to a directory path to specify another one) in a friendly, human-readable way and happily plays with spaces (I used it on an NTFS mount in fact).
I get a syntax error when coptying and pasting this command
Источник