How do I write a shell script that reads all the filenames in a directory and finds a specific string in the filenames?

I need a shell script to find a line in a file like the following: FileName_1.00_r0102.tar.gz And then select the maximum value from multiple occurrences.

I'm interested in the "1.00" part of the filename. I can get this part separately in UNIX shell using commands:

find /directory/*.tar.gz | cut -f2 -d'_' | cut -f1 -d'.'
1
2
3
1
find /directory/*.tar.gz | cut -f2 -d'_' | cut -f2 -d'.'
00
02
05
00

      

The problem is that there are multiple files with this line:

FileName_1.01_r0102.tar.gz

FileName_2.02_r0102.tar.gz

FileName_3.05_r0102.tar.gz

FileName_1.00_r0102.tar.gz

I need to select a file with FileName _ ("highest value") _ r0102.tar.gz

But since I'm new to shell scripting, I can't figure out how to handle these multiple instances in a script.

The script I only used for the integer part looks like this:

#!/bin/bash
for file in /directory/*
file_version = find /directory/*.tar.gz | cut -f2 -d'_' | cut -f1 -d'.'
done
OUTPUT: file_version:command not found

      

Kindly help. Thank!

+3


source to share


4 answers


If you only want the latest version number:

cd /path/to/files
printf '%s\n' *r0102.tar.gz | cut -d_ -f2 |  sort -n -t. -k1,2 |tail -n1

      



If you want the filename:

cd /path/to/files
lastest=$(printf '%s\n' *r0102.tar.gz | cut -d_ -f2 |  sort -n -t. -k1,2 |tail -n1)
printf '%s\n' *${lastest}_r0102.tar.gz

      

+2


source


You can try the following, which finds all matching files, sorts the filenames, takes the last one in this list, and extracts the version from the filename.



#!/bin/bash
file_version=$(find ./directory -name "FileName*r0102.tar.gz" | sort | tail -n1 | sed -r 's/.*_(.+)_.*/\1/g')
echo ${file_version}

      

+2


source


It is not necessary to parse the version number of the file name before finding the actual file name. Use GNU ls

-v

(natural look (version) in text):

ls -v FileName_[0-9.]*_r0102.tar.gz | tail -1

      

+1


source


I have tried and it is worth working below the script line you need.

echo `ls ./*.tar.gz | sort | sed  -n /[0-9]\.[0-9][0-9]/p|tail -n 1`;

      

+1


source







All Articles