Linux – Listing Files Based on Date

 

Background

I have a need to list files on a Linux Host sorted by date.

 

ls Command

Earliest First

Command


ls -ltr -G -g | grep -v '^total' | head -5

 

Output

 

Latest First

Command


ls -lt -G -g | grep -v '^total' | head -5

Output

 

ls Command In a ditch

Unfortunately, when there are numerous files in the target folder the ls command and other file utilities can get in a ditch.

Argument List Too Long

Command


ls *.LOG

Output

Image

Text

bash: /bin/ls: Argument list too long

 

Find Command

Earliest First

Command


find . -name '*.LOG' | sort | head -n 5

Output

Command


find . -name '*.LOG' | sort | tail -n 5

Output

 

Script

Let us get a bit more complicated.

And, we will do do by picking up individual files, process them, and exit after N Number of files have been processed.


# get current folder
echo "Current working folder is $PWD"


#echo list all files in current log
#find . -name "*.LOG" | xargs -i echo " {} "


declare -i iFileID
declare -i iFileIDMax
declare FORMAT_FILE_PROCESSING

iFileID=1
iFileIDMax=1000
iFileIDMax=50
FORMAT_FILE_PROCESSING="%d  file %s \n"

#echo $FORMAT_FILE_PROCESSING

#list earliest top N LOG files
find . -name "*.LOG" 2> /dev/null  | sort | head -n $iFileIDMax  | while  IFS="" read name;
do



     printf "$FORMAT_FILE_PROCESSING" $iFileID  $name

     iFileID=$((iFileID+1));

     if [ $iFileID  -gt  $iFileIDMax ]
      then


        break;

     fi


done



 

 

Dedicated

Dedicated to ….

The Electronic Toolbox
Argument list too long when copying/deleting/moving files on Linux
Link

Summary

When needing to list files by date, one can use the ls command,

In cases where there are numerous files, the type of filtering one can do is hampered by the fact that the ls command tries to accept all the files as a batch.

A good workaround is the find command.

 

Next Up

The error that I really need to fix was the one from running “gzip S066*

Image

Textual

bash: /usr/bin/gzip: Argument list too long

 

I know it has something to do with 666

 

Reference

  1. Error – Argument list too long …
    • The Electronic Toolbox
      • Argument list too long
        Link
  2. ls command
    • TheGeekStuff.com
      • Linux ls command examples
        Linux
    •  linuxcommand.org
  3. Find Command
    • Unix.com
      • While loop, input from find command
        Link
    • StackOverflow.com
      • Display modified datetime with Find Command
        Link
    • unix.stackexchange.com
      • Only find first few matched files using find
        Link
    • Superuser.com
      • How can I find the oldest file in a directory tree
        Link
  4. Loop Control
    • The Linux Documentation Project ( tldp.org )
    • LinuxCommand.org
      • Flow Control – Part 3
        Link
  5. If Command
    • TheGeekStuff.com
      • Bash If Statement Examples ( If then fi, If then else fi, If elif else fi, Nested if )
        Link
  6. Math
    • K-State Polythecnic
      • Math in Shell Script
        Link
  7.  printf
    • Linuxconfig.org
      • Bash printf basic commands
        Link
    • Bash Hackers wiki
      • The printf command
        Link
    • Unix.StackExchange.org
      • Printf formatting with variable format – what does this var reference?
        Link
  8. variables
    • Ryan

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s