Find the latest file in multiple files in Linux

Source: Internet
Author: User

Q: How can I find the latest file in multiple files (for example, *. cpp) in Linux?

 

A: You can use the following command format:

(1) sort the files from new to old and take the first one.

Ls-t *. cpp | head-1

(2) sort the files from old to new and take the last one.

Ls-rt *. cpp | tail-1

 

Example:

[Root @ jfht synway] # ls-lrt *. cpp
-Rw-r -- 1 root 246 2007-09-12 ch_set.cpp
-Rw-r -- 1 root 10073 2008-11-04 ssv_codec.
-Rw-r -- 1 root 2199 2008-11-04 main. cpp
-Rw-r -- 1 root 600 2008-11-04 config. cpp
-Rw-r -- 1 root 2533 2008-11-07 syn_thread.cpp
-Rw-r -- 1 root 998 2008-11-07 ssv_server.cpp
-Rw-r -- 1 root 5301 2008-11-07 ctsc_session.cpp
-Rw-r -- 1 root 9692 2008-11-07 ctmc_session.cpp
-Rw-r -- 1 root 83121 2010-06-30 ch_info.cpp

From the above output, it is clear that ch_info.cpp is the latest.
[Root @ jfht synway] # ls-t *. cpp | head-1
Ch_info.cpp
[Root @ jfht synway] # ls-rt *. cpp | tail-1
Ch_info.cpp
[Root @ jfht synway] #

Write it into a script for execution, as shown below:

[Root @ jfht synway] # vi newest_file.sh
#! /Bin/sh

Newest_file_of ()
{
Ls-t "$ @" | head-1
}

Echo "newest file of *. cpp is $ (newest_file_of *. cpp )"

[Root @ jfht synway] #./newest_file.sh
Newest file of *. cpp is ch_info.cpp
[Root @ jfht synway] #

 

 

A detailed explanation of the above commands:

 

Ls-t

Sort by modification time, and the newer files are listed at the front (sort by modification time), that is, sort by the new to the old files. If you want to sort data from old to new, you need to add the-r parameter (reverse ).

Man ls wrote
-L use a long listing format

-R, -- reverse
Reverse order while sorting

-T sort by modification time
 

 

Head-1

Take the first line. You can also write: head-n 1.

Man head writes
-N, -- lines = [-] N
Print the first N lines instead of the first 10; with the leading '-', print all but the last N lines of each file
 

 

Tail-1

Take the last row. You can also write: tail-n 1.

Man tail wrote
-N, -- lines = N
Output the last N lines, instead of the last 10

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.