Skip to content

Instantly share code, notes, and snippets.

@hbeale
Last active August 26, 2016 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hbeale/91dc35a75550322b8051c453d7dd7f1d to your computer and use it in GitHub Desktop.
Save hbeale/91dc35a75550322b8051c453d7dd7f1d to your computer and use it in GitHub Desktop.
Renaming strategy for cautious renamers
#################################
###
### Cautiously RENAME a comma delim list of files
###
#################################
### ASSIGN variable to input file
thisKey="Key_Renaming_26Aug2016_hb2.txt"
### All functions require a file containing renaming information
## The input file should be a plain text file containing a full source path and file name location followed by a comma and the full destination path and file name
# example line from input file:
# /home/bonzo/oldFileName.txt,/home/ballet/dogs/newFileName.txt
# note: new directories are not created
## Gotchas to look out for
# Any line that does not contain a file source and destination must start with a pound symbol (#).
# If you are exporting from excel, try "export csv (windows)" instead of MS-DOS. Exporting as CSV MS-DOS can generate line breaks incompatible with linux. If nothing happens when you run the command, it can be because the content is viewed as a single line.
# If you are exporting from excel, make sure there is no content in cells beneath the current lines. Excel likes to export extra lines.
#################################
###
### STEP 1: check that source files exist
###
#################################
#
# Requires:
# variable "$thisKey" containing the name of file describing the files to be renamed
#
# Returns:
# text file named *.sourceExistenceTestResults.txt indicating whether each source file is absent or found
#
# Purpose:
# Review the output to make sure that source files have been correctly specified
cat $thisKey | grep -v ^# | while read LINE
do oldName=${LINE/,*}
if [ ! -f $oldName ]
then echo "absent: " $oldName
else echo "found: " $oldName
fi
done > ${thisKey}.sourceExistenceTestResults.txt
#################################
###
### FUNCTION: check whether destination files ALREADY exist
###
#################################
#
# Input:
# set variable with name of file containing renaming contents
#
# Returns:
# text file named *.destinationExistenceTestResults.txt indicating whether each source file is absent or found
#
# Purpose:
# Review the output to make sure that destination files have been correctly specified
cat $thisKey | grep -v ^# | while read LINE
do newName=${LINE/*,}
if [ ! -f "$newName" ]
then echo "absent: " $newName
else echo "found: " $newName
fi
done > ${thisKey}.destinationExistenceTestResults.txt
#################################
###
### FUNCTION: rename files with logging (yet to come)
###
#################################
#
# Input:
# set variable with name of file containing renaming contents
#
# Returns:
# text file named *.RenamingResultsLog.txt indicating whether each file was copied or not
#
# Purpose:
# Review the output to make sure that source files have been correctly moved and address any errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment