how to rename a number of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to rename a number of files
# 1  
Old 04-30-2009
how to rename a number of files

Hi
I need to rename about hundred of files which contain ".R " and the end, for example:
/t1/data/f2993trn.ix7.R
The new file should not have ".R" extension, ex:
/t1/data/f2993trn.ix7.R

Is there a command which I can put in the loop to do this? Thansk for any advice -A
# 2  
Old 04-30-2009
Code:
cat your_list_of_files |
while read file_nm ; do

  echo /bin/mv $file_nm ${$file_nm%.*}

done

save that in a script.
run it.
if it looks good, pipe it to /bin/ksh:

Code:
quirks_script | /bin/ksh

and voila.
# 3  
Old 04-30-2009
Not sure if ${$file_nm%.*} will give you grief but you can do this:

Code:
ls *.R 2>/dev/null|while read file_nm ; do
 echo mv $file_nm ${file_nm%.*}
done

Then do what quirkasaurus said to check and then run.
# 4  
Old 04-30-2009
no need ls in fact
Code:
for file in *.R
do
....
done

# 5  
Old 04-30-2009
Quote:
Originally Posted by giannicello
Code:
ls *.R 2>/dev/null|while read file_nm ; do
 echo mv $file_nm ${file_nm%.*}
done


Not only is ls unnecessary, but it will break the script if any filenames contain spaces.

The filenames should be quoted:
Code:
for file in *.R ; do
 echo mv "$file" "${file%.R}"
done

# 6  
Old 04-30-2009
Not sure about Unix flavors, but if you have Linux, then you don't even need a loop. The rename command can fix the file names.

Code:
$
$ uname -o
GNU/Linux
$
$ ls -1
file1.R
file2.R
file3.R
file4.R
file5.R
file 6.R
$
$ rename .R '' *.R
$
$ ls -1
file1
file2
file3
file4
file5
file 6
$
$

HTH,
tyler_durden
# 7  
Old 04-30-2009
Quote:
Originally Posted by durden_tyler
Not sure about Unix flavors, but if you have Linux, then you don't even need a loop. The rename command can fix the file names.

There are two different versions of rename in Linux distributions, and the syntax is not the same for both. If you have a rename command, check the man page for the correct syntax.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

2. Shell Programming and Scripting

List files with number to select based on number

Hi experts, I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number. I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for... (5 Replies)
Discussion started by: mysocks
5 Replies

3. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

4. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

5. UNIX for Dummies Questions & Answers

Rename a large number of files in subdirectories

Hi, I have a large number of subdirectories (>200), and in each of these directories there is a file with a name like "opp1234.dat". I'd like to know how I could change the names of these files to say "out.dat" in all these subdirectories in one go. Thanks! (5 Replies)
Discussion started by: lost.identity
5 Replies

6. UNIX for Dummies Questions & Answers

Rename all .sh files to .pl

I have various .sh and .pl files in one directory. I want to rename all the .sh files to .pl i.e testscript.sh --> testscript.pl I am trying to use mv *.sh *.pl It doesnt work though!! (3 Replies)
Discussion started by: chrisjones
3 Replies

7. Shell Programming and Scripting

rename files Ax based on strings found in files Bx

Hi, I'm not very experienced in shell scripting and that's probably why I came across the following problem: I do have several hundred pairs of text files (PF00x.spl and PF00x.shd) where the first file (PF00x.spl) needs to be renamed according a string that is included in the second file... (12 Replies)
Discussion started by: inCH
12 Replies

8. Shell Programming and Scripting

rename files

hey all, I have files in the format of ABCD20061101 and ABCDEF20061101 in one directory, I would like to change all ABCD20061101 to ABCDEF20061101 and the problem is if I do a simple pattern match of ABCD, then those ABCDEF20061101 would also... (2 Replies)
Discussion started by: mpang_
2 Replies

9. UNIX for Dummies Questions & Answers

Rename a number of files using grep

Hi all, I'm trying to rename a directory of html-files: index.php?page=start should become start.html I've lost track of all the pipes and brackets and decided I need some help ;-) Many thanks. (4 Replies)
Discussion started by: TylerDurden
4 Replies

10. UNIX for Dummies Questions & Answers

How to rename files?

:confused: How can i rename a file 'x.log' to 'x_20020512 072909.log' :eek: i'm using perl, with system command from a unix web server, and need to timestamp my logs if the above format (filename _ year month day hr min sec .log) (9 Replies)
Discussion started by: CompuTelSystem
9 Replies
Login or Register to Ask a Question