Find the answer to your Linux question:
Results 1 to 6 of 6
Hello All, I need help creating a script that will take user input in the form of a file being dropped on the script, look at the file type (by ...
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90

    Script that accepts a file as input, and sorts it

    Hello All,

    I need help creating a script that will take user input in the form of a file being dropped on the script, look at the file type (by extension will do) and sort it into the directory tree based on a set of rules. I get the idea from the Plasmoid Magic Folder. Since I don't run KDE, that seems like an equivalent way to handle it.

    I need help on finding a way to have the script launch with the file dropped on it being the input (can this work with multiple files with multiple extensions?)

    also, will this work for the sorter? (veeeery rough, just trying to lay down a template):

    if [ $filetype -eq *.txt ];
    then mv here
    elif [ $filetype -eq *.jpg ];
    then mv there
    fi.

    If accepting multiple dropped files does not work, can a script be made that just looks at all the files in the directory the script is sitting in when ran (say the desktop), and just moves the ones it is scripted to know how to move?

  2. #2
    Just Joined!
    Join Date
    May 2007
    Posts
    72
    Why not make a sandbox directory with a few dummy files and experiment?

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    Thats all good, but to experiment with a script, i need to write it first. I dont know how to make a script take a file as input when the file is dropped on the scrpit's icon.

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I don't know how to have the file given as input when it is dropped on your script graphically. My guess is that the name of the file is passed in as the first argument, but I can't prove that. Some research on the Gnome website (or possibly Nautilus itself?) would probably answer that, assuming you're using Gnome.

    In any case, your if statement will not work. In Bash, "-eq" is for numbers, first of all, and also, you can't check equality against a glob. What I think that you actually want is to extract the extension:
    Code:
    extension=$(echo "$filetype" | sed -re 's/.*\.(.*)$/\1/')
    This code will store into the variable $extension whatever occurs after the last '.' character in $filetype. Alternatively, if you want whatever occurs after the first '.' in the name, you can use:
    Code:
    extension=$(echo "$filetype" | sed -re 's/[^.]*\.(.*)$/\1/')
    Does this help?
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    yeah, actually it does. I also found that you can do a

    find ~/Desktop/ -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \) -exec mv {} ~/Pictures/ \;

    to get all common pictures. I just post a bunch of these with different file types into a bash script, I put a shortcut on my top panel, and click it to execute. qucik and dirty, but effective. The only thing is, it does ALL the files on the desktop, not only the ones I want, so I do a "-maxdepth 1 -filetype f", so I can put some files in a "junk" folder before I run. With those two, it ignores non-files and folders.

  6. #6
    Just Joined!
    Join Date
    Nov 2006
    Posts
    90
    This is kinda stupid, but it actually works. I named it housekeeping.sh, put it in my documents folder, created a shortcut on the panel that was set to run in terminal, and just run it from there.

    Use caution if you use my script exactly as follows, I tend to throw away windows executables when Im done with them, and the script reflects that. It also throws away 0 byte files and .torrent files.

    Code:
    #!/bin/bash
    set -e
    clear
    echo "Sorting Desktop, Please be patient"
    
    find ~/Desktop/ -maxdepth 1 -type f -iname "*" -empty \
    	-exec mv {} ~/.local/share/Trash/files/ \; 2> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f -iname "*~" \
    	-exec mv {} ~/.local/share/Trash/files/ \; 2>> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \) \
    	-exec mv {} ~/Pictures/ \; 2>> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f \( -iname "*.pdf" -o -iname "*.doc" -o -iname "*.rtf" -o -iname "*.ott" -o -iname "*.ots" \
    	 -o -iname "*.txt" -o -iname "*.odf" -o -iname "*.dot" -o -iname "*.odt" \) \
    	-not -name "housekeepersnotes.txt" -exec mv {} ~/Documents/ \; 2>> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f \( -iname "*.sh" -o -iname "*.bash" -o -iname "*.shell" -o -iname "*.script" \) -not -name "Cleanup.sh" \
    	-exec mv {} ~/Documents/Scripts/ \; 2>> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f \( -iname "*.mp3" -o -iname "*.m3u" -o -iname "*.ogg" -o -iname "*.wma" \) \
    	-exec mv {} ~/Music/unsorted/ \; 2>> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f \( -iname "*.flv" -o -iname "*.wmv" -o -iname "*.mpg" -o -iname "*.mp4" -o -iname "*.mpeg" \) \
    	-exec mv {} ~/Videos/ \; 2>> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f \( -iname "*.gz" -o -iname "*.tar.gz" -o -iname "*.gzip" -o -iname "*.zip" -o -iname "*.7zip" -o -iname "*.7" \
    	-o -iname "*.7z" -o -iname "*.tar" -o -iname "*.deb" \) \
    	-exec mv {} ~/zips/ \; 2>> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f \( -iname "*.ISO" \) \
    	-exec mv {} /repository/ISOs/ \; 2>> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f \( -iname "*.exe" -o -iname "*.dll" -o -iname "*.com" -o -iname "*.js" -o -iname "*.torrent" \) \
    	-exec mv {} ~/.local/share/Trash/files/ \; 2>> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f -iname "*" -not -name "Cleanup.sh" -not -name "housekeepersnotes.txt" \
    	-exec mv {} /home/kainalu/Stuff\ im\ working\ on/sweep-up/ \; >> ~/Desktop/housekeepersnotes.txt
    
    find ~/Desktop/ -maxdepth 1 -type f -iname "housekeepersnotes.txt" -empty \
    	-exec rm {} \; 2> ~/Desktop/housekeepersnotes.txt
    
    
    sleep 1s
    clear
    echo
    echo
    echo "All done! If the housekeeper had any notes for you,"
    echo "they were left on your desk. Have a nice day!"
    sleep 2s
    exit 0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...