NO sed or awk necessary. READ ON............
You can do mass file renaming in bash using a for loop and substitution.
This is one of the best articles I've ever found on the topic, it's from
1996 and it's as relevant today as it was then.
http://linuxgazette.net/issue18/bash.html
Read the example and information in that article on how to copy all
*.txt files to *.bak. It explains substitution, the for loop, etc. This
article has proven invaluable over the years!!
Renaming files involving your own suffixes and prefixes are the simplest
and can be done in a single commandline using a for loop and
substitution. (see the article above for examples and good background info).
When I needed to replace the middle of a filename with a different
string, I came up with the following simple one line solution. This
script renames all the files in a folder that contain '&' in the
filename to the word 'and' in its place. I call this Bash script,
rename-amp-to-and.sh:
#!/bin/bash
for i in *\&*; do /bin/mv -iv "$i" "${i/\&/and}"; done
WARNING! TEST BEFORE EXECUTING CODE LIKE THE LINE ABOVE!!
1. When creating these one-liners, test with the echo command before you
stick in cp or mv and let the code make real changes!!
2. Use the cp command and see how that works BEFORE you go with the mv
command. Alternatively, back up all the files to another folder before
running your script on them.
You can run the for-loop line on the commandline, but it is better to
save it in a Bash script file (plain text file) and give it execution
privileges (chmod 755 rename-amp-to-and.sh). 755 gives ONLY the owner
of the file execution privileges vs. chmod +x which gives every user on
the machine execution privileges. You probably don't want other users to
be running your scripts. ;)
Make sure your script includes the line to run it in its own Bash shell:
#!/bin/bash
TIP: Some books will tell you to run: #!/bin/sh but you're trusting
that the shell is Bash not something else, particularly when you're at
an unfamiliar machine or a UNIX box (Sun or HP), which don't use Bash by
default and therefore may or may not run your script correctly, if at all!!
To run it at a console prompt, enter:
./rename-amp-to-and.sh
NOTE: the & is escaped with a backslash. Using the substitute syntax
common to regular expressions, it scans the variable 'i' for '&' and
replaces it with 'and'.
Syntax is: var-to-search/search text/replacement text/
For proper shell expansion/interpretation it's enclosed in ${}, as:
"${var/search text/replacement text}"
The quotes are a necessary safeguard for things like spaces, special
chars, etc.
Scott Vargovich wrote:
I know that mass file renaming can be done with sed and awk, but I
have no clue what the syntax is for it. Help anybody???
On Sat, Feb 20, 2010 at 11:16 AM, Robert Citek <[email protected]
<mailto:[email protected]>> wrote:
On Sat, Feb 20, 2010 at 8:59 AM, Dos-Man 64 <[email protected]
<mailto:[email protected]>> wrote:
> Thanks, I thought I read somewhere that either Unix or Linux files
> can't have spaces in the names, but it's one of those things
where you
> can't remember what you read or where you read it.
Files definitely can have spaces and other characters:
$ touch foo bar foo\ bar foo$'\n'bar foo$'\t'bar \
foo$'\b'bar f...@bar foo:bar foo*bar
$ ls -1 --show-control-chars
bar
foo
foo bar
foo:bar
f...@bar
foo*bar
fobar
foo bar
foo
bar
$ ls -1b
bar
foo
foo\ bar
foo:bar
f...@bar
foo*bar
foo\bbar
foo\tbar
foo\nbar
A common topic on linux forums tends to be how to delete or rename
files with these special characters.
Regards,
- Robert
--
You received this message because you are subscribed to the Linux
Users Group.
To post a message, send email to [email protected]
<mailto:[email protected]>
To unsubscribe, send email to
[email protected]
<mailto:[email protected]>
For more options, visit our group at
http://groups.google.com/group/linuxusersgroup
--
<>< Scott Vargovich <><
------------------------------------------
OpenPGP Key ID: F8F5DC7E
------------------------------------------
--
You received this message because you are subscribed to the Linux
Users Group.
To post a message, send email to [email protected]
To unsubscribe, send email to [email protected]
For more options, visit our group at
http://groups.google.com/group/linuxusersgroup
--
You received this message because you are subscribed to the Linux Users Group.
To post a message, send email to [email protected]
To unsubscribe, send email to [email protected]
For more options, visit our group at
http://groups.google.com/group/linuxusersgroup