find mv and create directory structure


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find mv and create directory structure
# 1  
Old 02-06-2011
find mv and create directory structure

Hi there, I'm trying to pull all my flacs out of my Music collection. I can do it with following command
Code:
find b/ -name *.flac -exec mv {} flac/ \;

which works great except it moves all the flac files to the flac folder. I want it to recreate the original folder the flacs were found in and mv the files there. essentially recreate directory structure but only folders with flac files. Can anyone help please?
# 2  
Old 02-07-2011
So you want to move all your files
Code:
/whatever/b/*.flac

to
Code:
/whatever/newdir/b/*.flac

?
just
Code:
mkdir -p /whatever/newdir
cp -pr /whatever/b /whatever/newdir

# 3  
Old 02-07-2011
Quote:
Originally Posted by ctsgnb
So you want to move all your files
Code:
/whatever/b/*.flac

to
Code:
/whatever/newdir/b/*.flac

?
just
Code:
mkdir -p /whatever/newdir
cp -pr /whatever/b /whatever/newdir

Hi thanks for the reply. That's not exactly what I'm after. I have a directory "b/" with flac, mp3 and wma files in sub folders say:
Code:
b/artist1/foo.mp3
              b/artist2/foo.wma
              b/artist3/foo.flac

I want to search for folders containing flac files in "b/" and mv the directory containing the flac files to my new folder "flac/". tbh I don't think i explained it very well the first time Smilie
# 4  
Old 02-07-2011
Because there is much variation in unix and Linux commands, please post:
Operating System and version.
What Shell you use.

The generic answer to this question is to use "find" piped to "cpio -p" with more local parameters. See "man find" AND "man cpio".
The normal method is to copy the tree to the target, verify the copy, then delete the source files.
# 5  
Old 02-08-2011
I am sure there may be some more elegant way to do it but something like this should do the job
Code:
ksh
cd b/
find ./ -type f -name "*.flac" | while read a
do  
     b=${a#??}
     mkdir -p ./flac/${b%/*}
     mv "$a" ./flac/"$b" && rm "$a"
done


Last edited by ctsgnb; 02-08-2011 at 04:59 AM..
# 6  
Old 02-08-2011
Quote:
Originally Posted by methyl
Because there is much variation in unix and Linux commands, please post:
Operating System and version.
What Shell you use.

The generic answer to this question is to use "find" piped to "cpio -p" with more local parameters. See "man find" AND "man cpio".
The normal method is to copy the tree to the target, verify the copy, then delete the source files.
The system I'm using is a Qnap nas so i'm not exactly sure of the exact os. I can also perform any command needed from an Ubuntu 10.10 server which has the folders mounted

---------- Post updated at 10:23 PM ---------- Previous update was at 09:17 PM ----------

Quote:
Originally Posted by ctsgnb
I am sure there may be some more elegant way to do it but something like this should do the job
Code:
ksh
cd b/
find ./ -type f -name "*.flac" | while read a
do  
     b=${a#??}
     mkdir -p ./flac/${b%/*}
     mv "$a" ./flac/"$b" && rm "$a"
done

Hi thanks for the help. When i try this in Ubuntu server 10.10:
Code:
 
cd b/
find ./ -type f -name "*.mp3" | while read a
 do 
b=${a#??}
mkdir -p ./mp3/${b%/*}
mv "$a" ./mp3/"$b" && rm "$a"
done

i get a "mv: unable to rename no such file or directory" error for each file piped from find. Also after i run the above the directory i was working in has new empty folders for each word in any of the original directories titles. e.g if i had a folder
Code:
b/An Album By An Artist

i end up with the following folders
Code:
b/An/
b/Albun/
b/By/
b/An/
b/Artist/
b/An Album By An Artist
b/mp3/

Maybe this is because there is spaces in the directory names?
# 7  
Old 02-08-2011
I didn't gleam much from the manufacturers website except that you are probably using some sort of SAN.
QNAP Systems, Inc. - Quality Network Appliance Provider

Though I could come unstuck with syntax on whatever Operating System is at your command prompt, I feel that the combination of "find" and "cpio" is the correct direction:

Quote:
find b/ -name *.flac -exec mv {} flac/ \;
Idea for replacement code which keeps the directory tree and works with directory and file names containing space characters.
Code:
cd /start_point
find b/ -name '*.flac' -print | cpio -pdumv flac/

This is a copy operation. When successful, then consider deleting the original files.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a flat file and directory structure

Hi All, is there any work around to generate the file and directory structure like below at left side at Output? and exclude all file except .abc .txt Current Directory structure |-------------files |---------------Share |-----------------dir1 |-----------------dir2... (11 Replies)
Discussion started by: heros
11 Replies

2. Shell Programming and Scripting

Create Directory structure

Hello ; ) again Now I have my file like this : DIR2/DIR3 DIR2 DIR2/DIR3/DIR4/DIR5 I am looking for help to create a loop that will create the directory structure. I need something like this : If "DIR2" does not exist > Create IF "DIR2" exist already > check if onther "DIR"... (5 Replies)
Discussion started by: Aswex
5 Replies

3. Shell Programming and Scripting

How we can create the master file through shell to show the tree structure of the directory?

Can we create the master file that show the whole tree structure of the directory till a particular folder? Database that contains four sub repository Sybase,sql,oracle,mysql and sql and oracle contains two subrepostories Siebel and plsql and each repositories contains three folders... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

4. Shell Programming and Scripting

find size, cp breaks directory structure

I'm using this now: find /some/path/with/sourcefiles -type f -size -7M -exec /bin/cp -uv {} /some/path/ \; but it doesn't preserve the directory structure, also I've tried it with find /some/path/with/sourcefiles -type f -size -7M -exec /usr/bin/rsync -auv {} /some/path/ \; but that doesn't... (9 Replies)
Discussion started by: unclecameron
9 Replies

5. Shell Programming and Scripting

Find directory and create backup

What I'm attempting to do is create a script that will do a search for directories that meet the following criteria: find . -name "config" -type d this comes back with: ./dir1/anotherDir/test_dir/config ./dir1/anotherDir/test_dira/config ./dir2/test/test_dir/config The results could... (4 Replies)
Discussion started by: cbo0485
4 Replies

6. UNIX for Dummies Questions & Answers

create tar archive without preserving directory structure?

I am adding some individual files to a tar archive and would like them to be added to the archive without any directory hierarchy, even though the files themselves exist in levels of hierarchy. Unfortunately, tar seems to always preserve the directory hierarchy when it adds the files. Here is... (2 Replies)
Discussion started by: Special_K
2 Replies

7. Shell Programming and Scripting

How to create a directory structure with getting input from a file.

Hi How to create a directory structure with getting input from a file. I have file in that following lines are written. ./activemq-4.1.2/activemq-core-4.1.2.jar ./activemq-4.1.2/backport-util-concurrent-2.1.jar ./camel-1.4.0/apache-camel-1.4.0.jar ./camel-1.4.0/lib/activation-1.1.jar... (12 Replies)
Discussion started by: itsjoy2u
12 Replies

8. Shell Programming and Scripting

stop unix find on a directory structure after finding 1st occurrence

Hi, Has anyone tried to restrict Solaris 10 unix find on a large directory structure based on time to stop running after finding the first occurrence of a matching query. Basically I'm trying to build up a usage map of user workspaces based on file modification (week/month/3 months/year etc) and... (3 Replies)
Discussion started by: jm0221
3 Replies

9. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

10. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies
Login or Register to Ask a Question