How do I rename a filename in a directory?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I rename a filename in a directory?
# 1  
Old 02-10-2010
How do I rename a file extensionin a directory?

Hi,

I've got a large to task to do, which I've broken into three section. I'm just stuck on one of the sections.

I have to change the end of a filename from .txt to .doc in a directory. So if I have a directory called "folder1" and two files contained in it called "file1.txt" and "file2.txt", and I specify -r, it changes the end of the filename to "file1.doc" and "file2.doc".

I already know how to change a filename (should I post my code?), but I don't know how to do it in a directory. How do I go about this?

p.s. - I did a search and couldn't find anything sufficient.

Last edited by TeddyP; 02-10-2010 at 01:48 PM..
TeddyP
# 2  
Old 02-10-2010
Strange, a search for "change file extension" turns up lots of possible solutions. Like this one. And it even works if you're using an absolute path with the file.
# 3  
Old 02-10-2010
I already know how to change the file extension, I just don't know how to do it in a directory.

Plus I have to signify it by entering -r.

Should that be signified by entering it like this, "sh ./name-of-script.sh > -r"?

EDIT: Should I post the code I've got so far?
TeddyP
# 4  
Old 02-10-2010
Code:
for i in folder1/*.txt;do echo mv "${i}" "${i%*.txt}.doc";done

If that's what you want remove the echo Smilie
# 5  
Old 02-10-2010
I don't exactly know what you are looking for. You can "cd" into folder1 and rename the files. If you're looking to rename files in a directory without changing to that directory, here's a for loop:

Code:
for i in `find ./folder1 -type f -name '*.txt'`; 
do 
j=`echo $i | sed -e 's/\.txt$/\.doc/'`;
mv $i $j; 
done


If you're asking about renaming a directory, use the "mv" command.

Last edited by pludi; 02-10-2010 at 02:01 PM.. Reason: code tags, please...
# 6  
Old 02-10-2010
Quote:
Originally Posted by TeddyP
Plus I have to signify it by entering -r.
So it's homework, right?
# 7  
Old 02-10-2010
Quote:
Originally Posted by danmero
Code:
for i in folder1/*.txt;do echo mv "${i}" "${i%*.txt}.doc";done

If that's what you want remove the echo Smilie
Thanks for the help. I've already tried that and it works, but I need to run that command by signalling -r. Here is the if statement I've already created in my current program:

Code:
rec=2

if [ $1 = "-r" ]
then
    rec=1
fi

if [ $1 = "-n" ]
then
    rec=0
fi

if [ $rec = "2" ]
then
    rec=0
else
    shift
fi

I signal it by pressing "sh ./name-of-script.sh > -r". Is that the right way to do it? Because it's not actually changing anything.

---------- Post updated at 01:05 PM ---------- Previous update was at 01:03 PM ----------

Quote:
Originally Posted by pludi
So it's homework, right?
Yeah, it's part of my coursework. I'm having trouble with it, I just need to get this part done to finish it.

Is coursework help allowed in this forum?
TeddyP
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename first N numeric strings in filename

Hello, I have some video files containing numbers and characters . To tell the problem shortly, due to a limitation, I am unable create a playlist regularly changing on a daily basis with the command shuffle....So I decided to rename filenames, just a replacement of first five numbers randomly.... (10 Replies)
Discussion started by: baris35
10 Replies

2. Shell Programming and Scripting

Bash to rename files repeats previous filename in directory

In the below bash processes substitution, if there are 3 files in a directory /home/cmccabe/medex.logs/analysis.log, the filename variable is set to where these files are located. The code does execute, the problem is that if there is a renamed file in the output directory below, it gets... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Shell script to get one to one map and rename the filename

I have 2 files sorted by numerically. I need help with shell script to read these 2 files and do a 1:1 mapping and rename the filenames with the mapped case#; For example: cat case.txt 10_80 10_90 cat files.txt A BCD_x 1.pdf A BCD_x 2.pdf ls pdf_dir A BCD_x 1.pdf A BCD_x 2.pdf ... (2 Replies)
Discussion started by: iaav
2 Replies

4. Shell Programming and Scripting

Filename rename with characters of file

Hi, I need a bit of help. I've used awk to get the first 7 characters of a file - awk '{print substr($0,0,7)}' test.csv How do I now take this variable to rename test.csv to variable.csv ? Any help or advice would be greatly appreciated! (2 Replies)
Discussion started by: sianm
2 Replies

5. Shell Programming and Scripting

Rename all files (filename with spaces) to different extension

Hi, I have files with filenames as below. SGM Daily Sales Email-en-us-05312012.xlwa I want to rename it in .xls. I am writing a script to change this, as there can be multiple files in subfolders. I have the following script. #!/bin/ksh for oldfile in $(find... (1 Reply)
Discussion started by: mac4rfree
1 Replies

6. Shell Programming and Scripting

wget same filename from subdirectories and rename or concat

I would like to wget a file "index.html" from a site which is lies in different subdirectories and is different in size. The index.html shall be concatenated or renamed to index01.html index02.html .... I would like to store this file in just one directory and use the option -nd. Though i can't get... (0 Replies)
Discussion started by: sdf
0 Replies

7. Shell Programming and Scripting

Rename FileName in the Directory

In the Directory all the Files are following format. Filename_yyyymmdd_numbers.txt eg. file_name_20120106_015802.txt . I want to write the Shell script to rename all the file to file_name.txt.in the directory. Thanks Mani (5 Replies)
Discussion started by: gavemani
5 Replies

8. UNIX for Dummies Questions & Answers

rename filename

Hi, I am pretty new to this. I have a condition where in I want to replace all files within a folder. All filenames with character "abc" would need to replaced with "xyz". eg: helloabcworld-->helloxyzworld helloworld-->helloworld ... ... Thanks in advance. (6 Replies)
Discussion started by: sakets_2000
6 Replies

9. Shell Programming and Scripting

Grep and rename the filename

Hi All, Can you please help me. The situation is like this. There are many different file name in this directory. I have to grep all the file that the name start with "PTWO" and rename it to COM with the current date. This is the script that I have done and it hit an... (16 Replies)
Discussion started by: badbunny9316
16 Replies

10. Shell Programming and Scripting

rename multiple filename.45267.txt to >> filename.txt

i have several thousand files and in subdirs that are named file.46634.txt budget.75346.pdf etc i want to remove the number but retain the extension. it is always a 5 digit. thanks. (6 Replies)
Discussion started by: jason7
6 Replies
Login or Register to Ask a Question