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
ZGREP(1)                                                      General Commands Manual                                                     ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 11:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy