LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to rename a file in linux (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-rename-a-file-in-linux-4175538581/)

sramani 04-02-2015 04:25 PM

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.

Keith Hedger 04-02-2015 04:27 PM

Use the rename command, it will rename a file based on a template, check the manpage for details.

frankbell 04-02-2015 09:03 PM

In routine cases, the mv (move) command will do the job. See

Code:

man mv

Gerard.M.Frey 04-02-2015 10:04 PM

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.


All times are GMT -5. The time now is 03:00 AM.