Rename files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename files
# 1  
Old 03-24-2014
Rename files

I am getting some files with this mask:

aaaa_SP_bbb.txt
aaaa_FX_bbbb.txt
aaaabbbb.txt

I want to rename the files containing:

_SP_ -> Rename - >> fileSP.TXT
_FX_ -> Rename - >> fileFX.txt

and other file that does not contain these words rename them:

filenamexx.txt

as I can do?
# 2  
Old 03-24-2014
What have you tried?
# 3  
Old 03-24-2014
This:

mover_fichero(){
## Compramos los ficheros y los movemos.
ls *_FX_* | awk '{system("mv " $0 " " sprintf("userbjbfx%02d", ++i))}'
ls *_SP_* | awk '{system("mv " $0 " " sprintf("userbjbsp%02d", ++i))}'
}
# 4  
Old 03-24-2014
Is this working for you?

Can you code tags?

Last edited by anbu23; 03-24-2014 at 08:44 AM..
# 5  
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 :-)
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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question