copy files to other folder - find

0

I have to copy files to folder with name with their type using command 'find'. There are a lot of files with a lot of folders. I tried command:

find ./find -type f -exec bash -c ' file -b "$1"|cut -d " " -f 1 ' none {} \;

But I don't know how I can use mkdir to make folder, when I use "|".

I think about two command. First make the folders from type of files. Second copy files to this folder.

But how I can make this folders.

diego9403

Posted 2015-08-22T05:29:56.033

Reputation: 807

What is the benefit of using find here? Why not use rsync instead to sync content from one folder to another?

– JakeGould – 2015-08-22T06:03:47.607

This is task from studies and we didn't use rsync. – diego9403 – 2015-08-22T06:23:38.583

I found solution:' find ./find -type f -exec bash -c ' file -b "$1"|cut -d " " -f 1 |awk -f aw' none {} ;|xargs mkdir' but I can't copy this file. My command: 'find ./find -type f -exec bash -c ' file -b "$1"|cut -d " " -f 1 |awk -f aw|cp "$1" ' none {} ; ' And error: cp: omitting directory `find/PNG/' – diego9403 – 2015-08-22T09:22:21.443

I am trying copy files to another folder, but I can't. Command:

find ./find -type f -exec bash -c ' file -b "$1"|cut -d " " -f 1 |awk -f wa|xargs cp "$1" ' none {} ;

But I only see error:

cp: ./find/PDF/20163.32630.27874' andfind/PDF 20163.32630.27874' are the same file – diego9403 – 2015-08-22T15:00:57.420

Answers

0

Using your file-cut expression to determine the directory name:

find . -type f -exec bash -c 'd="../$(file -b "$1"|cut -d " " -f 1)"; mkdir -p "$d"; cp "$1" "$d" ' none {} \;

How it works

  • d=../$(file -b "$1"|cut -d " " -f 1)

    This finds the name of the directory corresponding to the file's type. I added ../ so to put these under the parent directory. You may want to put them somewhere else.

  • mkdir -p "$d"

    This creates the directory if it doesn't already exist.

  • cp "$1" "$d"

    This copies the file to the directory.

John1024

Posted 2015-08-22T05:29:56.033

Reputation: 13 893