PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 19, 2022

[FIXED] How to sort a list of different filenames sorting a number in the filename with bash to find the "newest" filename in bash?

 November 19, 2022     bash, macos, scripting     No comments   

Issue

I run a command, getFiles, that outputs a newline delimited list filenames. Very similar to the output of ls -1. The filenames all end in a number representing a kind of timestamp. Larger numbers are more recent. The filenames follow no pattern. e.g. file1234, other44234, something34142, carrot123.

I need to find the filename with the largest number (numerically). In this example other44234.

After finding the filename I need to pass it as an argument into another command. i.e. doItToIt "$THE_FILE"


Solution

Another way:

$ sed -E 's/^([a-Z]+)([0-9]+)/\1 \2/' files.txt |
    sort -n -k2 |
    tail -n1 |
    tr -d ' '

other44234


Answered By - Gilles Quenot
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing