LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-07-2005, 10:41 AM   #1
DigiTalk
LQ Newbie
 
Registered: Sep 2005
Posts: 5

Rep: Reputation: 0
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)

For example:
root
dir1
file.mov
file2.mov
file3.txt
dir11
file.ren
file3.mov
dir2
file.hak
dir22
file.mov

now all mov files should be moved into

root
movfiles
dir1
file.mov
file2.mov
dir11
file3.mov
dir2
dir22
file.mov

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 :-)
 
Old 09-07-2005, 10:49 AM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
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.
 
Old 09-07-2005, 10:58 AM   #3
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL
Distribution: Funtoo
Posts: 6,926

Rep: Reputation: 128Reputation: 128
Welcome to LQ!

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.
 
Old 09-07-2005, 11:32 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Code:
$ cd /source/dir/
$ find . -filters... -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
I can't test now, but it should work.

Yves.
 
Old 09-08-2005, 04:22 AM   #5
DigiTalk
LQ Newbie
 
Registered: Sep 2005
Posts: 5

Original Poster
Rep: Reputation: 0
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.

P.S. Using SuSE 9.3, logged in as root on tty2.

Thanx again for your help...
 
Old 09-08-2005, 05:04 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
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 05:10 AM.
 
Old 09-08-2005, 05:32 AM   #7
DigiTalk
LQ Newbie
 
Registered: Sep 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Thanx for the detailed advice :-)

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!
 
Old 09-08-2005, 06:07 AM   #8
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL
Distribution: Funtoo
Posts: 6,926

Rep: Reputation: 128Reputation: 128
If you'd like to remove the spaces from the filenames and folders,
this works in one directory and changes the space to _ (underscore):
Code:
To remove the spaces in files ->
for i in *; do mv "$i" `echo $i | tr ' ' '_'`; done
And this bash script is a little more elegant:
Code:
#!/bin/bash
IFS='
'
j=`find $1 -printf "%d\n" | sort -u | tail -n 1`
j=$((j-1))
echo "Max dir depth:" $j
for (( i=0; i<=j ; i++ ))
do
  for name in `find -mindepth $i -maxdepth $i -iname "* *" -printf "%p\n"`
  do
    newname=`echo "$name" | tr " " "_"`
    echo "$name" "$newname"
    mv "$name" "$newname"
  done
done
Since spaces are escape codes in *nix, I had to rid all those Windoze
files and folders of them when migrating...especially music files.

Last edited by Bruce Hill; 09-08-2005 at 06:08 AM.
 
Old 09-08-2005, 06:26 AM   #9
DigiTalk
LQ Newbie
 
Registered: Sep 2005
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 09-08-2005, 07:11 AM   #10
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
This should help:
Code:
blindcoder@ceres:~/tmp$ while read file ; do dirname=$(dirname "$file"); mkdir -p target/${dirname#source/}; mv "$file" "target/${file#source/}" ; done < <( find source/ -type f -a -name '*' )
Here's an example run:

Code:
blindcoder@ceres:~/tmp$ ls -lR
.:
total 8
drwxr-xr-x  6 blindcoder users 4096 Sep  8 13:05 source
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:05 target

./source:
total 16
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:05 a
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:06 b
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:06 c
drwxr-xr-x  3 blindcoder users 4096 Sep  8 13:06 d

./source/a:
total 0
-rw-r--r--  1 blindcoder users 0 Sep  8 13:05 foo

./source/b:
total 0
-rw-r--r--  1 blindcoder users 0 Sep  8 13:06 bar

./source/c:
total 0
-rw-r--r--  1 blindcoder users 0 Sep  8 13:06 Name with spaces

./source/d:
total 4
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:06 e

./source/d/e:
total 0
-rw-r--r--  1 blindcoder users 0 Sep  8 13:06 do not touch me

./target:
total 0
blindcoder@ceres:~/tmp$ while read file ; do dirname=$(dirname "$file"); mkdir -p target/${dirname#source/}; mv "$file" "target/${file#source/}" ; done < <( find source/ -type f -a -name '*' )
blindcoder@ceres:~/tmp$ ls -lR
.:
total 8
drwxr-xr-x  6 blindcoder users 4096 Sep  8 13:05 source
drwxr-xr-x  6 blindcoder users 4096 Sep  8 13:10 target

./source:
total 16
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:10 a
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:10 b
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:10 c
drwxr-xr-x  3 blindcoder users 4096 Sep  8 13:06 d

./source/a:
total 0

./source/b:
total 0

./source/c:
total 0

./source/d:
total 4
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:10 e

./source/d/e:
total 0

./target:
total 16
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:10 a
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:10 b
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:10 c
drwxr-xr-x  3 blindcoder users 4096 Sep  8 13:10 d

./target/a:
total 0
-rw-r--r--  1 blindcoder users 0 Sep  8 13:05 foo

./target/b:
total 0
-rw-r--r--  1 blindcoder users 0 Sep  8 13:06 bar

./target/c:
total 0
-rw-r--r--  1 blindcoder users 0 Sep  8 13:06 Name with spaces

./target/d:
total 4
drwxr-xr-x  2 blindcoder users 4096 Sep  8 13:10 e

./target/d/e:
total 0
-rw-r--r--  1 blindcoder users 0 Sep  8 13:06 do not touch me
blindcoder@ceres:~/tmp$
It won't remove empty directories, but this can be easily done in a second run:

Code:
blindcoder@ceres:~/tmp$ find source/ -type d -a -empty -exec rmdir {} \;
Run this command until all empty directories are removed.

Last edited by blindcoder; 09-08-2005 at 07:13 AM.
 
Old 09-08-2005, 07:43 AM   #11
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
I have put this into a shellscript for easier use:
http://shellscripts.org/project/mvpartial
Please read the instructions located at:
http://shellscripts.org/wiki/mvpartial
 
Old 09-08-2005, 09:04 AM   #12
DigiTalk
LQ Newbie
 
Registered: Sep 2005
Posts: 5

Original Poster
Rep: Reputation: 0
This works perfectly well!
That is really gread and I didn't have to move around 20GB of image files around by hand (with really really long paths...)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursively move files michaelsanford Linux - General 5 02-17-2016 02:24 AM
rearrange folder structure using mp3 tags roemisch Linux - Software 1 04-18-2005 11:15 AM
how to rm backup files recursively wazoo Linux - Newbie 6 12-31-2004 11:03 AM
ahred folder structure d_kilshtein Linux - Networking 2 10-31-2004 07:11 AM
Recursively listing files over FTP? overbored Linux - Software 4 10-14-2004 01:12 PM


All times are GMT -5. The time now is 10:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration