LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   File extension and upper and lower case in .text files (https://www.linuxquestions.org/questions/linux-newbie-8/file-extension-and-upper-and-lower-case-in-text-files-915191/)

thepowerofone 11-23-2011 04:46 PM

File extension and upper and lower case in .text files
 
Hello there,

I am very new to Linux and I would like to know how do I change file extensions from .txt to .text.

Once I have done that I would like to change all uppercase to lowercase in all files that have a .text extension.

If anyone could help I would be really grateful and it is great to find such a helpful site like this.

goodhombre 11-23-2011 05:15 PM

Hi,

For files renaming you cat use rename command .
Code:

rename .txt .text *.txt
To change to lowercase please read this post .

You can use find command to change and rename all files content .
Ex :

Code:

find . -name *.text exec sed -i 's/\(.*\)/\L\1/' {} \;
Check rename, find and sed manual pages for more details.

thepowerofone 11-23-2011 05:24 PM

Thanks you so much goodhombre that is such a great help.

fingers99 11-23-2011 05:28 PM

I'm just interested as to why you want to rename .txt to .text?

thepowerofone 11-23-2011 05:42 PM

It is just personal preference really as I like the sameness of things, it relates to my OCD.

am28 11-24-2011 08:56 AM

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

rahulkya 11-24-2011 11:43 AM

alternate you can use move command too....
Quote:

mv abcd.txt abcd.text

chrism01 11-24-2011 08:08 PM

Quote:

Once I have done that I would like to change all uppercase to lowercase in all files that have a .text extension.
Is that change case of filenames or file contents ?
Incidentally, see tr http://linux.die.net/man/1/tr

David the H. 11-25-2011 08:47 AM

Quote:

Originally Posted by am28 (Post 4532806)
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

goodhombre 11-25-2011 11:47 AM

Thanks David the H. .


All times are GMT -5. The time now is 06:08 PM.