Moving certain files recursively preservice folder structure
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Moving certain files recursively preservice folder structure
Hi to all!
I already searched here, but found only a thread about moving files recursively into one directory.
What I need seems quite simple:
All files of a specific type in a folder and its sub-folders should be moved into another directory, but into the very same sub-folders.
But the original sub-folders may not be deleted (only if they are empty afterwards)
The dest-subfolders don't exist.
Is there any possibility to do this with one command?
Because I have hundreds of files in thousands of directories which should be moved (specifically - all drive-image files should be moved onto their own partition, but, for easier finding - in the same subdirectories as they have been before.)
Thanks in advance!
Greetings,
Digi
P.S. mv -r would be a great option if it would exist :-)
I doubt that there exists a command which is so specific (it would be against the philosophy of Unix ). Instead you can write a short script which does exactly what you want.
I believe "cp -aR" will do what you want, although nothing is moved.
Which might be better, since you could check afterwards if you have
achieved your desired results.
Thanx for your fast help, sorry for my late answer.
@Chinaman:
Thanx for the welcome :-)
I just tried cp - but it doesn't find the files (mov in my example) which may be due to the fact, that the real dirs contain spaces.
Also when using a test-directory structure without spaces, only the first file is copied.
@theYinYeti:
I'm very sorry, but I could get your script ro run...
Most likely because I'm doing something wrong...
I copied your code to a textfile, and executed as bash.
I replaced "$file" with "*.mov", but filters... gives me an error.
I should have given more details. One never knows what knowledge others have
So for your specific need, it should be:
Code:
$ cd /source/dir/
$ find . -type f -iname "*.mov" -print | xargs tar cf - | (cd /dest/dir/ && tar xpvf - >/tmp/list)
$ while read file; do rm -f "$file"; done </tmp/list
$ rm -f /tmp/list
where /source/dir/ is the directory in which files have to be moved, and /dest/dir/ is the directory where the moved directory structure has to be put.
Advice:
- Please report here, before executing anything, if some of your files' names contain spaces in them.
- Make sure that /source/dir/ is not a place somewhere inside /dest/dir/, or the other way around, as this could lead to unpredictable results.
- Don't proceed with lines 3 and 4 until you have checked that the files you want to move are OK in the destination directory.
- Don't change directory ("cp" command) between lines 2 and 3.
Yves.
Last edited by theYinYeti; 09-08-2005 at 04:10 AM.
First: Yes, file names and folder names often contain spaces.
Second: I tried executing on a test folder structure / file structure and got an error which corresponds to the first point...
Tar could not find folders (which are given as parts of foldernames with spaces)
I won't start on the real files before my testsructure works correctly, because they are image files (between 100MB and 10GB big) on the fileserver of a mid-sized company and contain very important customer data...
So I test the commands on a test-machine with its own, unimportant linux-installation using a test-structure with spaces etc.
So, thanx for your help and especially your patence!
Unfortunately the files are on a samba share for Windows-clients...
And there are hundrets of files in thousands of folders.
And most files can't be renamed because the development software won't find them afterwards anymore...
So I need to preserve the spaces.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.