Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Stuck on a shell script.

Name: !L33tUKZj5I 2012-07-09 18:45

Hello, /prog/

Think I'll be able to figure this out eventually, but I've called it a day for now.

I have a folder that is so full of picture files, it takes forever to open in a normal file viewer. So I wanted to create a script that made a directory for each letter of the alphabet or number, then shift all the pictures that start with that letter or number into these directories, so that when I want to find a file I don't have to wait an ice age for the main folder to load and I can just click through to where I know it is.

I managed that part fine.

Some files though start with a space character, or a hyphen for some reason. I wanted to put these in a folder called "others".
These are giving me some gyp (other special characters seem to be fine so far) because when the ones that start with a space are fed into awk, awk works on fields so just starts from where the letters start instead of the space and doesn't strip out the proper filename. Fuck knows why "-" causes trouble too, I'm not really that versed in bash at all.

This is is the script anyway. It's the last line that isn't quite right...although it works fine for files that start with an underscore or an exclamation mark or something...


    #directory tidy program
    exec 1>/dev/null 2>/dev/null;

    # files starting lowercase
    for count in {a..z}

    do
    mkdir $count;
    mv "$count"* $count;

    done;

    # files starting uppercase
    for count in {A..Z}
    do
    mkdir $count;
    mv "$count"* $count;
    done

    #files starting with numbers
    for count in {0..9}

    do
    mkdir $count;
    mv "$count"* $count;

    done

    # special case files
    mkdir others;
    ls -n| grep -v ^d|awk '{print "mv \""$8"\" others"}'>tempfile.sh;chmod 777 tempfile.sh;./tempfile.sh;rm tempfile.sh;




Fuck if I know what to do.

Name: Anonymous 2012-07-09 18:58

Just move all the files left in the directory "others"?
Also, avoid the word "folder", some people will cringe

Name: Anonymous 2012-07-09 18:58

have you read your sicp today?

Name: !L33tUKZj5I 2012-07-09 19:01

>>2
It's not that simple, if I try to move everything left into the folder marked others it will move all the directories I've created too, hence the convoluted awk line to strip out a list of files in the directory. It's buggering up on some special characters though.

Name: !L33tUKZj5I 2012-07-09 19:09

Well I'll be damned, someone somewhere else managed to help me. You change that long awk line to this.

find . -maxdepth 1 -type f -exec mv {} others \;

and it works fine.

Thread closed.

Name: 3 2012-07-09 19:16

LATE SUBMISSION
#!/bin/bash
set -e
set -o pipefail

mkdir "target"

find -maxdepth 1 -print0 | while IFS= read -r -d '' f; do
    if [ "x$f" = "x./target" -o "x$f" = "x." ]; then
        continue
    fi
    c="$(basename "$f")";c="${c:0:1}"
    mkdir -p "target/$c/"
    mv "$f" "target/$c/"
done

Don't change these.
Name: Email:
Entire Thread Thread List