LinuxQuestions.org
Latest LQ Deal: Linux Power User 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 04-02-2015, 04:25 PM   #1
sramani
LQ Newbie
 
Registered: Apr 2015
Posts: 1

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


hello,

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


#!bin/bash
//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

done

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

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

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

Code:
man mv
 
Old 04-02-2015, 10:04 PM   #4
Gerard.M.Frey
LQ Newbie
 
Registered: Mar 2015
Posts: 12

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

Code:
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.
 
  


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

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

All times are GMT -5. The time now is 01:04 AM.

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