Rename the files by using code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename the files by using code
# 1  
Old 04-05-2012
Rename the files by using code

Hi friends I have a question,

Assume that I have 3 files' named as file356.dat, file358.dat, file1521.dat

how can I rename them as file-356, file-358, file-1521


Thanks
# 2  
Old 04-05-2012
Code:
for file in `ls file*`;
do
mv $file `echo $file | sed s:file:file-:`
done


Last edited by 47shailesh; 04-05-2012 at 12:15 PM.. Reason: variable correction
This User Gave Thanks to 47shailesh For This Post:
# 3  
Old 04-05-2012
Code:
#!/bin/bash

for FILE in file*
do
        echo mv "$FILE" file-"${FILE:4}"
done

Remove the echo once you've tested and are sure it does what you want.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 04-05-2012
Quote:
Originally Posted by Corona688
Code:
#!/bin/bash

for FILE in file*
do
        echo mv "$FILE" file-"${FILE:4}"
done

Remove the echo once you've tested and are sure it does what you want.
thank you for reply but this code puts .dat to the end of the names. can you do it excluding the .dat extension.
# 5  
Old 04-05-2012
Sure.

Code:
#!/bin/bash

# Make variable splitting happen on "."
IFS="."

for FILE in file*
do
        # Set $1="filename", $2="ext", because it splits on "."
        set -- $FILE

        echo mv "$FILE" file-"${1:4}"
done

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 04-05-2012
@rpf, if you use the suggestion with IFS, don't forget to put IFS back to its original value if you plan to do more stuff in the same shell, otherwise you will get unexpected results...
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-05-2012
Another sugestion. This time using sed:
Code:
#!/bin/bash
for i in file*; do
  mv "$i" `echo "$i" | sed 's/file/file-/ ; s/\.dat//'`
done

However, I think this solution might be a little bit slower when compared to that using pure bash.
Have fun!

Last edited by chapeupreto; 04-05-2012 at 06:31 PM..
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

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

3. UNIX for Dummies Questions & Answers

Rename Files

Hi all, I am newbie and Im trying to rename a set of files & there are over 2900 of them. So, the best way I thought was through a script and here is what I got & it doesnt work. Im not sure as how to figure this out. Thanks Gonzalez Here is what I have - -a:~/Users/GonzaPue/ls -altr... (3 Replies)
Discussion started by: PG3
3 Replies

4. 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

5. Shell Programming and Scripting

Rename files

Hi, I am new to Unix and i have a requirement where i need to write a shell script where i have to loop through various files present in a directory and rename them based on below criteria. Files in the folder are in the following format. _YYYYMMDD.dat] SDL_V1_20100530.dat... (6 Replies)
Discussion started by: bishoo
6 Replies

6. Shell Programming and Scripting

Rename many files

Hi, I have many files ex: file1, file2 ...file100, and I would like to rename only files with "1" in name. I don't have experience with bash and other shells. I know I can use "for i in" and "if", and I can use "sed" to change "1" but I have no idea how should "if" look. I will be grateful... (6 Replies)
Discussion started by: Physix
6 Replies

7. Shell Programming and Scripting

Rename files

I wrote a script that accepts filenames as argument but I am having difficulty if filename has both(uper/lower) cases..so I want to write a script that accepts one or more filenames as arguments and converts filenames to uppercase..(actually rename files..) (2 Replies)
Discussion started by: aadi_uni
2 Replies

8. 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

9. Shell Programming and Scripting

Rename many files

Hi all I have files in the following format: 01_anote1.pdf 01_bnote1.pdf 01_control1.pdf 01_ethics1.pdf 01_invoice1.pdf 01_invoice_21.pdf 20_quote_l1.pdf I need to rename them to 01_anote.pdf 01_bnote.pdf 01_control.pdf 01_ethics.pdf 01_invoice.pdf (9 Replies)
Discussion started by: lmatlebyane
9 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