-
-
Save washere/78817fc4d39b20ea2fef8e827a215a98 to your computer and use it in GitHub Desktop.
rename each text file according to its first line of text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is a fork of dylan-k code. Only the actual short line to rename inside a loop is changed to a long line. | |
#Original code gave errors on some special characters. For example if a back slash \ was in the first line to be used as new name. | |
#Those files were not renamed. This new line ignores those problematic characters and even more, using alphanumeric naming. | |
#Many ways to do alpnum renaming are suggested but most actually fail in many cases and bashes and distros. | |
#This new forked way, alpnum renaming will not fail. | |
#dylan-k: A shell script that will rename all the text files in a directory | |
#each file will be named with the first line of text from that file | |
for file in * | |
do | |
# Avoid renaming diretories! | |
if [ -f "$file" ] | |
then | |
newname=`head -1 $file` | |
if [ -f "$newname" ] | |
then | |
echo "Cannot rename $file to $newname - file already exists" | |
else | |
mv "$file" $(echo "$newname.txt" | sed -e 's/[^A-Za-z0-9._-]/_/g') | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment