Renumbering files bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renumbering files bash script
# 1  
Old 10-08-2014
Renumbering files bash script

I am new to the world of Linux scripting, and would like to make the following 2 scripts:

I have 67 files named Alk-0001.txt to Alk-0067.txt
I would like them to be numbered Alk-002.txt to Alk-0134.txt
eg
Code:
Alk-0001.txt > Alk-0002.txt
Alk-0002.txt > Alk-0004.txt
Alk-0003.txt > Alk-0006.txt

Basically just the original file number doubled.

I also have files that I would like renumbered such that the original file number is doubled then minus 1

eg
Code:
Alk-0001.txt > Alk-0001.txt
Alk-0002.txt > Alk-0003.txt
Alk-0003.txt > Alk-0005.txt

I imagine these scripts should be easy but I can't work it out at present, can anyone help?

Many thanks in advance,

Last edited by Franklin52; 10-10-2014 at 07:26 AM.. Reason: Please use code tags
# 2  
Old 10-09-2014
Try

Code:
#!/bin/ksh

for i in *.txt; do
	
	[[ "$i" = */* ]] && path="${i%/*}" || path="."

	file="${i##*/}"
        base="${file%.*}"
	ext="${i##*.}"
	new=$(printf "%s/%s-%04d.%s" $path ${base%-*}  $((${base##*-}*2-1)) $ext)

	# remove echo cp if you feel result is as expected	
	echo cp $i $new

done


Usage

Code:
ksh yourscript

# 3  
Old 10-09-2014
I am hoping you are going to be careful and create a backup before doing any test on it, otherwise you most likely are going to overwrite your files.

Look at this:

Code:
Alk-0001.txt > Alk-0002.txt
Alk-0002.txt > Alk-0004.txt
Alk-0003.txt > Alk-0006.txt

There's a file Alk-0002.txt already, right? What do you think it will happen if you
Alk-0001.txt > Alk-0002.txt
And then you
Alk-0002.txt > Alk-0004.txt ?
You will be practically overwriting files right and left.

Any solution presented must have that in consideration. I think it would have to be a two pass renaming in order to be safe.

Let me show you how to test safely

Code:
mkdir working_dir && cd working_dir
touch Alk-{0001..0067}.txt

Now, you have a dummy army of files to test.
cp the script to this file and execute it only when you are cd into.

Now a possible solution for the first request

Code:
#!/bin/ksh

change_serial() {

    for f in Alk-*.txt; do
        fnameplus=${f%.*}
        ext="${f##*.}"
        serial="${fnameplus##*-}"

        case "$1" in
            1)
                trim_serial=$(printf "%010d" "$((serial*2))");
            ;;
            2)
                trim_serial="${serial:6}"
            ;;
            *)
                echo "bailing out";
                exit 1;
            ;;
        esac

        mv "$f" "Alk-${trim_serial}.${ext}"
    done
}

change_serial 1
change_serial 2


Last edited by Aia; 10-09-2014 at 02:40 AM.. Reason: ksh
# 4  
Old 10-09-2014
Here is a slight change to Aia's code that uses a tmp folder instead of two passes.

It also accepts formula, format and filename wildcard:

Code:
change_serial() {
    mkdir ./tmp_$$

    fn=$1
    fmt=$2
    shift 2

    for f in $*
    do
        prefix=${f%%[0-9]*}
        suffix=${f##*[0-9]}
        serial=${f#$prefix}
        serial=10#${serial%$suffix}
        new=$(printf "$fmt" $(($fn)) )
        mv $f ./tmp_$$/$prefix$new$suffix
    done

    mv ./tmp_$$/* ./
    rmdir tmp_$$
}

change_serial 'serial*2'   '%04d' "Alk-*.txt"
change_serial 'serial*2-1' '%04d' "Blk-*.txt"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script for check files

I created this script for check whether specific files exist or not in the given location. but when I run this its always showing Failed - Flag_lms_device_info_20160628.txt do not exist Failed - Flag_lms_weekly_usage_info_20160628.txt do not exist but both files are existing. appreciate help... (2 Replies)
Discussion started by: lfreez
2 Replies

2. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

3. Shell Programming and Scripting

renumbering within a file

Hi All, I have 100 files named - rep-0.txt, rep-2.txt...rep-99.txt. They each contain information in the following format: abc 1 qwe asd 2 zxc poi 3 lkj pdh 4 ldf hgf 5 tyu i would like to re-number , so that all the new files (rep0.dat, rep1,dat....) have... (1 Reply)
Discussion started by: chen.xiao.po
1 Replies

4. UNIX for Dummies Questions & Answers

renaming (renumbering) fasta files

I have a fasta file that looks like this: >Noname ACCAAAATAATTCATGATATACTCAGATCCATCTGAGGGTTTCACCACTTGTAGAGCTAT CAGAAGAATGTCAATCAACTGTCCGAGAAAAAAGAATCCCAGG >Noname ACTATAAACCCTATTTCTCTTTCTAAAAATTGAAATATTAAAGAAACTAGCACTAGCCTG ACCTTTAGCCAGACTTCTCACTCTTAATGCTGCGGACAAACAGA ... I want to... (2 Replies)
Discussion started by: Oyster
2 Replies

5. Shell Programming and Scripting

column renumbering

Hi, I am a beginner in awk scripting! I need your help; I want to replace the fifth column number (which is 15 here) here in this file for example : ATOM 142 N PRO A 15 ATOM 143 CD PRO A 15 ATOM 144 HD1 PRO A 15 ATOM ... (5 Replies)
Discussion started by: adak
5 Replies

6. Shell Programming and Scripting

Again integrate files - bash script

I wrote script in bash which generates this report "users.csv": I wrote script in bash which generates this report "groups.csv" I want to integate two reports: "users.csv" and "groups.csv". I want like so that "result.csv": (2 Replies)
Discussion started by: patrykxes
2 Replies

7. Shell Programming and Scripting

Bash script - integrate two files

I wrote script in bash which generates this report "users.csv": I wrote script in bash which generates this report "groups.csv" I want to integate two reports: "users.csv" and "groups.csv". I want like so that "result.csv": Thx (5 Replies)
Discussion started by: patrykxes
5 Replies

8. Shell Programming and Scripting

Renumbering files scripting help!

I am new to the world of UNIX scripting - and would like to make the following script: I have 100 files numbered 1-100. However - i would like to continue the file list - so that I add another 100 files following on, so that file 101 = 99; file 102 = 98 ; 103 = 97 and so on.... (basically ...... (6 Replies)
Discussion started by: AJC1985
6 Replies

9. UNIX for Dummies Questions & Answers

renumbering user id

Other than deleting and recreating a user can a users id number be changed? I need to have my user id the same on more than one system. (1 Reply)
Discussion started by: thumper
1 Replies
Login or Register to Ask a Question