Quote:
Originally Posted by am28
Hi goodhombre would you mind explain the code
-name *.text exec sed -i 's/\(.*\)/\L\1/' {} \;
as im only new at linux and i don't understand what im trying to do
|
The
find command is used to, well, find files, and optionally perform various operations on them. In this case it searches for filenames ending in ".text" and then executes the
sed command on each one it finds. And the sed expression given above simply changes the input text to lowercase.
...Or at least it would, were it not for a few problems. First of all, the "find" command name has clearly been cut off from the beginning. Second,
exec should be
-exec. Third, "
*.text" needs to be quoted, because "
*" indicates a shell globbing pattern that will expand to a list of all matching files in the current directory if not protected.
find is also recursive into subdirectories by default, which may or may not be desirable.
But most importantly,
sed, particularly with the
-i option, modifies the
contents of files, so all this command would do is make all the text in every matched file lowercase! While I admit that the wording of the initial request could be interpreted this way, I highly doubt it's what he really wants. Most likely he was referring to lowercasing the file
names.
Here are a couple of links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html
Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
Now to give
my answer to the OP. First of all, the batch renaming of files is a question that has been asked and answered repeatedly. There are hundreds of threads here and on the web dealing with it, as well as half-a-dozen programs specifically designed just for the task. A quick search or two would have likely answered the question before it was even asked.
But at the very core of the problem, all you really need is
a loop, some
parameter substitution, and the
mv command.
Code:
for name in *.txt; do
newname=${name%.*}
newname=${newname,,}.text
mv "$name" "$newname"
done
Note that as it stands the above only works correctly on files in the current directory. To safely handle longer path names you'd have to modify it to split the name and path first, so that it won't try to modify the names of the containing directories. Again, this has been covered many times before.
See here for more on renaming files:
http://mywiki.wooledge.org/BashFAQ/030