RENAMING

Renaming .CSV to .csv

for f in *.CSV; do
   newf=$(echo "$f" | sed -e "s|.CSV|.csv|")
   mv "$f" "$newf"
done

Renaming .XLS to .xls

Macro excel does not accept XLS

for f in *.XLS*; do
    newf=$(echo "$f" | sed -e "s|.XLS|.xls|")
    mv "$f" "$newf"
done

Renaming certain files

#!/bin/bash

PathTouch=/home/mxbrain/data/"$client"/mx_touch_"$client"/mx_touch_"$client"_depot/

cd $PathSsh

for f in *.csv; do
    #On commence par chercher les fichiers qui contiennent "Teil39", "Teil40" ou "Teil41" dans le nom du fichier
    if [ $(echo $f | grep -c Teil39) -gt 0 ] || [ $(echo $f | grep -c Teil40) -gt 0 ] || [ $(echo $f | grep -c Teil41) -gt 0 ]; then
        #On ajoute Boiler_ devant le nom du fichier
        new_file=Boiler_$(echo $f)
        #On déplace le fichier "Boiler_xxx" dans le répertoire dépôt
        mv $f "$PathRepo"$new_file
    fi
    # We delete the CAM_date_ files
    if [ $(echo "$f" | grep -c CAM_date_) -gt 0 ]; then rm $f; fi
    # Other csv files don't have anything special to be done to them
    mv "$f" "$PathTouch"
done
  • Renaming the files that contain the TOTO keyword (inside the files themselves, not in the title)
#!/bin/bash
for f in *.csv; do
    if [ $(grep "TOTO" $f | wc -l) -gt 0 ]; then
        mv $f "TOTO_"$f
    fi
done 

Replacing a string within the data of a file

  • Here in the PHDextract01_ files we replace "Snapshot/Before" with "chuck norris"
for f in *.csv; do
    if [ $(echo $f | grep -c PHDextract01_) -gt 0 ]; then
        sed -i "s|Snapshot/Before|chuck norris|g" "$f"
        mv "$f" "$PathRepo"
    fi
done
  • In that case separator was "|" and we needed to have it ";"
for f in *.csv; do
    if [ $(echo $f | grep -c PHDextract01_) -gt 0 ]; then
        sed -i 's/|/\;/g' $f
        mv "$f" "$PathRepo"
    fi
done 

Modifying the extension of a set of files

#!/bin/sh
for i in *.Xls; do
    mv $i $(basename $i .Xls).xls
done 

To keep the directory tree in the filename

#Décommenter les 4 lignes suivantes si on veut traiter une archive. Si on a déjà le dossier dezippé pas besoin
#Uncomment the 4 following lines if you want to handle an archive. If it's an unzip folder, no need
#for f in *.zip; do
#7z x "$f" -aot
#   unzip "$f"
#done

#Path to folder of the choosen archive (files will be send here)
#Chemin où se trouve le dossier ou l'archive à traiter (Les fichiers renommés correctement seront placés ici)
newpath="/home/martins/Bureau/Script/arch/"

# Cleaning spaces in name of folder / files
#Nettoyage des espaces dans les noms de dossiers / fichiers
find -name "* *" -print0 | sort -rz |
    while read -d $'\0' f; do
        detox -r "$f"
    done

#Browse every folders
#On parcourt chaque dossiers
for f in */; do
    #Rename all folders
    #On renomme tous les fichiers
    for file in $(find $f -type f -name "*"); do
        shortname=${file#$f/}
        newname="$newpath/${shortname//\//_}"
        if [ -f $newname ]; then
            echo "$newname already exists."
        else
            echo "copy: $file"
            echo "  --> $newname"
            cp $file $newname
        fi
    done
done

Copying and renaming files according to the parent directory

Given this directory structure:

|test1/
MONDAT123.txt
|test2/
MONDAT123.txt
|test3/
MONDAT123.txt
|script.sh
|toupload/

We want to move all MONDAT123.txt files from test* folders to toupload folder, keeping as prefix their original directory name.

for i in $(ls -d test*); do
    cp -nv "$i"/MONDAT123.txt toupload/"$i"-MONDAT123.txt
done

Explanations:

$(ls -d test*)

=> Lists all directories starting with "test"

cp -np 

=> Copies the file and leaves the original (replace with mv if you want to delete the source file)

toupload/"$i"-MONDAT123.txt

=> Moves the files in the "toupload" directory, "$i" contains the name of the parent directory

Renaming files if it contains a column name

If the file has the column "Fin Ecrasage", it add "22KTinfo" in the beginning of the file name.

for f in *.csv; do
    if [ $(egrep -a -h "Fin Ecrasage" "$f" | wc -l) -gt 0 ]; then
        mv $f $PathRepo"/22KT_info_"$f
    fi
    mv $f $PathRepo
done 

Was this article helpful?

Powered by Zendesk