[ale] stripping filename extension in bash
aaron
aaron at pd.org
Sun Dec 1 23:30:44 EST 2002
On Saturday 30 November 2002 15:45, Christopher Bergeron wrote:
> Does anyone know how I can strip the extension from a filename in a
> bash script?
Given the diversity of programs and programming choices on Linux there
are probably a hundred ways to do this, and several solutions seem to
have already been offered.
Still,I figured I'd attach my solution since It happened that, about the
time this question appeared on the list, I was trying to improve my
understanding of bash scripting by writing a "user friendly" program that
would do this kind of batch tag / file sequence renaming as a button for
the Worker file manager. I also wanted to adapt it to work as a stand
alone shell script, so my fiddling is attached as the "pattern_mv"
script. I'm certain my solution is much more involved than it needs to
be, but for now I can blame that on wanting to explore various bash
functions and operations.
The script will rename file sequences or groups of files which share a
common string, like ".tags".
USAGE: pattern_mv "*File_Pattern*" "*Replace_String*" [/Path/]
- All File and Link names in /Path which match the
File_Pattern are renamed.
- Script works with most all filenames, including those containing
spaces and unusual punctuation characters (with the exception
of file names containing asterisks).
- QUOTED "File_Pattern" and "Replace_String" arguments are required
and *File_Pattern* must have "*" wild card(s) at tail*, *head or *both*.
- The "/Path" arg is optional; default path is current directory (pwd).
- A File_Pattern of a single asterisk "*" will match all files in /Path
- A Replace_String of a single asterisk "*" is interpreted as an
empty string and effectively removes the File_Pattern string
from matching file names.
- User Confirmation of settings is offered before any renaming (mv)
is performed; Permissions, File Type and Existing File are
checked before mv is attempted. Each processed File name
is echoed to standard out.
EXAMPLES:
- To rename a file sequence in the current directory from
001 fred jpg ; 002 fred jpg ... to
001-joe.jpg ; 002-joe.jpg ... :
% pattern_mv "* fred *" "-joe."
- Note: Asterisk *references in Replace_String are optional
when File_Pattern includes a string.
- To add the tag ".txt" to all files in the /tmp/textfiles directory:
% pattern_mv "*" "*.txt" /tmp/textfiles
- Note: The lone *asterisk File_Pattern matches all files in
/tmp/textfiles. The Replace_String *asterisk then becomes the
reference for placing the tag at the tail or head of the file name.
- To remove the tag ".bmp" from all files in the
/home/me/bitmaps directory:
% pattern_mv "*.bmp" "*" /home/me/bitmaps/
- Note: The lone asterisk as Replace_String is the format for an
empty replacement string.
============================
Critiques of the program are welcomed.
thanks!
peace
aaron
PS.BTW --
The Advanced Bash Scripting Guide found at
http://www.tldp.org/LDP/abs/html/
was extremely informative, helpful and quite often invaluable
for grepping the more cryptic aspects of shell scripting.
A really nice little bash reference.
pattern_mv
More information about the Ale
mailing list