move files and retain subdir structure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting move files and retain subdir structure
# 1  
Old 03-25-2009
Lightbulb move files and retain subdir structure

hi:

I have some files like this

folder1/recording1.mp3
folder1/docs/budget.doc

folder2/others/misc.mp3
folder3/others/notes.doc

all this folders and files are under the mp3 folder.

I would like to move just the mp3s to another folder but retain the subdir structure i have.

So if a mp3 was under folder2/others
then it is moved to
newmp3s/folder2/others

All the .doc files should remain intact and not moved.

Thanks in advance.
# 2  
Old 03-25-2009
Code:
find folder1/ folder2/ -name '*.mp3' -print | while read file
do
    echo mkdir -p $( dirname $file )
    echo mv $file newdir/$( dirname $file )
done

If it looks like it should (eg no unexpected stuff), remove the echos.

Last edited by pludi; 03-25-2009 at 09:31 AM.. Reason: Corrected formatting
# 3  
Old 03-25-2009
when I use it with echos it looks like it should work. mkdir commands are duplicated though.

When I run it without echos I get a "No such file or directory" message.

It finds the files and everything but it doesnt create the folders I guess.
# 4  
Old 03-25-2009
Sorry, my bad. Missed to include the new directory in the mkdir (red part)
Code:
find folder1/ folder2/ -name '*.mp3' -print | while read file
do
    echo mkdir -p newdir/$( dirname $file )
    echo mv $file newdir/$( dirname $file )
done

# 5  
Old 03-25-2009
thanks!
Image
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to move files and Creation of folder structure

We are receiving few zipped files in one location say : apple/oranges/incoming All .zip files are placed here in incoming folder. So few of the files are password encrypted. There are only 10 zipped files, so we are planning to create a script which will pick that zip file from incoming... (1 Reply)
Discussion started by: Sidhant
1 Replies

2. Shell Programming and Scripting

Parallel move keeping folder structure along with files in it

The below will move all the files in the directory dir to the destination using parallel and create a log, however will not keep them in the directory. I have tried mkdir -p but that does not seem to work or at least I can not seem to get it (as it deletes others files when I use it). What is the... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

How to move the files older than x days with similar directory structure?

Hello, I need to move all the files inside /XYZ (has multi-depth sub directories) that are older than 14 days to/ABC directory but with retaining the SAME directory structure. for example: /XYZ/1/2/3/A/b.txt should be moved as /ABC/1/2/3/A/b.txt I know about find /XYZ -type f -mtime +14... (3 Replies)
Discussion started by: prvnrk
3 Replies

4. UNIX for Dummies Questions & Answers

copying the dir/subdir structure from one server to another?

Hi All, I want to copy the dir/subdir structure from SERVER-A to SERVER-B without copying all the files in each dir. Is it possible using SCP / SFTP command? For example, SERVER-A has following two dir/subdirectories and files under each subdir. ... (1 Reply)
Discussion started by: Hangman2
1 Replies

5. Shell Programming and Scripting

Move all files from dir and subdir to Archive with File name as complete path_filename

HI, I need to move all files from a dir & its all subdir to Archive folder which is indise dir only. and moved filename should changed to complete path ( Like Dir_subdir_subdir2_.._filename ). also all files names shoud capture in a file in order to mail I written below code ... (11 Replies)
Discussion started by: minijain7
11 Replies

6. OS X (Apple)

Batch file to move video files and retain sub-directories

I have just purchased my first ever Apple computer - and am therefore new to UNIX also. I would like to create a simple "batch file" (apologies if this is the wrong terminology) to do the following: When I plug my camera into the MAC it automatically downloads photos and videos into a new... (1 Reply)
Discussion started by: mm0mss
1 Replies

7. Shell Programming and Scripting

Help - Script to move files to subdir depending on file extension.

Hi, First off I'm pretty new to scripting so please be gentle. I am looking for some help with a script that will move all files with a certain extension into a folder within their current location. Just for clarity I have all my photos organised in directories such as: ... (4 Replies)
Discussion started by: guinch
4 Replies

8. Shell Programming and Scripting

Move files & folder structure

Hey, this might be a really basic question, but I'm new to Unix scripting. I'm trying to find a command to replicate a file structure from one location to another & move the actual files contained in the base directories, i.e. I have this structure - home/temp/test/dir1/ ... (3 Replies)
Discussion started by: SOCLA_CELT
3 Replies

9. UNIX for Dummies Questions & Answers

Finding file in a directory structure + move

Hi All, im a new guy if it comes to Unix. I am trying to auto categorize Nzbget downloads the most basic way. I already manage to find files within the directory i'm at and move them with if check to a certain dir. Unfortunately this command is restricted to the directory i'm at and does not... (2 Replies)
Discussion started by: RoxXxoR
2 Replies
Login or Register to Ask a Question