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!
source to share
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
source to share
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}
source to share