Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Rename files in bash via string handling UNIX
I periodically receive files that are somewhat cryptically named, and I like to rename them to something more understandable. I found this older hint, but it didn't work for my needs -- my renamed files contain spaces. For example, I might receive several files whose names are something like this:
TS_412_96k.fil
TS_413_96k.fil
TS_414_32k.fil
And I want to rename them something like:
Total Entry #412.txt
Total Entry #413.txt
Total Entry #414.txt
After nearly tearing my hair out trying various combinations of awk and sed in my scripts, I discovered that the bash shell has its own string handling functions! This quickly allowed me create the following simple script:
for prog in *.fil; do
    progno=${prog:3:3}
    mv $prog 'Total Entry #'$progno'.txt'
done
Voila! Note that bash starts character numbering at zero rather than one, as do awk and sed. Thus, in the example above, the substring returned by {prog:3:3} would be the numeric values in the original filenames.
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[28,530 views]  

Rename files in bash via string handling | 12 comments | Create New Account
Click here to return to the 'Rename files in bash via string handling' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Rename files in bash via string handling
Authored by: riceran on Sep 07, '06 08:54:27AM
Another approach to this problem is the use of the 'rename' perl script (available at http://www.greenfly.org/rename). With this script, you can simply use a perl regex to systematically rename files. I find it quite handy!

rename 's/TS_(\d+)_.*/Total Entry #$1.txt/' TS*

[ Reply to This | # ]
Rename files in bash via string handling
Authored by: i5ao on Sep 07, '06 10:24:53AM
the hint above replaces by string character position, but you can also use simple pattern matching using slashes: ${variable_name/find/replace}
for f in abc-1.txt abc-2.txt abc-33.txt
do
  echo ${f/abc-/new name -> def_}
done

new name -> def_1.txt
new name -> def_2.txt
new name -> def_33.txt


[ Reply to This | # ]
Rename files in bash via string handling
Authored by: JadeNB on Sep 07, '06 09:11:01AM

As evidenced on the other thread, any discussion like this inevitably devolves into an "OK, but here's how <em>I'd</em> do it discussion" -- to which I'm tempted to contribute, but let's set that aside. I'm really interested to discover that bash has these string-editing features. (I'd alwas thought one had to go to Perl for anything more complicated than =~.) Do you know where to look for documentation?



[ Reply to This | # ]
Rename files in bash via string handling
Authored by: riceran on Sep 07, '06 09:22:39AM

In spite of my comment above, I do regularly use the bash string editing features for other purposes. The resource that I used to learn about these was the O'Reilly book on bash. Of course, that requires the purchase of a book, which is not ideal...and likely not necessary. I'm sure that you can google good web resources.



[ Reply to This | # ]
Rename files in bash via string handling
Authored by: barefootguru on Sep 07, '06 02:42:02PM
I can highly recommend the Advanced Bash-Scripting Guide, which is free, and available at http://www.die.net/doc/linux/abs-guide/.

[ Reply to This | # ]
Rename files in bash via string handling
Authored by: -dl- on Sep 07, '06 09:30:18AM

man bash



[ Reply to This | # ]
Rename files in bash via string handling
Authored by: lihtox on Sep 07, '06 12:31:24PM
man bash

Specifically, look for "Parameter Expansion" on the bash man page. (And while you're at it, it's fun to read through the whole man page, to see what else you don't know about!)

[ Reply to This | # ]

Rename files in bash via string handling
Authored by: MordEth on Sep 07, '06 10:31:46AM

Another thing you may wish to look into is using sed. I do a lot of quick renames this way, for example:

for i in *.mp3; do mv $i `sed -e "s/^[01][0-9]_/artist--/g"`; done

This would convert files that begin with "##_" (where ## is between 00 and 19) to start with "artist--" instead.

Here is a useful and concise guide to regular expressions that I've recommended to other people in the past (which could be used with sed, awk, grep, perl, etc.):

http://etext.lib.virginia.edu/services/helpsheets/unix/regex.html



[ Reply to This | # ]
learn perl
Authored by: SOX on Sep 07, '06 10:15:57AM

If you find yourself tearing your hair out with awk, sed and bash, then just learn perl. That's exactly what it was built for. exactly its original intent: a seemles blend of awk,sed,grep, and bash.



[ Reply to This | # ]
Documentation for bash string manipulation
Authored by: kd4ttc on Sep 07, '06 02:41:57PM
A good reference for bash is the linus documentation project. Look at http://www.tldp.org/LDP/abs/html/string-manipulation.html, which is a subsection of a very good bash tutorial/reference. The bash manual, available as man bash is an authoritative reference, but not a tutorial. It is also 60 pages long. I find it useful to enter
man -t bash | pstopdf -i -o bash.pdf; open bash.pdf
to get a nicely formatted pdf of the bash manual and open it as soon as the pstopdf utility completes. Later, one can just open the bash.pdf file which is saved in your home directory.

---
Steve Holland

[ Reply to This | # ]

Rename files in bash via string handling
Authored by: neffs on Sep 07, '06 03:59:44PM
i would recommend file list from peter maurer (the developer of butler)
http://www.petermaurer.de/nasi.php?section=filelist
it does all kinds of renaming, you may want to use the advanced setting

you can even create droplets

[ Reply to This | # ]
Rename files in bash via string handling
Authored by: sanandak on Sep 08, '06 05:52:04AM

Here is a perl script that allows you to use ANY perl expression that converts one string to another - be careful!

% rename.pl 's/xyz/abc/' *

## strip .orig from filename
% rename.pl 's/.orig$//' *.orig

##make lc, except Makefile
% rename.pl 'tr/A-Z/a-z unless /^Make/' *

-----save this as rename.pl in your path, and chmod +x rename.pl to
-----make executable
<code>
#!/usr/bin/perl -w
# From T. Christiansen and N Torkington,
# The Perl Cookbook, O'Reilly, 1998
# Usage: rename.pl perlexpr [files]

($op = shift) || die "Usage: rename.pl perlexpr [filenames]\n";
if(!@ARGV) {
@ARGV=<STDIN>;
chop(@ARGV);
}
for (@ARGV) {
$was=$_;
eval $op;
die $@ if $@;
# print "$was ==> $_\n";
rename($was,$_) unless $was eq $_;
}
</code>
Sincerely,

Sridhar.



[ Reply to This | # ]