Move Files to Directories Based on Extension

Move files to directories based on extension

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.

How do you move files of specific file extension in multiple directories, one directory deeper within their respective directories?

You can do what you need with find that searchers for all *.jpg files and a simple helper script called by the -exec option to find to create the jpg directory and move all .jpg files into the new directory.

The helper script will simply get the absolute filename utilitzing readlink -f and then using quick parameter expansion to trim the last /... component from the absolute filename to obtain the full path. Then it is simply a matter of creating the jpg directory at the end of the path and moving the file to the new directory.

Your helper script (I called it helper.sh) could be:

#!/bin/sh

test -z "$1" && exit ## validate 1 argument given or exit

full=$(readlink -f "$1") ## get full filename
dir="${full%/*}" ## get full path

test "${full##*.}" = 'jpg' || exit ## test extension is jpg or exit

test -z "$dir" && dir="/" ## check if file was in / (root)

test -d "$dir/jpg" || mkdir -p "$dir/jpg" ## check/create jpg dir at end of path

mv "$full" "$dir/jpg" ## move file into new jpg dir

(note: after creating the helper script, make sure you make it executable with chmod +x helper.sh)

Original Projects Directory Tree

$ tree Projects/
Projects/
├── Project_001
│   └── Image_001.jpg
├── Project_002
│   ├── Image_002.jpg
│   └── Image_003.jpg
└── Project_003

Your find command operating on the Projects directory calling the helper script for each file would be:

$ find Projects/ -type f -name "*jpg" -exec ./helper.sh '{}' \;

Resulting Projects Directory Tree

$ tree Projects/
Projects/
├── Project_001
│   └── jpg
│   └── Image_001.jpg
├── Project_002
│   └── jpg
│   ├── Image_002.jpg
│   └── Image_003.jpg
└── Project_003

Let me know if you have further questions.

Preserving Files Already In jpg Directory

Per-your additional comment, in order to preserve .jpg files already within a jpg directory under your Projects, all you need to do is add one additional check. If the last component of the path is already jpg, just exit the helper, e.g.

test "${dir##*/}" = 'jpg' && exit           ## if already in jpg dir, exit

Shown in context in helper.sh:

test -z "$1" && exit                        ## validate 1 argument given or exit

full=$(readlink -f "$1") ## get full filename
dir="${full%/*}" ## get full path (trim last /*)

test "${dir##*/}" = 'jpg' && exit ## if already in jpg dir, exit
test "${full##*.}" = 'jpg' || exit ## test extension is jpg or exit
...

Original Projects Directory Tree (w/existing jpg)

$ tree Projects/
Projects/
├── Project_001
│   ├── Image_001.jpg
│   └── jpg
│   └── Image_000.jpg
├── Project_002
│   ├── Image_002.jpg
│   ├── Image_003.jpg
│   └── jpg
│   ├── Image_000.jpg
│   └── Image_001.jpg
└── Project_003

Resulting Projects Directory Tree

$ tree Projects/
Projects/
├── Project_001
│   └── jpg
│   ├── Image_000.jpg
│   └── Image_001.jpg
├── Project_002
│   └── jpg
│   ├── Image_000.jpg
│   ├── Image_001.jpg
│   ├── Image_002.jpg
│   └── Image_003.jpg
└── Project_003

How to move files with specific extension from subdirectories to another directory including creating the subdirectories the files are from using CMD?

I think that ROBOCOPY will do what you want. Learn about the options using ROBOCOPY /?.

ROBOCOPY /S /MOV C:\Parent\basis C:\Parent\basis_ft *.txt

If you are ready to use PowerShell, you could do the following. When you are sure that the correct files will be deleted, remove the -WhatIf from the Remove-Item command.

Copy-Item -Path 'C:\Parent\basis' -Destination 'C:\Parent\basis_ft' `
-Filter '*.txt' -Recurse
Get-ChildItem -Path 'C:\Parent\basis' -Filter '*.txt' |
ForEach-Object { Remove-Item -Path $_.FullName -WhatIf }

python moving files based on extensions?

I would use a dict of locations and extensions, for example {'/home/xxx/Pictures': ['jpg','png','gif'], ...} Where I use the "keys" as destinations and the values are lists of extensions for each destination.

source = '/home/xxx/randomdir/'
mydict = {
'/home/xxx/Pictures': ['jpg','png','gif'],
'/home/xxx/Documents': ['doc','docx','pdf','xls']
}
for destination, extensions in mydict.items():
for ext in extensions:
for file in glob.glob(source + '*.' + ext):
print(file)
shutil.move(file, destination)

While Fabre's solution is good, you would have to repeat his double-loop solution for every destination folder, whereas here you have a triple-loop that does everything, as long as you give it a proper dict

Also a word of advice, if you write code that looks so repetitive, like yours do, be sure there is a way to make it simpler, either with a loop or a function that takes arguments.

How do I move just the files with a particular extension from nested sub-directories?

If you have bash v4 and have

shopt -s globstar

in your .profile, you can use:

mv ./sourcedir/**/*.ext ./targetdir

Move files from parent directories to subdirectories based on file extension

Run this in the top level directory:

for dir in xxx_*; do
mv "$dir"/*.{csv,jpg} "$dir"/submissionDocumentation/
done

Update : Move files from one folder to another based on file extension

Just add pathname.deleteOnExit(); on accept method

@Override
public boolean accept(File pathname) {
boolean source = pathname.getName().toLowerCase().endsWith(".csv");

if (source) {
pathname.deleteOnExit();
return true;
}
return false;
}

whole code :

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

/**
* Created by Lenovo on 02/12/2018.
*/
public class FileMove {
public static void main(String a[])

{
try {

File source = new File("C:\\Users\\sh370472\\Downloads");
File dest = new File("E:\\Query\\");

FileUtils.copyDirectory(source, dest, new FileFilter() {

@Override
public boolean accept(File pathname) {
boolean source = pathname.getName().toLowerCase().endsWith(".csv");

if (source) {
pathname.deleteOnExit();
return true;
}
return false;
}

});
} catch (IOException e) {
e.printStackTrace();
}
}
}

batch recursive move files of extension to folder named with that extension

find /music/ -name '*.mp3'  -execdir mv {} mp3/  \;
find /music/ -name '*.flac' -execdir mv {} flac/ \;

-execdir runs the specified command in the directory of the current file. This lets you use mp3/ and flac/ directories relative to the current artist/album.



Related Topics



Leave a reply



Submit