Sponsored Content
Full Discussion: Rename files
Top Forums Shell Programming and Scripting Rename files Post 302894164 by Jomeaide on Monday 24th of March 2014 07:49:30 AM
Old 03-24-2014
I currently solved temporarily. But I do not like the solution I have now.

Code:
SERVICE="BJBuser"
DIRRECIBIDOBJB="/tmp/home/usuariossftp/BJBuser/archivos/"
DIRRENOMBRADOBJB="/tmp/home/usuariossftp/BJBuser/archivos/.toMUTUACTIVOS/"
FILEOUT="datosbjb"
FICHEROS=""
LOGFILE="/tmp/var/log/BJB/BJBmove"$(date +%Y_%m)".log"
MD5SUM=""
NULL=/dev/null
i='let i=1'

cd $DIRRECIBIDOBJB

check(){
i=`ls $DIRRENOMBRADOBJB | wc -l`
}


logs(){
echo "$(date)  $*: Recibido: $fichero --> Renombrado: $FILEOUT$i.txt ($MD5SUM)" >> $LOGFILE
}

compare_dir(){
FICHEROSCOMP=`ls -1 $DIRRENOMBRADOBJB`
for compfile in $FICHEROSCOMP
do

    if [[ ! -r "$DIRRENOMBRADOBJB$compfile" || ! -r "$DIRRENOMBRADOBJB$fichero" ]]
    then
    echo "El fichero $compfile existe" >> $NULL
    fi
    continue
done
}

while true
do

valor=$(ps aux | grep root | grep -v grep | grep -i sshd | grep BJBuser)


# comprobamos si hay una conexion sftp del usuario BJBuser
#
if [ ! "$valor" ]
    then  
#        echo "No hay conexion del usuario: $SERVICE por sftp"
        FICHEROS=`ls -1 $DIRRECIBIDOBJB`
        check;
        for fichero in $FICHEROS
        do
                if [ ! -f $DIRRECIBIDOBJB$fichero ]   # Comprueba que existe
                then
                        echo "$FICHEROS no exite." >> $NULL
                sleep 2    
                        continue        # Seguimos
                fi
            compare_dir;    
            sleep 2
                let "i+=1"
            if [ -f $DIRRENOMBRADOBJB$FILEOUT$i.txt ]    
            then
                    echo "$DIRRENOMBRADOBJB$FILEOUT$i.txt existe" >> $NULL
            else 
                ls $DIRRECIBIDOBJB*_FX_* | awk '{system("mv " $0 " " sprintf(".toMUTUACTIVOS/userbjbfx%02d.txt", ++i))}'
                ls $DIRRECIBIDOBJB*_SP_* | awk '{system("mv " $0 " " sprintf(".toMUTUACTIVOS/userbjbsp%02d.txt", ++i))}'
                mv $DIRRECIBIDOBJB$fichero $DIRRENOMBRADOBJB$FILEOUT$i.txt
                MD5SUM=`md5sum $DIRRENOMBRADOBJB$FILEOUT$i.txt`
            fi
                logs;
                chown BJBuser $LOGFILE >> $NULL
        done

        else 
        echo "El servicio $SERVICE esta levantado" >> $NULL
fi

exit 0

done

I would like to improve.

When you rename a file containing _FX_ destination is overwritten if it exists and does not want to.

Sorry for my English, it helps me a google translator :-)
 

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

rename files help

Hi, I've already search for this issue but I found different scripts for rename files, but I don't know how to customize it for my needs. Here's what I want to do: I have a lot of files inside many directories, like this: /aa/01.txt /aa/02.txt /ab/01.txt /ab/02.txt I want all those files... (2 Replies)
Discussion started by: piltrafa
2 Replies

3. Shell Programming and Scripting

Rename files

Hello, I've a list of file like this img_001 img_22 img_44 and I would rename all with this form photo_0001 photo_0002 photo_0003 photo_0004 suggestions?Thanks to all. (2 Replies)
Discussion started by: cv313x
2 Replies

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

5. Shell Programming and Scripting

Rename files

Hi, I wanna rename bunch of files which has ":" to -. ie. rename file named file1:file1 to file1-file1. any ideas? (2 Replies)
Discussion started by: linuxaddict7
2 Replies

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

7. Shell Programming and Scripting

How to rename files

Hi Guys, I have to rename about 180 files in different folders in linux. For example, abc_110117.txt eff_110117.txt zzz_110117.txt After renaming the files, these files should like like abc.txt eff.txt zzz.txt I created a small script to rename the files like ls... (2 Replies)
Discussion started by: naveed
2 Replies

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

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

10. 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
RENAME(3)								 1								 RENAME(3)

rename - Renames a file or directory

SYNOPSIS
bool rename (string $oldname, string $newname, [resource $context]) DESCRIPTION
Attempts to rename $oldname to $newname, moving it between directories if necessary. If $newname exists, it will be overwritten. PARAMETERS
o $oldname - Note The old name. The wrapper used in $oldname must match the wrapper used in $newname. o $newname - The new name. o $context - Note Context support was added with PHP 5.0.0. For a description of contexts, refer to "Streams". RETURN VALUES
Returns TRUE on success or FALSE on failure. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.3.1 | | | | | | | rename(3) can now rename files across drives in | | | Windows. | | | | | 5.0.0 | | | | | | | rename(3) can now also be used with some URL | | | wrappers. Refer to "Supported Protocols and Wrap- | | | pers" for a listing of which wrappers support | | | rename(3). | | | | | 4.3.3 | | | | | | | rename(3) may now be able to rename files across | | | partitions on *nix based systems, provided the | | | appropriate permissions are held. Warnings may be | | | generated if the destination filesystem doesn't | | | permit chown() or chmod() system calls to be made | | | on files -- for example, if the destination | | | filesystem is a FAT filesystem. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 Example with rename(3) <?php rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt"); ?> SEE ALSO
copy(3), unlink(3), move_uploaded_file(3). PHP Documentation Group RENAME(3)
All times are GMT -4. The time now is 02:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy