views:

95

answers:

3

I have some video files all ending in .wmv .mov .mpg. I have music files ending in .mp3 .wma. And finally I have some text files all end in the extension .txt

I wrote a shell script that generates subfolders, one for the music files, one for the video files, and one for the text files, and then organizes all of the files into the correct subfolders. But I ran into a little problem ...

*** I would like the script to be interactive and to prompt the user if he/she wants to organize the files. Also, I would like the script to write a log file that contains, for each file, the original file name, as well as the new file path/name it was moved to. And I would like the script to accept a command line argument, which is the folder that contains the unorganized files. This should allow the script to be located and run from anywhere in the file system, and accept any folder of unorganized files.

Example:

organizefiles.sh mystuff/media

where the subfolders would go inside "media"

Any ideas on how to do that? Thank you!

+2  A: 

I would recommend switching to python, much easier to write and maintain such a code.

As for bash:

  1. Reading input: read
  2. Reading command line arguments: see here
  3. Writing to a log file, simply do echo "something" >> mylogfile.log
Am
Thank you for the links, they were very useful!
jualin
A: 

Here is the script I have so far

#!/bin/bash
mkdir movies
mkdir songs
mkdir textfiles
mv *.wmv movies
mv *.mov movies
mv *.mpg movies
mv *.mp3 songs
mv *.wma songs
mv *.txt text
ls -l movies, >> log.txt
ls -l songs >> log.txt
ls -l textfiles >> log.txt
jualin
You should post follow-ups as comments or by editing the original question.
Dennis Williamson
I am sorry, thank you!
jualin
+3  A: 

here's a partial implementation for you to start with. Try and do the rest yourself.

find /path -type f \( -iname "*.mp3" -o -iname "*.txt" \) -exec file -N "{}" + | while IFS=":" read -r filename type
do
    case "$type" in
        *[vV]ideo*|*AVI* ) echo "Video: $filename";;
        *[Aa]udio*|*MPEG*ADTS*) echo "Audio file: $filename";;
        *[Aa]scii*[tT]ext*) echo "Text: $filename" ;;
        * ) echo "No type: $filename-> type: $type";;
    esac
done

Please read bash tutorial or this to get familiarize with shell scripting.

ghostdog74
You are overly kind, sir. This is like a mini tutorial itself!
codebliss