Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-02-2015, 05:25 PM   #1
LQ Newbie
Registered: Apr 2015
Posts: 1

Rep: Reputation: Disabled
Question how to rename a file in linux


I am trying to rename the file by adding .txt extension and also
before renaming, I want to replace . in file with _

right now file looks like this mdm.201504021628

after execution of my script file name should be mdm_201504021628.txt

//reading all files from directory

files=$(hadoop fs

-ls /dl/data/landing/hivedb/lnd_attunity_kpi_db_backup/auth_master |
awk '!/^d/ {print $8}')

for f in $files; do

//using sed to replace . with _ and then feeding to hadoop fs command

sed 's/./\_/g' $f | hadoop fs -mv $f $f.txt


Thanks for your help in advance.
Old 04-02-2015, 05:27 PM   #2
Keith Hedger
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,499

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
Use the rename command, it will rename a file based on a template, check the manpage for details.
Old 04-02-2015, 10:03 PM   #3
LQ Guru
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Debian, Mageia, and whatever VMs I happen to be playing with
Posts: 12,990
Blog Entries: 18

Rep: Reputation: 3396Reputation: 3396Reputation: 3396Reputation: 3396Reputation: 3396Reputation: 3396Reputation: 3396Reputation: 3396Reputation: 3396Reputation: 3396Reputation: 3396
In routine cases, the mv (move) command will do the job. See

man mv
Old 04-02-2015, 11:04 PM   #4
LQ Newbie
Registered: Mar 2015
Posts: 12

Rep: Reputation: Disabled
If you just want to rename files, you can do something simple like this

for i in $files; do echo mv "$i" "$(echo $files | sed 's/\./_/g' ).txt" ; done
The echo command will print the output of the for loop script without affecting your files, it's sortof a preview of what will happen.

If the for loop is giving you the results you are asking, run the script again without the echo part.


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
Shell script to get name of file, delete original file, rename blank file chrisgti Linux - General 11 09-15-2012 03:49 AM
[SOLVED] Rename a file in batch of directories in linux ppat18 Linux - Newbie 6 11-17-2011 04:09 PM
Can I rename a system file from my Linux amer_58 Linux - Newbie 4 06-07-2005 08:42 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:56 AM.

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