-1

I would like to have in a variable the most recent file with a specific format.

exemple : in /home/test, 5 files :

  • file_test_hadoop_20181130.csv (This is the last modified one for hadoop file)
  • file_test_hadoop_20181130.txt
  • file_test_hadoop_20181130.ini

  • file_test_hub_20181130.txt (This is the last modified one for txt file)

  • file_test_hub_20181130.csv
  • file_test_hub_20181130.ini

So the result that i want is the last modified one in each type : HADOOP_NAME=file_test_hadoop_20181130.csv
HUB_NAME=file_test_hub_20181130.txt

So i started to do something like this :

HADOOP_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hadoop*" -printf '%f\n')

HUB_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hub*" -printf '%f\n')

But i get all files.

0

So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:

[haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
file_test_hadoop_20181130.csv

[haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
file_test_hub_20181130.txt
0

Tried with below command and it worked fine

ls -ltrh /home/test/file_test_hadoop*| tail -n1
ls -ltrh /home/test/file_test_hub* | tail -n1
0

Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:

cd /home/test
hadoop_files=(file_test_hadoop*)
HADOOP_NAME=${hadoop_files[-1]}
unset hadoop_files
hub_files=(file_test_hub*)
HUB_NAME=${hub_files[-1]}
unset hub_files
cd -

This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).

0

Your first command

ls -tr 

, correctly lists the files by time

When you pipe that to

tail -n1

It correctly selects the last entry, the most recent file

Problem is it may or may not be matching your file pattern

Of course, your last command simply does not receive any input from the previous ones and so it runs simply as it ought to and it lists all the files

find /home/test -maxdepth 1 -name "file_test_hadoop*" -printf '%f\n'

To solve this, I would first begin with ls and grep to match the correct pattern, i.e

ls -tr | grep "file_test_hadoop" > File_List.txt

This would create a file with a list and latest file being at the most bottom,

tail -n1 File_List.txt

So your code would be HADOOP_NAME="$(tail -n1 File_List.txt)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.