Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to move media and other files which are in a specified directory to another directory, which will be created if it does not exist. And, I'm trying to create a directory where the remaining files with different extensions will go. My first problem is that my script is not making a new directory and it is not moving the files to other directories. How can I move files with different extensions to one directory?

This is what i have had so far. Please correct me where I'm wrong and help modify my script:

To=/home/katy/mo #directory where the media files will go
WA=/home/katy/do # directory where the other files will go
 if [ ! -d "$To" ]; then
   mkdir -p "$To"
cd $From
find . -type f -name"*.mp4" -exec mv {} $To \;
share|improve this question
A better place for this question would be StackOverflow – Nitin Venkatesh Dec 11 '11 at 12:55
up vote 2 down vote accepted

Try this:

# Call this script with the extensions you want to move.
# E.g.,     ./script mp4 avi flv
To="/home/katy/mo" #directory where the media files will go
WA="/home/katy/do" # directory where the other files will go
 if [[ ! -d "$To" ]]; then
   mkdir -p "$To"
cd "$From"
for i in "$@"; do # You could also hard code this: for i in mp3 mp4 avi; do
  find . -type f -name "*.${i}" -exec mv "{}" "$To" \;

{ and } are special characters. I think that adding a bunch of quotes to your script will help make it clearer and work better.

share|improve this answer
thanks its working though im stack on one thing how can i move file like .mp3,.mp4.avi to the same folder using one command or using the same script – kickev Dec 11 '11 at 11:16
thanks man it worked perfectly:) – kickev Dec 11 '11 at 13:15
hey scott i need a favour again on this same script i want to modify so that it parses parameters,My first problem is that I want when I put -d and full path on the terminal it should move only media files, -l and full path to move all text files, then -x to change the extension to uppercase, then -u to lowercase. – kickev Dec 17 '11 at 19:07
-m path Move media files -t path Move text files -u Change extensions to upper-case -l Change extensions to lower-case – kickev Dec 17 '11 at 19:25
I suggest you read up on getopt. Or better yet, switch languages to something like Ruby or Python, as this is more complex than I like to do in Bash. Also, remember that you'll have to use functions for something like this. I don't have time to write the code for you, as it'll take significantly more code, but I'm willing to help you debug the code you write. – Scott Severance Dec 18 '11 at 1:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.