renaming multiple text files in linux

June 8, 2011 at 06:51:00
Specs: Windows XP
Hello - Here is my situation.

I have a tar file and when I extracted it, I got some N number of text files.

This is how it looks after I extracted from the tar file.
latinum_20080101_0305-200272-93930-2-25.txt
latinum_20080101_0310-200279-93934-2-25.txt
latinum_20080101_0315-200671-94112-2-25.txt

and I want to rename them to get the following:
latinum_20080101_0305.txt
latinum_20080101_0310.txt
latinum_20080101_0315.txt

Thanks
Aarthi


See More: renaming multiple text files in linux

Report •

#1
June 8, 2011 at 07:19:18
Using "-" as a delimiter, parse the filename looking for the first field. Change the cp to mv once you are satisfied this works:

#!/bin/bash

for file in latinum*.txt
do
   set -- $(IFS="-"; echo $file)
   cp "$file" "${1}.txt"
done


Report •

#2
June 8, 2011 at 07:40:04
Many thanks for your response. I have made a mistake. the file name structure is like this:

latinum.8300.ram_ips-200272.txt
uranmium.0305.ram_ips-200286.txt
platinum.1800.ram_ips-200256.txt


and I want to rename them to get the following:

latinum.8300.ram_ips.txt
uranium.0305.ram_ips.txt
platinum.1800.ram_ips.txt

is there any single line command(awk, sed) instead writing a shell script?

Thanks
Aarthi



Report •

#3
June 8, 2011 at 08:13:20
The only thing that changes is the pattern:

for file in latinum*.txt

to:

for file in *ram_ips.txt

You might be able to do it with an awk script, but I wouldn't consider it a "one-liner". After parsing the file name, you would have to build a string and use awk's system command to call the external move command. It's not something I want to do.


Report •
Related Solutions


Ask Question