Messing with other people’s files

There is this girl in the office that claims to be an expert in Linux. The truth is that she barely knows how to operate a computer. I don’t like her, or her attitude, so I made this script.

#!/bin/bash

actual=`pwd`
cd $1

#We create the list of files
ls -1 > ../list1
mv ../list1 .
cp list1 original

#Get the total number of files
number=`wc -l list1 | awk '{print $1}'`

#Do the changes, as random as is could manage to...
for i in `seq 1 $number`
do
    #Invert the list
    tac list1 > list2
    #Get a random number for desition
    ran=$(( $RANDOM % 10 ))
    if  [ $ran -lt 5 ]
    then
        #Get a random number for desition
        ran2=$(( $RANDOM % 10 ))
        if  [ $ran2 -gt 5 ]
        then
            let cola=$number-$i
            #Put the last line as the first one
            tail -$i      list2 >  list1
            head -n $cola list2 >> list1
        else
            #Swap lines
            awk 'NR%2{x=$0;next}{print $0"\n"x;}END{if(NR%2)print;}' list2 > list1
        fi
    else
        let cola=$number-$i
        #Put the first line at the end
        tail -n $cola  list2 >  list1
        head -$i       list2 >> list1
    fi
done

#Changes the files list to temporary
while read line
do
    echo $line-tmp >> list3
done < list1 #Merge the original and tmp lists paste original list3 > list4

#Get the new names to the files
while read line
do
    mv `echo $line`
done < list4

#remove the tmp tag
for i in $(cat list3)
do
    raiz=`echo $i | cut -f1 -d-`
    mv $i $raiz
done

#Remove the rubbish
rm list1 list2 list3 list4 original
cd $actual

exit 0

Basically, what it does is to interchange the names of the files in a directory. It’s not big deal, unless you’re dealing with thousands of them and you’re a computer illiterate bitch. Anyways, it takes as first argument the name of the directory that you want to mess with.

I will have fun on Monday.

Anuncios

Acerca de José Manuel

I'm from Mexico and currently in a PhD program in the University of Oviedo, in Spain.
Esta entrada fue publicada en Escuela y etiquetada , , . Guarda el enlace permanente.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s