You are not logged in.

#26 2019-12-14 01:12:22

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,969
Website

Re: [WIP] Sort the Downloads folder (sort.sh)

^While it's probably good practice to avoid putting spaces in filenames, as you've found files that come from elsewhere are outside your control. For this reason it's even more important to write scripts which can handle such names. Quoting of variables, use of null delimiters... plenty of testing with nasty filenames, lots of googling, it's more work, but worth it in the end.

Oh, maybe rather than grabbing a whole glob and running it through a series of piped commands all at once, as in

find_files=$(file --mime-type $HOME/Downloads/* | grep text | awk '{print $1}' ORS=' ' | tr -d {[:]})

it might be easier to use a "for" loop, so you can focus on each file individually, something like:

for file in $HOME/Downloads/*
do
    type=$( file --mime-type --brief "$file" )
    [[ $type = <what I want> ]] || continue
    < add "$file" to a list, or just mv it right away >
    < whatever... >
done

...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Online

#27 2019-12-14 01:55:22

clusterF
Member
Registered: 2019-05-07
Posts: 539

Re: [WIP] Sort the Downloads folder (sort.sh)

Ok thanks for that johnraff, i will have to play around with this a bit more.

Offline

#28 2019-12-14 11:53:58

clusterF
Member
Registered: 2019-05-07
Posts: 539

Re: [WIP] Sort the Downloads folder (sort.sh)

something like this for text files?

#!/usr/bin/env bash

for file in $HOME/Downloads/*
do
    type=$( file --mime-type --brief "$file" )
    [[ $type = text/plain ]] || continue
    mv $file $HOME/Documents/    
done

how would this be implemented to cater for multiple file types in the same script though, eg audio/mpeg, application/zip etc etc? I like functions so my first thought would be to put that script in separate functions and call them from a menu?

edit: answering my own question maybe.

#!/usr/bin/env bash

sort_folder=$"$HOME/Downloads/*"

docs () {
	for file in $sort_folder
do
    type=$( file --mime-type --brief "$file" )
    [[ $type = text/plain ]] || continue
    mv $file $HOME/Documents/

done
}

echo "Bash Menu To Clean Up The Downloads Folder"
PS3='Please enter a number of your choice: '
options=("Documents" "Music" "Videos" "Pictures" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Documents")
            docs
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

Last edited by clusterF (2019-12-14 12:23:56)

Offline

#29 2019-12-14 12:16:08

damo
....moderator....
Registered: 2015-08-20
Posts: 6,047

Re: [WIP] Sort the Downloads folder (sort.sh)

Although I would use something like "f" or "FILE" instead of "file", which is a reserved word. Just in case.


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#30 2019-12-14 12:25:51

clusterF
Member
Registered: 2019-05-07
Posts: 539

Re: [WIP] Sort the Downloads folder (sort.sh)

damo wrote:

Although I would use something like "f" or "FILE" instead of "file", which is a reserved word. Just in case.

camel case might be better perhaps?

Offline

#31 2019-12-14 15:20:46

damo
....moderator....
Registered: 2015-08-20
Posts: 6,047

Re: [WIP] Sort the Downloads folder (sort.sh)

clusterF wrote:

...
camel case might be better perhaps?

Stand well clear while the haters weigh in!  Some style guides say not to use camelCase in bash scripts.


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#32 2019-12-15 03:00:58

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,969
Website

Re: [WIP] Sort the Downloads folder (sort.sh)

What damo said about "file".  big_smile

And don't forget to quote variables if you want spaces in filenames to work:

mv "$f" "$HOME/Documents/"

(Inside [[ ... ]] it's not usually necessary, but doesn't hurt.)

If you wanted to get fancy about destinations for different file types you could make an associative array.
(man bash smile )
eg

declare -A destination
destination=([text/plain]="$HOME/Documents" [audio/mpeg]="$HOME/Music" ) # etc
for f in "$HOME/Downloads"/* # don't quote the blob!
do
    type=$( file --mime-type --brief "$f" )
    [[ -n ${destination[$type]) ]] || continue
    mv "$f" "${destination[$type]}"
done

But this is completely untested.

I recommend the package shellcheck and use it all the time. Once installed, you can get a "Lint" (or possibly other name) item in geany's "build" menu.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Online

#33 2019-12-15 04:23:38

clusterF
Member
Registered: 2019-05-07
Posts: 539

Re: [WIP] Sort the Downloads folder (sort.sh)

@ damo, well im not a very stylish person to begin with  wink. Google style guide?

@ johnraff, associative arrays looks interesting. Thanks again.

Offline

Board footer

Powered by FluxBB