Loop renaming files w/ a count problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop renaming files w/ a count problem
# 1  
Old 12-01-2011
Error Loop renaming files w/ a count problem

Smilie

Hello there,

basically in my program where im stuck at is when it comes to rename the files in a loop.

- the program counts the number of files w a given name (works!)

- and then if the number of files is greater or equal to the MAX_VERSIONS (numbers of files allowed w the same name) then it would need to re name file_v1 to file_v2 and file_v3 to file_v4 and etce etc so that the new copied file can be the v1 and also delete the version that is above MAX_VERSIONS

- i know in the piece of code provided i did not put the comand to copy the new file to file_v1 or the delete the file that is greater MAX_VERSIONS
thats coz im just strugling to move the v1 to v2 and so on...

can anyone what am i doing wrong??????????????


Thankss

Quote:
#!/bin/bash
TRASH_PATH=$HOME/delete
MAX_VERSIONS=3

fileName=$1

#count number of files and store in numOFiles

numOFiles=0

for file in $TRASH_PATH/$fileName\_v*
do
numOFiles=$(($numOFiles+1))
done


# the problematic loop

if [[ $numOFiles -ge $MAX_VERSIONS ]] ; then

versions=$MAX_VERSIONS

for ((i=1;i <= $MAX_VERSIONS; i++))
do

((versions--))

mv $TRASH_PATH/$fileName\_v$i $TRASH_PATH/$fileName\_v$versions

done
# 2  
Old 12-01-2011
I am not sure why your script is failing, but you should implement what you stated:
  • delete any version greater that max versions
  • move version N to version N+1
  • move file to version 0

In your version, you count the number of files - which would be thrown off if you had file versions "_v1", "_v2", "_v3", "_v42", and "_v314198".

Code:
#! /bin/bash

TRASH=${TRASH_PATH:-${HOME}/delete}
MAX_VERSIONS=${MAX_VERSIONS:-3}

for file in "${@}"; do

    for x in "${TRASH}/${file}_v"*; do
        v="${x##*_v}"

        if [[ "${v}" == '*' ]]; then
            continue
        fi

        if [[ ${MAX_VERSIONS} -lt "${v:-0}" ]]; then
            rm -f "${x}"
        fi
    done

    v=${MAX_VERSIONS}

    while [[ 0 -lt ${v} ]]; do
        (( n = v - 1 ))
        mv -f "${TRASH}/${file}_v${n}" "${TRASH}/${file}_v${v}" 
        (( v = v - 1 ))
    done

    if ! mv "${file}" "${TRASH}/${file}_v${v}"; then
        echo ${file}: unable to trash 1>&2
    fi
done

Now the one problem both scripts will have is if you call it with a path'ed file, such as:
Code:
intothetrash /the/path/to/this/file

I don't think there is an issue with always appending "_v#" to a filename, unless you surpass the maximum length of a filename.

Last edited by m.d.ludwig; 12-01-2011 at 08:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem renaming files on Solaris 10 server

Good day all. I'm trying to rename some files in my home directory with some bizarre results. Basically I need to change the IP address in the filename to the hostname which I ggrep from within the file: -rw-r--r-- 1 bh694n nrc 5095 May 2 20:03 alarms_999.189.161.146.log... (2 Replies)
Discussion started by: BRH
2 Replies

2. Shell Programming and Scripting

Error files count while coping files from source to destination locaton as well count success full

hi All, Any one answer my requirement. I have source location src_dir="/home/oracle/arun/IRMS-CM" My Target location dest_dir="/home/oracle/arun/LiveLink/IRMS-CM/$dc/$pc/$ct" my source text files check with below example.text file content $fn "\t" $dc "\t" $pc "\t" ... (3 Replies)
Discussion started by: sravanreddy
3 Replies

3. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

4. Shell Programming and Scripting

Record count checking for multiple files through for-loop

Hi Friends, I wrote one shell script to check the record count in two files and that will send us the notification activity if found zero record count. What i did is I created for loop and checking the count for both of the files but what is happening is for first file has data then it's... (13 Replies)
Discussion started by: victory
13 Replies

5. UNIX for Advanced & Expert Users

Problem with renaming files

I have about 1000 files containing the character * in the name. I need to find these files and replace the * with a -. I am working with HP UX v11. I am using the following command find . -type f -name '*\**' -exec bash -c 'f="$1"; mv "$f" "${f//\*/-}"' - '{}' \ People tell me it works for... (4 Replies)
Discussion started by: MikeDavid
4 Replies

6. Shell Programming and Scripting

Problem renaming files using variables

Hi, I have the following problem: I have a list of files: 1.txt 2.txt 3.txt 4.txt Then I have a list of variable names inside variable.txt: A B C D I'd like to rename 1.txt, 2.txt etc using the variables from variable.txt (2 Replies)
Discussion started by: hubleo
2 Replies

7. Shell Programming and Scripting

Renaming of files with different extensions on the same path to .found with the help of loop

hi , I have certain files at the same path with differeent extensions like .dat , .txt etc ...........i need to rename them with extension .found at the same path with the help of loop.... also the files names will be like this ; abc_2010_c1.dat abc_2010_c2.dat xyz_2010_c1.txt (2 Replies)
Discussion started by: amitpta
2 Replies

8. UNIX for Advanced & Expert Users

problem with renaming files

Hi, I need to rename all the .txt files present in current directory to .dat files respectively in UNIX. for example: $ ls aaa.txt bbb.txt ccc.txt I need to change them to $ ls aaa.dat bbb.dat ccc.dat Is there any UNIX command to do this in one go? ... (3 Replies)
Discussion started by: Johny001
3 Replies

9. Shell Programming and Scripting

Need Help in using count in while loop

Hi , I have a source file having structure as below aaaaaa cvvvvvv bbbbbb ddddddd wwww cccccds I want to create a new file in which each line contains two pipe separated characters eg first character till end of first space and then next 2 characters from same file eq... (5 Replies)
Discussion started by: ashish2345
5 Replies

10. UNIX for Dummies Questions & Answers

Renaming duplicate files in a loop

Hello, I have a bunch of files whose names start with 'xx' The first line of each file looks something like: a|...|...|...|... , ... In order to rename all of these files to whatever's between the 4th | and the comma (in the first line of that particular file) , I have been using: for... (2 Replies)
Discussion started by: juliette salexa
2 Replies
Login or Register to Ask a Question