HOWTO: Strip out date/time portion from a file name in Unix

Hi, Experts:

From file name: File1_YYYYMMDD.txt to file name: File1.txt
How do I strip out anything after _ and before file ext. so, file name ends up without date stamp on a Linux server

I have files with date and time stamps also, so key part is only to leave file name with extension.

Pl. suggest with actual code in Linux.
Thanks,
abubelalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
This works also for e.g. "File1_abc99.txt" or "Fi le1_abc123.txt" etc.:

FILE="File1_20150506.txt"
FILE="${FILE/_[0-9a-zA-Z]*\./.}"
echo $FILE

If it's just for a numeric timestamp:

FILE="File1_20150506.txt"
FILE="${FILE/_[0-9]*\./.}"
echo $FILE
0
abubelalAuthor Commented:
Thanks!

Will this format work too in posted note.
File1_YYYYMMDD_HHMM.csv should turn into:

File1.csv ?
0
woolmilkporcCommented:
Both versions will work,  but the second one only if there are just numerics and underscores and nothing else:

FILE="File1_20150506_1855.csv"
FILE="${FILE/_[0-9a-zA-Z]*\./.}"
echo $FILE

FILE="File1_20150506_1855.csv"
FILE="${FILE/_[0-9]*\./.}"
echo $FILE
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

abubelalAuthor Commented:
Thank you. I will test on my end and get back.
0
abubelalAuthor Commented:
Hi,
Looks like we cannot rename file (requirement); can you pl. drop me the commands to find and copy:

Find file:
File_20150506.txt and copy as:
File.txt

Same with date/time version:
Find file:
File_20150506_1735.txt  and copy as:
FIle.txt
0
abubelalAuthor Commented:
Sorry, find by patter matching ... (basically static file name part is known)

_YYYYMMDD
_YYYYMMDD_HHMM
0
woolmilkporcCommented:
With "find", do you mean using the Unix/Linux "find" command?

By which criteria should we search the files(s)? By the known static part, or by the ever changing date/time part?
0
woolmilkporcCommented:
The second part (copy as ...) would look like this:

FILE="File1_20150506_1855.csv"
cp /old/path/"$FILE" /new/path/"${FILE/_[0-9]*\./.}"
0
abubelalAuthor Commented:
Search by Static part and copy without ever changing date or date/time parts

Pl. advice on both versions (date and date/time).
Thx
0
woolmilkporcCommented:
Let's assume the static part is "File1",  immediately followed by the dynamic parts  "_YYYYMMDD" or "_YYYYMMDD_HHMM" immediately followed by a static file extension ".xxx" with "xxx" being any arbitrary value.

Now there's a  problem:

Given two files "File1_20150506_1855.csv" and "File1_20150507_1855.csv". Both files would have to be copied as "File1.csv" thus the second new file will overwrite the first one!

How to handle this?

Assuming the above will never happen we would continue like this:

Given the directory where to start the search as your current directory, and given the directory where to copy the file with shortened names as "/new/dir":

find . -name "File1_*.*" | while read FILE; do cp $FILE /new/dir/${FILE/_[0-9]*\./.}; done
0
abubelalAuthor Commented:
Thank you. Your assumption is correct.
Was above tested on your end for both versions (date and date/time).
Currenlty I do not have access to a LX box...
0
woolmilkporcCommented:
My solutions are tested for both formats and do what they're supposed to.

But again: Renaming "File1_20150506_1855.csv" and "File1_20150507_1855.csv" the suggested way would result in both files becoming "File1.csv" Attention!
0
abubelalAuthor Commented:
Thank you. I will get back.
0
abubelalAuthor Commented:
Hi,
 
Req has updated a little bit. It is now as:

Find most recent file by static known part and copy that as:

 Find file:
 File1_20150512.txt and copy/ create a new file as:
 File1txt

 Same with date/time version:
 Find file:
 File2_20150512_0752.txt  and copy/create a new file as:
 FIle2.txt

Appreiate providing revised command; please also test.
Thanks,
0
ozoCommented:
Which what part is the static known part?
0
abubelalAuthor Commented:
File name is known static part; basically we have files come to us with date and date/time stamps. And before we load them we need to remove date and date/time from file names and leave with file name only. We search for most recent, in case there would be more than one file (with same static part with dynamic date, date/time) sitting in the directory.
0
woolmilkporcCommented:
If "most recent" means "newest by file age" the this will do:


File1 (date version):

cd /dir/with/files
FILE=$(ls -rt File1_* | tail -1)
cp $FILE /new/dir/${FILE/_[0-9]*\./.}


File2 (date/time version):

cd /dir/with/files
FILE=$(ls -rt File2_* | tail -1)
cp $FILE /new/dir/${FILE/_[0-9]*\./.}
0
ozoCommented:
file=`ls -t File1*.txt | head -1`
cp $file ${file/_*./.}
0
woolmilkporcCommented:
OK, and if the newest file shall be found by the date or date/time string contained in its name instead of the age of the file as stored in its inode:

File1 (date version):

cd /dir/with/files
FILE=$(ls -rt File1_* |sort -k 2,2 -t"_" | tail -1)
cp $FILE /new/dir/${FILE/_[0-9]*\./.}


File2 (date/time version):

cd /dir/with/files
FILE=$(ls -rt File2_* | sort -k 2,2 -k 3,3 -t"_" | tail -1)
cp $FILE /new/dir/${FILE/_[0-9]*\./.}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abubelalAuthor Commented:
Thanks; what commands would be on reverse side, i.e. static files are placed and we need to suffix them by __YYYYMMDD.txt  and _YYMMDD_HHMM.txt as new files in same dir ?
0
abubelalAuthor Commented:
... Take date, date/time from system.
0
abubelalAuthor Commented:
Please test them out, as I do not currently have access to test.
0
woolmilkporcCommented:
Your new requirement is qute different from the old one, so please ask a new question.
0
abubelalAuthor Commented:
Done; ID: 28671995
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.