Monday, May 13, 2013

Parallel/threaded rename to numbers script.

#! /bin/bash
# Rename all the files and folders in the current directory serially.
# DOES NOT PRESERVE EXTENSION.
#1st argument -- no. of processors
# 2nd argument -- no of presiding 0s
names="$(ls -1)"
# Remove current script file
names="$(echo "$names" | grep --invert-match --line-regexp "${0//.\/}")"
# No. of items -- required.
count=$(echo "$names" | wc --lines)
declare -i quot
declare -i i
declare -i from
declare -i till
from=1
i=1
# Function does the actual renaming.
# takes in arguments as the line no. of $names
# Since each line in $name is a file to be renamed randomly, an instance of the function will have to be supplied with with range of names it'll process.
# 'an instance' cause it's multithreaded
# first argument -- from which line will it start processing.
# second argument -- till which lines will it process.
rename_till () {
 declare -i c
#  from $c
 local c=$1
 declare -i entries
 local entries=$2
#  Till $entries in $names
 while [[ $c -le $entries ]]
 do
#   head -$c output's the first $c lines. Last line is of interest only, it's the cth line.
  local name="$(echo "$names" | head -$c | tail -1)"
#   File processed, proceed to next line.
  c=$c+1
  mv -i "$name" $(printf "%06d" $c) &
  echo "$c of $entries done." &
 done
}
# rename_till() calling mechanism.
# $till determines values of $2 in rename_till()
# Each thread will process $count/$1 lines
# This truncates decimals.
till=$(($count/$1))
while [[ $i -le $1 ]]
do
 rename_till $from $till &
 i=$i+1
 from=$from+$(($count/$1))
 if [[ $i -eq $1 ]]
 then
#   Remaining fines (truncated by $count/$1) added to last rename_till() call.
  till=$till+$(($count/$1))+$(($count%$1))
 else
  till=$till+$(($count/$1))
 fi
done

Parallel/threaded random rename script.

#! /bin/bash
# Rename all the files and folders in the current directory to random.
# DOES NOT PRESERVE EXTENTION
#1st argument -- no. of processors
names="$(ls -1)"
# Remove current script file
names="$(echo "$names" | grep --invert-match --line-regexp "${0//.\/}")"
# No. of items -- required.
count=$(echo "$names" | wc --lines)
declare -i quot
declare -i i
declare -i from
declare -i till
from=1
i=1
# Function does the actual renaming.
# takes in arguments as the line no. of $names
# Since each line in $name is a file to be renamed randomly, an instance of the function will have to be supplied with with range of names it'll process.
# 'an instance' cause it's multithreaded
# first argument -- from which line will it start processing.
# second argument -- till which lines will it process.
rename_till () {
 declare -i c
#  from $c
 local c=$1
 declare -i entries
 local entries=$2
#  Till $entries in $names
 while [[ $c -le $entries ]]
 do
#   head -$c output's the first $c lines. Last line is of interest only, it's the cth line.
  local name="$(echo "$names" | head -$c | tail -1)"
#   File processed, proceed to next line.
  c=$c+1
  # random name generated dynamically. Run in background, it's faster
  mv -i "$name" $(cat /dev/urandom| tr -dc 'a-zA-Z0-9_' | fold -w 15| head -n 1) &
  echo "$c of $entries done." &
 done
}
# rename_till() calling mechanism.
# $till determines values of $2 in rename_till()
# Each thread will process $count/$1 lines
# This truncates decimals.
till=$(($count/$1))
while [[ $i -le $1 ]]
do
 rename_till $from $till &
 i=$i+1
 from=$from+$(($count/$1))
 if [[ $i -eq $1 ]]
 then
#   Remaining fines (truncated by $count/$1) added to last rename_till() call.
  till=$till+$(($count/$1))+$(($count%$1))
 else
  till=$till+$(($count/$1))
 fi
done