Register
It is currently Wed Jul 30, 2014 9:21 pm

Very noob script to move file types to various destinations


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Jul 23, 2011 11:36 pm   

Joined: Sat Jul 23, 2011 10:41 pm
Posts: 2
Ok, I have this script that I have been practicing with. What it does, is simply looks in my downloads folder and looks for (*.filetype)

Then, it will distribute each file type to specific destinations ie. html to Documents, or mp3 to Music etc.

The script works, but I still get errors such as:
Quote:
mv: cannot stat `/Downloads': No such file or directory


If no file types exist I get similar errors in the shell, but if they do, they are sent to the destination directory, but I still get the above error in the quote.

Here is the script:
Code:
#! /bin/bash

   files=$1

      files=$(ls /home/dude/Downloads/*.pdf);
      files=$(ls /home/dude/Downloads/*.mp3);
      files=$(ls /home/dude/Downloads/*.html);
      
   if [[ $1 != 0 ]]

         then
      
   cd /home/dude/Downloads && mv *.pdf /Downloads /home/dude/Documents;
                  
    mv *.mp3 /Downloads /home/dude/Music;
                  
    mv *.html /Downloads /home/dude/Documents;
 
         else
      
      echo "No such file types exist in this directory"

   fi


Top
 Profile  
 PostPosted: Sun Jul 24, 2011 9:30 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
You get this error because « /Downloads » doesn't exist. It's « Downloads/ ».
( Assuming you ran the script from your home directory. )

And mv(1)'s syntax is: [file(s)] [dir], not [files] [dir from] [dir to].

------

Just loop through the files in the directory and use a case(0) statement.

Never parse ls(1). It's not intended to be parsed. It's just a representation of files for
human eyes only.

Code:
#!/bin/sh

for f in $HOME/Downloads/*; do
    case $f in
        *.pdf|*.html) mv -- "$f" $HOME/Documents ;;
               *.mp3) mv -- "$f" $HOME/Music
    esac
done


Or just use mv(1) twice:

Code:
mv -- $HOME/Downloads/*.pdf $HOME/Downloads/*.html $HOME/Documents
mv -- $HOME/Downloads/*.mp3 $HOME/Music


And if you want a more efficient way, you can use extglob if you have at least bash(1) 2.02-alpha1.

Code:
shopt -s extglob
mv -- $HOME/Downloads/*.@(pdf|html) $HOME/Documents
mv -- $HOME/Downloads/*.mp3 $HOME/Music


Top
 Profile  
 PostPosted: Wed Jul 27, 2011 11:08 pm   

Joined: Sat Jul 23, 2011 10:41 pm
Posts: 2
Hey, thanks a lot for showing me those variations.

I appreciate it.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP