Replacing extension of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing extension of files
# 1  
Old 02-05-2014
Replacing extension of files

hi,

i am having a certain files in a folder where i need to replace the extension with similar file name

for eg
Code:
1.csv
2.csv
3.csv
4.csv

i need to replace the extension
Code:
1.csv
1.txt
2.csv
2.txt
3.csv
3.txt
4.csv
4.txt

the text file will be 0 byte file but csv file will have data in each files
Code:
for i in *.csv
do
touch $i
done

# 2  
Old 02-05-2014
Use cp instead of touch with parameter substitution:
Code:
for i in *.csv
do
        cp "$i" "${i/\.csv/\.txt}"
done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-05-2014
I think it must be simply . not \.
To produce 0 length files:
Code:
for i in *.csv
do
  touch "${i/.csv/.txt}"
done

Or
Code:
for i in *.csv
do
  touch "${i%.*}.txt"
done

Old Unix shells need an external program basename
Code:
for i in *.csv
do
  touch `basename "$i" .csv`.txt
done

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 02-05-2014
basename internally is "${path##*/}" for bash and ksh.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

2. Shell Programming and Scripting

Python - glob () - How to grep same files with different extension files

Hi I Have a directory and i have some files below abc.txt abc.gif gtee.txt ghod.pid umni.log unmi.tar How can use glob function to grep abc files , i have created a variable "text" and i assigned value as "abc", please suggest me how can we use glob.glob( ) to get the output as below... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

3. Shell Programming and Scripting

Moving files extension one by one

Hi, I'm trying to do a script that move .Gz extension (one by one) in a new repertory called old-logs and then copy what's inside old-logs to a new.log but adding a date like this (something. gz: 2012:12:09). thanks in advance for your answers. (5 Replies)
Discussion started by: Froob
5 Replies

4. UNIX for Dummies Questions & Answers

How to list files with no extension together with *.prog files?

Hi, I know that to list files with no extension, we can use.. ls -1 | grep -v "\." And to list .prog files, we can use.. ls -1 *.prog or ls -1 | grep '.prog$' (4 Replies)
Discussion started by: adshocker
4 Replies

5. UNIX for Dummies Questions & Answers

copying files of certain extension?

Hi, I am using scp to copy a certain directory over the network. This folder contain some files that I am not interested in. My question is; is it possible to copy files of certain extension only, keeping the same directory hierarchy as it is (that is sub-folders)? Thanks (8 Replies)
Discussion started by: faizlo
8 Replies

6. Shell Programming and Scripting

Reading extension of files

Hi, I need a command to read extension of files. Could anyone please help me? (14 Replies)
Discussion started by: priyadarshini
14 Replies

7. HP-UX

Files without extension

I am brand new to hp unix systems. I see some files without extension on this system. If I type name of the file it shows me so many detail but does not take me back to command prompt. What are these files and how do I come back to command prompt? Please help (1 Reply)
Discussion started by: rajahindustani
1 Replies

8. Shell Programming and Scripting

How to get files with a specific extension

Hi All, How do I get only the files with the .csv extension. I have a shell script something like below: #!/usr/bin/ #Directory to scan for files SCANDIR="/cmb/data/exstream/scriptslogs/"; LOGFILE="/cmb/data/exstream/scriptslogs/test.log"; cd $SCANDIR for FILE in * ; do FILENAME=$FILE... (9 Replies)
Discussion started by: janavenki
9 Replies

9. UNIX for Dummies Questions & Answers

How to test for files with a particular extension.

Hi!!, Is there any way on Ksh to test the files having a particular Extension? My program looks something like: for i in *$1* do if "$i" != files with ext of .Z then compress -f $i fi done Any suggestions??? :):) (1 Reply)
Discussion started by: jyotipg
1 Replies

10. UNIX for Dummies Questions & Answers

How to list files will no extension

Can some one help ...... I want to list files with no extension in directory. I have tried the following ls * That listed all files ls *.* That listed files with extension ls That listed all files Thanks (5 Replies)
Discussion started by: Wing m. Cheng
5 Replies
Login or Register to Ask a Question