2 Replies - 2327 Views - Last Post: 11 January 2013 - 09:18 AM Rate Topic: -----

#1 glenak1911  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 16-May 11

Shell Script to navigate sub-directories and move directories

Posted 10 January 2013 - 09:28 PM

Hi guys, I currently have a Ubuntu Server set up that I'm using as a media server. I have movies and music that transfer over to the server in a general "downloads" folder. Then I move the folders over to the respective areas, Music files to the music folder, Videos to the video folder, etc. I get tired of manually moving folders and would like to automate this task a bit. I don't have much experience writing shell scripts, but I started writing a bash script for this task, but alas, it doesn't work! Here is what I have so far:

echo "Starting..."
cd /home/glen/downloads
ls -l
for directory in /home/glen/downloads
	for file in directory
			if [ "${file}" == "*.mp3" ] || [ "${file}" == "*.flac"]
				cp directory /home/glen/Music
			else if [ "${file}" == "*.avi" ]||[ "${file}" == "*.mkv" ]||[ "${file}" == "*.mp4" ]
				cp directory /media/sdb/Videos
echo "Finished!"
cd $CWD

If anyone could point me in the right direction I would be very grateful!

Is This A Good Question/Topic? 0
  • +

Replies To: Shell Script to navigate sub-directories and move directories

#2 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon

Reputation: 11040
  • View blog
  • Posts: 18,852
  • Joined: 19-March 11

Re: Shell Script to navigate sub-directories and move directories

Posted 10 January 2013 - 10:19 PM

Used to be I'd reach for perl for a job of this sort. Lately, I've been enjoying python a lot, so here's a sketch of how I'd do it in python. It's actually very simple.

extension = ".mp3"
destdir = "/home/glen/music"
srcdir = "/home/glen/downloads"

import os

#  this gives me some tools I need: listdir and rename

l = os.listdir(srcdir)

#  This gets a list of files in the source directory and stores it as l

movelist = filter(lambda file: file.find(extension)>=0, l)

#  This is a little tricky: it creates a secondary list, of those files in l which contain the string 
#  ".class" - that is, the filenames for which find(".class") returns a positive number
for file in movelist:
    os.rename(file, destdir+file)

#  This renames each file in that second list, moving it into the subdirectory. 

The only tricky part is the filter function. This takes as an argument a function and a list. The function takes one input and returns a boolean value: the returned list consists of those members of the source list for which the function returns "true" - in this case, those filenames which contain the extension we're looking for.

Hope this helps a little - although it's not the nicest code I've ever written, it should work to move some files, and you can learn a bit of python making it work more to your needs.
Was This Post Helpful? 0
  • +
  • -

#3 GWatt  Icon User is offline

  • member icon

Reputation: 307
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: Shell Script to navigate sub-directories and move directories

Posted 11 January 2013 - 09:18 AM

You should probably look into the rsync utility. It will do intelligent directory transfers across machines.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1