Linux command last file

How to Find Recent or Today’s Modified Files in Linux

In this article, we will explain two, simple command line tips that enable you to only list all today’s files.

One of the common problems Linux users encounter on the command line is locating files with a particular name, it can be much easier when you actually know the filename.

However, assuming that you have forgotten the name of a file that you created (in your home folder which contains hundreds of files) at an earlier time during the day and yet you need to use urgently.

Below are different ways of only listing all files that you created or modified (directly or indirectly) today.

1. Using the ls command, you can only list today’s files in your home folder as follows, where:

  1. -a – list all files including hidden files
  2. -l – enables long listing format
  3. —time-style=FORMAT – shows time in the specified FORMAT
  4. +%D – show/use date in %m/%d/%y format
# ls -al --time-style=+%D | grep 'date +%D'

Find Recent Files in Linux

In addition, you can sort the resultant list alphabetically by including the -X flag:

# ls -alX --time-style=+%D | grep 'date +%D'

You can also list based on size (largest first) using the -S flag:

# ls -alS --time-style=+%D | grep 'date +%D'

2. Again, it is possible to use the find command which is practically more flexible and offers plenty of options than ls, for the same purpose as below.

  1. -maxdepth level is used to specify the level (in terms of sub-directories) below the starting point (current directory in this case) to which the search operation will be carried out.
  2. -newerXY , this works if timestamp X of the file in question is newer than timestamp Y of the file reference. X and Y represent any of the letters below:
    1. a – access time of the file reference
    2. B – birth time of the file reference
    3. c – inode status change time of reference
    4. m – modification time of the file reference
    5. t – reference is interpreted directly as a time

    This means that, only files modified on 2016-12-06 will be considered:

    # find . -maxdepth 1 -newermt "2016-12-06"

    Find Today

    Important: Use the correct date format as reference in the find command above, once you use a wrong format, you will get an error as the one below:

    # find . -maxdepth 1 -newermt "12-06-2016" find: I cannot figure out how to interpret '12-06-2016' as a date or time 

    Alternatively, use the correct formats below:

    # find . -maxdepth 1 -newermt "12/06/2016" OR # find . -maxdepth 1 -newermt "12/06/16"

    Find Todays Modified Files in Linux

    You can get more usage information for ls and find commands in our following series of articles on same.

    In this article, we explained two important tips of how to list only today’s files with the help of ls and find commands. Make use of the feedback form below to send us any question(s) or comments about the topic. You can as well inform us of any commands used for the same goal.

    Источник

    Find Files That Have Been Modified Recently in Linux

    announcement - icon

    The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

    To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

    Connect your cluster and start monitoring your K8s costs right away:

    1. Introduction

    There are various occasions when we want to search for files that have been changed recently.

    For example, as a system admin, we’re responsible to maintain and configure computer systems. Sometimes, because we’re dealing with a lot of configuration files, we probably want to know what are the files recently modified.

    In this tutorial, we’re going to find the files that have been changed recently in Linux using bash commands.

    2. The find Command

    First, we’ll explore the find utility which is the most common way to achieve the intended purpose. This command is used to find files and directories recursively and to execute further operations on them.

    2.1. -mtime and -mmin

    -mtime is handy, for example, if we want to find all the files from the current directory that have changed in the last 24 hours:

    Note that the . is used to refer to the current directory. -mtime n is an expression that finds the files and directories that have been modified exactly n days ago.

    In addition, the expression can be used in two other ways:

    • -mtime +n = finds the files and directories modified more than n days ago
    • -mtime -n = finds the files and directories modified less than n days ago

    In the same way, we can use the -mmin n expression to rely on minutes instead of days:

    So, this command recursively finds all the files and directories from the /home/sports directory modified at least 120 minutes ago.

    Next, if we want to limit the searching only to files, excluding directories, we need to add the -type f expression:

    find /home/sports -type f -mmin +120

    Furthermore, we can even compose expressions. So, let’s find the files that have been changed less than 120 minutes ago and more than 60 minutes ago:

    find . -type f -mmin -120 -mmin +60

    2.2. -newermt

    There are times when we want to find the files that were modified based on a particular date. In order to fulfill this requirement, we have to explore another parameter, which has the following syntax:

    By using this expression, we can get the files that have been changed earlier than the specified date.

    So, let’s build a command to better understand the new parameter:

    find . -type f -newermt 2019-07-24

    Moreover, we could get the files modified on a specific date by using a composed expression.

    So, we’re going to get the files modified on ‘2019-07-24’:

    find . -type f -newermt 2019-07-24 ! -newermt 2019-07-25

    Finally, there’s another version of the -newermt parameter similar to -mmin and -mtime.

    The first command finds the files modified in the last 24 hours. The rest of them are similar:

    find . -type f -newermt "-24 hours" find . -type f -newermt "-10 minutes" find . -type f -newermt "1 day ago" find . -type f -newermt "yesterday"

    3. The ls Command

    We know that the ls command lists information about the files in a specific directory. One of its usages is to show the long format of the files and to sort the output by modification time:

    Which would result in something like:

    -rw-r--r-- 1 root root 4233 Jul 27 18:44 b.txt -rw-rw-r-- 1 root root 2946 Jul 27 18:12 linux-commands.txt -rw-r--r-- 1 root root 5233 Jul 20 17:02 a.txt

    We may not be able to list the files recently modified exactly as the find command does. But, we can filter the above output based on a specific date or time by applying the grep command on the result of the ls command:

    -rw-r--r-- 1 root root 4233 Jul 27 18:44 b.txt -rw-rw-r-- 1 root root 2946 Jul 27 18:12 linux-commands.txt
    -rw-r--r-- 1 root root 5233 Jul 20 17:02 a.txt

    Note that the find command is recursive by default. In order to enable the recursive capability on the ls command, we also need to add the R(uppercase) parameter:

    4. Conclusion

    In this quick tutorial, we’ve described a few ways that help us find the files that have been changed recently on a Linux operating system.

    First, we’ve explored the find command and created several examples with different parameters like -mtime, -mmin and -newermt.

    Then, we’ve shown how we can achieve similar results using a combination of two better known Linux utilities like the ls and grep commands.

    Источник

    How to Find Last Modified Files in Linux?

    This tutorial explains how to find last modified files in Linux using different commands and according to custom needs.

    After reading this tutorial you’ll know how to execute the following tasks:

    • How to find files modified in a specific day range
    • How to find last modified specific file type (e.g mp4, png)
    • Finding files modified before / after X minutes
    • How to find files modified in a specific date
    • Finding modified files recursively
    • Search omitting files or directories
    • Find files by access date

    Finding last day modified files in Linux:

    To start, let’s search files modified less than a day ago. To find files modified a day ago you can use the commands find and newermt used in the following example.

    The find command is used to search files. The newermt command compares files timestamp with the argument passed, in this case “1 day ago”. Then, the ls command is passed to list the files.

    To find last day modified files, you can also use the mtime command together with find. By specifying the option 0 as in the example below, mtime will return all files modified in the last 24 hours.

    Find Last Modified Specific File Type in Linux:

    You can use a wildcard to limit your search to a specific file type. In the following example, find and newermt are instructed to list all mp4 files modified a day ago.

    cc lang=”bash” width=”100%” height=”100%” escaped=”true” theme=”blackboard”]$ find /home/linuxhint/*.mp4 -newermt “1 day ago” -ls[/cc

    In the following example, find and newermt are used to find all .png images less than 15 days old.

    Finding Last Hour Modified Files in Linux:

    The following example combines the find command with the mmin command. We can use the mmin command to specify minutes. In the example below, the find and mmin commands will print all files under the /root directory, whose modifications are less than 60 minutes old.

    Contrary to the previous example in which files modified in the past 60 minutes were found. You can also use +mmin to search files modified after X minutes. For example, the following command will show files modified 60 minutes ago or more.

    Finding Files Modified on a Specific Date in Linux:

    You can use the ls command to list files including their modification date by adding the -lt flag as shown in the example below. The flag -l is used to format the output as a log. The flag -t is used to list last modified files, newer first.

    Then you can combine ls -lt with grep to print all files which were modified on a specific date.

    Find Last Modified Files Recursively:

    Previous examples are useful to find last modified files

    The command below can be used to print last modified files recursively.

    Search File by Date Omitting Files or Directories:

    Contrary to the previous example, you can search files omitting directories. For this purpose, you need to implement the -type flag with the option f (file) as shown in the following example. As a result, you’ll see only final files and no directories.

    You can also search directories only and the output will omit files. For this, just replace the f with a d after the -type flag.

    Find Files by Access Date:

    You also may want to find unmodified files by access date. For this purpose, you can use the atime command. It is similar to the mtime command explained before, but instead of identifying files by modification, it can display files by access. With this command you can learn the last accessed files and directories in the system.

    The following command shows all files accessed in the past 10 days.

    Like the previous command, you can also use the d option to show only directories:

    If you don’t specify a type, atime will show all files and directories:

    In the following example, find and atime are used to find files and directories with modification older than 20 days.

    As with previous examples, you can also limit the listing to files or directories with the -type flag.

    Conclusion:

    As you can see, Linux offers different methods to find files according to modification time. Any Linux user level can easily learn those methods to search files with a single command. Finding files by modification or access within a system is part of the basic knowledge a Linux user needs.

    I hope this tutorial was useful. Keep following Linux Hint for more Linux tips and tutorials.

    About the author

    David Adams

    David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

    Источник

    Читайте также:  Linux mint восстановление пароля
Оцените статью
Adblock
detector