Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move files to directories based on extension

Tags:

linux

shell

I am new to Linux. I am trying to write a shell script which will move files to certain folders based on their extension, like for example in my downloads folder, I have all files of mixed file types. I have written the following script

mv *.mp3 ../Music
mv *.ogg ../Music
mv *.wav ../Music
mv *.mp4 ../Videos
mv *.flv ../Videos

How can I make it run automatically when a file is added to this folder? Now I have to manually run the script each time.

One more question, is there any way of combining these 2 statements

mv *.mp3 ../../Music
mv *.ogg ../../Music

into a single statement? I tried using || (C programming 'or' operator) and comma but they don't seem to work.

like image 239
cyberpirate92 Avatar asked Jun 27 '13 03:06

cyberpirate92


People also ask

How do I move a file to a different directory?

Right-click the file or folder you want, and from the menu that displays click Move or Copy. The Move or Copy window opens. Scroll down if necessary to find the destination folder you want. If you need to, click on any folder you see to access its subfolders.

How do I move a file from one directory to another in Linux shell script?

To move a file or directory from one location to another, use the command mv. Common useful options for mv include: -i (interactive) — Prompts you if the file you have selected overwrites an existing file in the destination directory. -f (force) — Overrides the interactive mode and moves without prompting.

Can directories have extensions?

No. A directory does not have an extension like a file.

How do I move 100 files from one directory to another in Linux?

The mv command is a command line utility that moves files or directories from one place to another . It supports moving single files, multiple files and directories. It can prompt before overwriting and has an option to only move files that are new than the destination.


4 Answers

There is no trigger for when a file is added to a directory. If the file is uploaded via a webpage, you might be able to make the webpage do it.

You can put a script in crontab to do this, on unix machines (or task schedular in windows). Google crontab for a how-to.

As for combining your commands, use the following:

mv *.mp3 *.ogg ../../Music

You can include as many different "globs" (filenames with wildcards) as you like. The last thing should be the target directory.

like image 68
AMADANON Inc. Avatar answered Oct 19 '22 16:10

AMADANON Inc.


Two ways:

  1. find . -name '*mp3' -or -name '*ogg' -print | xargs -J% mv % ../../Music
  2. find . -name '*mp3' -or -name '*ogg' -exec mv {} ../Music \;

The first uses a pipe and may run out of argument space; while the second may use too many forks and be slower. But, both will work.

like image 22
hd1 Avatar answered Oct 19 '22 16:10

hd1


Another way is:

mv -v {*.mp3,*.ogg,*.wav} ../Music
mv -v {*.mp4,*.flv} ../Videos

PS: option -v shows what is going on (verbose).

like image 5
Regis Barbosa Avatar answered Oct 19 '22 17:10

Regis Barbosa


I like this method:

#!/bin/bash                                                                                                                                                                                                 

for filename in *; do
  if [[ -f "$filename" ]]; then
      base=${filename%.*}
      ext=${filename#$base.}
    mkdir -p "${ext}"
    mv "$filename" "${ext}"
  fi
done
like image 5
Richard L Avatar answered Oct 19 '22 17:10

Richard L