Rename Files in sequence


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Rename Files in sequence
# 1  
Old 09-30-2008
Rename Files in sequence

I have a bunch of files below in some directory /home/tmp:
abc.txt
def.txt
ghi.txt


And I want to rename them with sequence as their extension (in the format 00# see below):
abc.001
def.002
ghi.003


I need some help with this and below is a basic code structure to get you started:
for file in `ls /home/tmp`
do
mv $file <this is where I need help>
done


Thanks,

- CB
# 2  
Old 09-30-2008
Hammer & Screwdriver Perhaps this will help

Code:
#! /usr/bin/bash

cnt=1
for myfile in `ls *.txt`
   do
   cntt=$(printf "%.3d" $cnt)
   myfilem=$(echo $myfile | cut -d"." -f1)
   echo $cntt $myfile $myfilem
   cp $myfile $myfilem"."$cntt
   cnt=$((cnt+1))
done

before running script
> ls -l *.txt
-rw-rw---- 1 xxx dp 4 Sep 30 08:58 abc.txt
-rw-rw---- 1 xxx dp 4 Sep 30 08:58 def.txt
-rw-rw---- 1 xxx dp 4 Sep 30 08:58 ghi.txt

after running script
> ls -l *.0*
-rw-rw---- 1 xxx dp 4 Sep 30 09:06 abc.001
-rw-rw---- 1 xxx dp 4 Sep 30 09:06 def.002
-rw-rw---- 1 xxx dp 4 Sep 30 09:06 ghi.003
# 3  
Old 09-30-2008
if you have Python, here's a script you can use. eg
Code:
# ls -1 *.txt
abc.txt
def.txt
ghi.txt

# filerenamer.py -s "txt" -e "001:999" -l -n "*.txt"
==>>>>  [ /home/abc.txt ]==>[ /home/abc.001 ]
==>>>>  [ /home/def.txt ]==>[ /home/def.002 ]
==>>>>  [ /home/ghi.txt ]==>[ /home/ghi.003 ]

# filerenamer.py -s "txt" -e "001:999" -n "*.txt"  #remove -l to commit
/home/abc.txt  is renamed to  /home/abc.001
/home/def.txt  is renamed to  /home/def.002
/home/ghi.txt  is renamed to  /home/ghi.003

# ls -1
abc.001
def.002
ghi.003

# 4  
Old 09-30-2008
You are the best... thanks for the prompt response.

- CB
 
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

Add sequence number to files

Hi I am having files in my dir ,some times it vary ,i need to assign sequence number with gunzip the file name .Please advise ex: file1.gz file1111.gz file11111.gz file1111111.gz expecting out output file_1 file1111_2 file1111111_3 i written this script ,its not working what i... (3 Replies)
Discussion started by: mohan705
3 Replies

3. Shell Programming and Scripting

Case script to get missing sequence among files

I want to use case statement to find the range of missing sequence in my directory which it has some few ( dat & DAT ) files my directory /home/arm/my_folder/20130428 contains : f01_201304280000.DAT f01_201304280001.DAT f01_201304280003.DAT f02_201304280000.dat f02_201304280002.dat... (2 Replies)
Discussion started by: arm
2 Replies

4. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

5. Shell Programming and Scripting

New sequence nos to be added in new files

Hi All, Please help me in below request. Got multiple files in a dir ex: /os . from each file need to filter based upon first field (field only has 1,2,3 or 4) and then put it in different files based on the Field 1. But need to generate a sequence while inserting and the sequence number needs... (1 Reply)
Discussion started by: rdakshn
1 Replies

6. Shell Programming and Scripting

print out missing files in a sequence

Hello all, I have several directories with a sequence of files like this IM-0001-0001.dcm IM-0001-0002.dcm IM-0001-0003.dcm IM-0001-0004.dcm IM-0001-0005.dcm I would like to print out the name of the file that is missing. I currently have the following ineffecient way to do this... (4 Replies)
Discussion started by: avatar_007
4 Replies

7. Shell Programming and Scripting

ls all files in sequence. How to?

Hi, all: Newbie questions: For example, if I have 6 files, respectively named as: How to "ls" all files in sequence of two ways? 1) The first way is according to the sequence of the natural number. Say, the same sequence as shown as they are given. 2) The second way is default... (2 Replies)
Discussion started by: jiapei100
2 Replies

8. Shell Programming and Scripting

perl merge two files by the time sequence

Hi Guys, i have two files: fileA: 20090611 00:00:11 20090611 00:00:11 20090611 00:00:24 20090611 00:01:10 20090611 07:13:00 fileB: 20090611 00:00:01 20090611 00:00:12 20090611 00:00:24 20090611 00:01:12 20090611 09:13:00 want to make two files into a single file, but follow the... (14 Replies)
Discussion started by: jimmy_y
14 Replies

9. Shell Programming and Scripting

script to touch a sequence of files

Hi, My requirement is i need to touch a sequence of files like touch a touch b touch c now i need the script in a way that checks if all files are touched or not. if any file say b is not touched then i should get this notification. Please suggest as i am new to unix (2 Replies)
Discussion started by: sussane
2 Replies

10. Shell Programming and Scripting

Delete all files up to a sequence

All, Please can you help me with this ? I have a requirement to compress the files older than a sequence number. Say the directory has the following files a_101 a_102 a_103 a_104 a_105 a_106 a_107 a_108 I would like to compress all files based on a number i supply. IE. if i... (10 Replies)
Discussion started by: kamathg
10 Replies
Login or Register to Ask a Question