LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-19-2012, 09:14 PM   #1
MisterBark
Member
 
Registered: Jul 2012
Location: Republic of Krakozhia
Distribution: Slackware & Zenwalk core + compile
Posts: 104

Rep: Reputation: 6
Question How to mv while keeping matching sub-directories


Hi!

My problem is kinda ridiculous but I can't find a simple solution!

I would like to make :
Code:
mv */*foo* /directory/
so that the files are moved to /directory/*/*foo* and not /directory/*foo*
(so it would preserve the matching paths)

It is important to make a REAL move, and not a cp+rm because it's on the same disk and it's huge.

THANKS A LOT!
 
Old 07-20-2012, 12:41 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You could create a test area with same dir file layout and a few small files to test, but I don't think(?) mv is that smart ie multi-levels of wildcards.
I'd use a script with find cmd, and test first eg in a test area or use 'echo' to echo out what it would have done instead of doing the actual mv.
 
Old 07-20-2012, 08:49 AM   #3
MisterBark
Member
 
Registered: Jul 2012
Location: Republic of Krakozhia
Distribution: Slackware & Zenwalk core + compile
Posts: 104

Original Poster
Rep: Reputation: 6
ok, I just wrote this program :
Code:
#!/usr/bin/perl
use strict;


my @src = $ARGV[ 0 .. ($#ARGV-1) ];

my $dest = $ARGV[$#ARGV];
$dest =~ s!/*$!/!;

foreach(@src){
	
	my $path = $_;
	$path =~ s!/[^/]+$!!;
	
	print("$_  >  $dest$path/  ...\n");
	
	unless( -d "$dest$path" ){
		mkdir("$dest$path");
	}
	
	system("mv '$_' '$dest$path/'");
	
}

print("DONE MOVING $#ARGV FILES.\n");
exit;
Didn't test it yet but it should work
 
Old 07-25-2012, 09:13 AM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
if you have the storage space you could

rsync -aviS source destination

then just rm -rf /foo

that script looks interesting too. sadly i know nothing about perl yet.
 
Old 07-25-2012, 09:30 AM   #5
MisterBark
Member
 
Registered: Jul 2012
Location: Republic of Krakozhia
Distribution: Slackware & Zenwalk core + compile
Posts: 104

Original Poster
Rep: Reputation: 6
Thanks
I didn't know rsync, that looks great (but not for what I wanted of course)
So it's some kind of MySQL replication concept, but for files?

But the thing is, I was trying to find a way to make a real MOVE, not copy + rm.
Copy + rm is very long for these files, and makes a problem of confidentiality since I'm gonna have to shred everything at the end...
But my program worked

Perl worth it lleb, it's an awesome language. There is nothing you cannot do in perl.
 
1 members found this post helpful.
Old 07-25-2012, 09:36 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could use `find' to locate the files and create a bash script to move them.
Code:
find dir??/*foo* -type f -printf "mv \"%p\" \"directory/%p\"\n" >movescript.sh

>cat movescript.sh
mv "dir10/1foo10/file5" "directory/dir10/1foo10/file5"
mv "dir10/1foo10/file3" "directory/dir10/1foo10/file3"
mv "dir10/1foo10/file1" "directory/dir10/1foo10/file1"
mv "dir10/1foo6/file5" "directory/dir10/1foo6/file5"
...
If the destination directory is on the same filesystem, the files directory entry is moved. If the destination is on a different filesystem, then the `mv' command does a copy & delete. If confidentiality is important, you could change the one liner adding && shred <pathname> to the end.

Code:
find dir??/*foo* -type f -printf "cp \"%p\" \"directory/%p\" && shred \"%p\"\n" >movescript.sh

Last edited by jschiwal; 07-25-2012 at 09:43 AM.
 
Old 07-25-2012, 09:38 AM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
the nice thing about rsync (remote Synchronize) is that it is extremely light on the system, does compression during the move as well as encryption for security.

sadly the rm is very heavy on the system, but not as heavy as mv.

i just have not had time to learn perl. wanted to learn C first. im only a new programmer as in less then 4-6 months with BASH. my next goal is to learn C, then work on perl.
 
Old 07-25-2012, 09:52 AM   #8
MisterBark
Member
 
Registered: Jul 2012
Location: Republic of Krakozhia
Distribution: Slackware & Zenwalk core + compile
Posts: 104

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by lleb View Post
sadly the rm is very heavy on the system, but not as heavy as mv.
The dilemma was not rm VS mv but cp+rm VS mv
 
Old 07-25-2012, 11:34 AM   #9
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
got ya.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
[SOLVED] Matching two tables of non-matching sizes astroumut Programming 3 03-03-2011 07:05 AM
copy wildcard files from directories to matching ones in another dir akufoo Linux - Newbie 1 02-11-2011 11:56 PM
Perl Script needed to be reversed to output matching, not non-matching 0bfuscated Programming 2 07-20-2010 10:51 AM
check for matching files when comparing directories WarriorWarren Linux - General 3 05-07-2003 01:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:47 PM.

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
Open Source Consulting | Domain Registration