LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-23-2011, 05:46 PM   #1
thepowerofone
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Rep: Reputation: Disabled
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.

Last edited by thepowerofone; 11-23-2011 at 05:47 PM. Reason: left out a word
 
Old 11-23-2011, 06:15 PM   #2
goodhombre
Member
 
Registered: Mar 2010
Location: Ungheni, Rep. Moldova
Distribution: Ubuntu
Posts: 84

Rep: Reputation: 21
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.
 
1 members found this post helpful.
Old 11-23-2011, 06:24 PM   #3
thepowerofone
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks you so much goodhombre that is such a great help.
 
Old 11-23-2011, 06:28 PM   #4
fingers99
Member
 
Registered: Nov 2011
Location: Liverpool UK
Distribution: Debian, Knoppix, Kanotix
Posts: 63

Rep: Reputation: Disabled
I'm just interested as to why you want to rename .txt to .text?
 
Old 11-23-2011, 06:42 PM   #5
thepowerofone
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
It is just personal preference really as I like the sameness of things, it relates to my OCD.
 
Old 11-24-2011, 09:56 AM   #6
am28
LQ Newbie
 
Registered: Nov 2011
Posts: 3

Rep: Reputation: Disabled
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
 
Old 11-24-2011, 12:43 PM   #7
rahulkya
Member
 
Registered: Feb 2009
Location: New Delhi
Distribution: Linux mint,Ubuntu,Debian,RHEL 5,slackware 13.1, free BSD,solaris.
Posts: 186
Blog Entries: 3

Rep: Reputation: 33
alternate you can use move command too....
Quote:
mv abcd.txt abcd.text
 
Old 11-24-2011, 09:08 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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
 
Old 11-25-2011, 09:47 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by am28 View Post
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
 
1 members found this post helpful.
Old 11-25-2011, 12:47 PM   #10
goodhombre
Member
 
Registered: Mar 2010
Location: Ungheni, Rep. Moldova
Distribution: Ubuntu
Posts: 84

Rep: Reputation: 21
Thanks David the H. .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mkisofs -iso-level 1 converts to lower case instead of upper case. stf92 Slackware 3 10-29-2010 01:32 AM
renaming directories from upper case to lower case, help!! linux_teller Linux - Newbie 3 03-07-2008 06:15 AM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 05:09 PM
Lower case to upper case letter sudhasmyle Programming 1 12-03-2004 05:15 AM
Apache and upper or lower case. dsiguy Linux - General 3 02-04-2003 12:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration