Skip to content

Instantly share code, notes, and snippets.

@alexfornuto
Last active August 29, 2015 14:09
Show Gist options
  • Save alexfornuto/35b8ca4275fbf7002c59 to your computer and use it in GitHub Desktop.
Save alexfornuto/35b8ca4275fbf7002c59 to your computer and use it in GitHub Desktop.
addAlphaDir.sh
#!/bin/bash
echo "Please provide the full path to the directory to organize: "
read path
touch ~/.addAlphaDir
while read -r ~/.addAlphaDir
do
if [$line_in == $path]; then
echo "You've already run this script on that directory. If you do it again, you'll create nested directorys, and have a bad time. \n"
exit
else
echo "Cool, man. \n"
fi
done
pushd "$path"
for ch in {a..z}
do
mkdir new"$ch"
mv "$ch"* new"$ch"
mv "${ch^^}"* new"$ch"
mv new"$ch" "$ch"
echo "$path" >> ~/.addAlphaDir.log
done
@Hitman666
Copy link

Hi Alex,

this is what works for me:

!/bin/bash

echo "Please provide the full path to the directory to organize: "

read path

while read line
do
if [ $line == $path ]; then
echo "You've already run this script on that directory. If you do it again, you'll create nested directorys, and have a bad time. \n"
exit
else
echo "Cool, man. \n"
fi
done <.addAlphaDir.log

pushd "$path"

for ch in {a..z}
do
mkdir new"$ch"
mv "$ch"* new"$ch"
mv "${ch^^}"* new"$ch"
mv new"$ch" "$ch"

    echo "$path" >> ~/.addAlphaDir.log

done

@alexfornuto
Copy link
Author

Sorry, GitHub mangled your formatting making it hard to read with proper syntax. In your version, where is 'while read line' pulling 'line' from?

@Hitman666
Copy link

Hi Alex,

Yes, sorry for that. Here's an updated version:

#!/bin/bash

echo "Please provide the full path to the directory to organize: "

read path

while read line
do
        if [ $line == $path ]; then
                echo "You've already run this script on that directory. If you do it again, you'll create nested directorys, and have a bad time. \n"
                exit
        else
                echo "Cool, man. \n"
        fi
done <.addAlphaDir.log

pushd "$path"


for ch in {a..z}
do
        mkdir new"$ch"
        mv "$ch"* new"$ch"
        mv "${ch^^}"* new"$ch"
        mv new"$ch" "$ch"

        echo "$path" >> ~/.addAlphaDir.log

done

So, basically it reads from a file .addAlphaDir.log which you can see in the code "done <.addAlphaDir.log". Why this kind of reading? I found it here: http://stackoverflow.com/questions/4642191/read-line-by-line-in-bash-script. Also, I found out the the "if" statement was wrong, as it needed spaces after the starting bracket and before the ending bracket (which you can see here: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html)

@Hitman666
Copy link

Well Alex, you never did get back to me if this helped you or not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment