SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2009-05-11 17:07:27

Linzt
Member
From: England
Registered: 2009-04-21
Posts: 35

Bash script newbie...

Idiot alert. You're going to have to laugh at this one, but go easy and point me at where I went wrong. For those experienced with bash scripting which is going to be a significant quantity of users here, I guess that I don't need to tell you what I've done. Which is brilliant because I don't actually know what I've done I just know it isn't good because now I can't use Thunar anymore and now I have a ridiculous amount of folders in my $HOME directory (I can see them under obmenu > places)...

Anyway, this is what has got me into this little pickle: Every time that I download something from the 'net, FF will save it (maybe an *.ogg/*.mp3 from Linuxoutlaws podcast archives, et cetera) to my $HOME directory - after all I told it to. "So what" you say - "that's fine." Well, no it isn't because my HD is partitioned in such a backward way due to how I faffed about with it a couple of months ago when I didn't have a clue about Linux (well I still don't...) and an equally aloof knowledge of good partitioning practice that I'd much rather have my downloads automatically sort to my DATA partition. Moreover, I could ask FF to ask me where to save files to but as you might agree, that would be tedious.

So to cut a long story short, I want/wanted a bash script that would automatically transfer my downloaded files from my $HOME directory across to my /media/DATA partition and into directories based on their file extension.

Example: A) I've downloaded a .pdf B) FF saves it to my $HOME directory C) my bash script ('next time a terminal is loaded) sorts my files based on their extensions and moves them to an appropriate folder in the /media/DATA partition.

I still want a script which'll do this but for now I'd like someone to help me undo what has just been done by the following script neutral

#!/bin/bash
# pwd = print working directory
# 

FILEDIR=/home/[USR]

# List file extensions and create directories
echo Files found in $( pwd )
for EXTENSIONS in $( ls -a | grep -v ^d | grep [.][a-zA-Z0-9] | cut -d. -f2 ); do
    if [ -d $FILEDIR/$EXTENSIONS ]; then
        echo "$EXTENSIONS found in $FILEDIR, not created"
    else
        mkdir -p /media/DATA/$EXTENSIONS
        echo "Directory created: /media/DATA/$EXTENSIONS"
    fi
done

# Start moving files into dirs
echo Moving files into directories...
for FILES in $( ls -a | grep -v ^d | grep [.][a-zA-Z0-9] ); do
    # Find the file's extention
    EXTENSION=$( ls $FILES | cut -d. -f2 )

    i=0
    if [ -e $FILEDIR/$EXTENSION/$FILES ]; then
        echo "File $FILES already exists, renaming..."
        i=$[$i+1]
        #$FILES=$i.$FILES
        mv $FILES $i.$FILES
    else
        mv $FILES $FILEDIR/$EXTENSION
        echo "$FILES moved to $FILEDIR/$EXTENSION"
    fi
done

Could someone also clarify for me what this did (I have a very rough idea) as well as a suggestion to put it right after a suggestion on how to reverse what it's done plus get Thunar working again.

Many thanks roll

Last edited by Linzt (2009-05-11 17:10:39)


I don't have a sig...

Offline

Be excellent to each other!

#2 2009-05-11 17:43:04

Toolz
#! Die Hard
From: Asia
Registered: 2009-02-02
Posts: 937

Re: Bash script newbie...

Linzt wrote:

Moreover, I could ask FF to ask me where to save files to but as you might agree, that would be tedious.

You set the preference and then after that doesn't it always remember your last location? Does Flashgot help? I think by default it uses wget and you can control behaviour through that.

Offline

#3 2009-05-11 17:49:35

Erik
#! CrunchBanger
From: Fort Worth, TX
Registered: 2009-03-28
Posts: 192

Re: Bash script newbie...

Why don't you just open firefox and go to 'edit', 'preferences', click on the 'main' tab and give in your DATA folder as place to save your files?

Offline

#4 2009-05-11 17:52:17

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,418

Re: Bash script newbie...

I can't help with your script but I would consider the Download Sort extension for Firefox.

Offline

#5 2009-05-11 18:06:39

Linzt
Member
From: England
Registered: 2009-04-21
Posts: 35

Re: Bash script newbie...

Erik wrote:

Why don't you just open firefox and go to 'edit', 'preferences', click on the 'main' tab and give in your DATA folder as place to save your files?

Lol - I just didn't.

I was thinking along the lines of different file types being moved into respective folders on my DATA partition. If I did things as in your suggestion, I'd find myself manually sorting = tedious.

Thanks Anon, I'll check that out.

EDIT:

Thunar now working...

Last edited by Linzt (2009-05-11 18:12:13)


I don't have a sig...

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo