Solved

Shell script to move files matching a pattern, and keeping the same tree structure

Posted on 2008-06-21
7
694 Views
Last Modified: 2013-12-26
Hi

I would like to write a shell script that would move files matching the pattern *_t.* to another location, while keeping the directory structure from the original location.

Can someone help ?
0
Comment
Question by:terencepires
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:rowansmith
ID: 21837465
You don't need a script for this, you can just use the tar command.

0
 
LVL 14

Expert Comment

by:agriesser
ID: 21837479
Untested (currently no access to a linux machine):

#!/bin/sh
 
basedir=/source
destdir=/destination
 
for file in $(find $basedir -type f -name "*_t.*"); do
  mkdir -p ${destdir}/$(dirname $file)
  mv $basedir/$file $destdir/$file
done

Open in new window

0
 
LVL 11

Expert Comment

by:rowansmith
ID: 21837517
The following takes everything in the parents subdirectory tartest, and coppies it to the current directory.

It will only create the sub directories where their is a file to be extracted...

Go to the directory where you want the files to be and replace ../tartest with the full path to the directory where your source files are.

Put the tar command in a script if you want :-)
rowan@daedalus:~/output$ tar -cvvf - ../tartest/ | tar -xvvf - *_t.*
 
[Storing everything starts here]
 
tar: Removing leading `../' from member names
drwxr-xr-x rowan/users       0 2008-06-21 23:25:55 ../tartest/
-rw-r--r-- rowan/users       0 2008-06-21 23:20:41 ../tartest/abc.t.33
drwxr-xr-x rowan/users       0 2008-06-21 23:23:20 ../tartest/dir2/
-rw-r--r-- rowan/users       0 2008-06-21 23:23:20 ../tartest/dir2/abc123_t.gft
-rw-r--r-- rowan/users       0 2008-06-21 23:22:56 ../tartest/abc123_t.gft
drwxr-xr-x rowan/users       0 2008-06-21 23:23:25 ../tartest/dir3/
-rw-r--r-- rowan/users       0 2008-06-21 23:23:25 ../tartest/dir3/abc123_t.gft
drwxr-xr-x rowan/users       0 2008-06-21 23:23:13 ../tartest/dir1/
-rw-r--r-- rowan/users       0 2008-06-21 23:23:13 ../tartest/dir1/abc123_t.gft
 
 
[Writing out starts here]
 
-rw-r--r-- rowan/users       0 2008-06-21 23:23:20 tartest/dir2/abc123_t.gft
drwxr-xr-x                  Creating directory: tartest
drwxr-xr-x                  Creating directory: tartest/dir2
-rw-r--r-- rowan/users       0 2008-06-21 23:22:56 tartest/abc123_t.gft
-rw-r--r-- rowan/users       0 2008-06-21 23:23:25 tartest/dir3/abc123_t.gft
drwxr-xr-x                  Creating directory: tartest/dir3
-rw-r--r-- rowan/users       0 2008-06-21 23:23:13 tartest/dir1/abc123_t.gft
drwxr-xr-x                  Creating directory: tartest/dir1

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:terencepires
ID: 21838351
thanks to both of you !

I prefer the scripting solution as it will be something i will have to do more than once, so it's a lot more convenient to me !

Anyway I get an error when running the script :

mv: rename /Users/terencepires/Documents/Skydog/Images/Images_Skydog///Users/terencepires/Documents/Skydog/Images/Images_Skydog//54_nude_honeys_FR_TOUR/1_t.jpg to
/Users/terencepires/Documents/Skydog/Images/Thumbnails//Users/terencepires/Documents/Skydog/Images/Images_Skydog//54_nude_honeys_FR_TOUR/1_t.jpg: No such file or directory

0
 
LVL 14

Accepted Solution

by:
agriesser earned 500 total points
ID: 21838366
OK, I'm now having access to a linux machine.
Here's the fixed version:
#!/bin/sh
 
basedir=/source
destdir=/destination
 
for file in $(cd $basedir && find . -type f -name "*_t.*"); do
  mkdir -p $destdir/$(dirname $file)
  mv $basedir/$file $destdir/$file
done

Open in new window

0
 
LVL 14

Expert Comment

by:agriesser
ID: 21838373
And here's a version that works with command line parameters (if desired).

Usage: move-files.sh /source /destination
#!/bin/sh
 
if [ "$#" != "2" ]; then
  echo "Usage: $(basename $0) <source-dir> <destination-dir>"
  exit 1
fi
 
basedir="$1"
destdir="$2"
 
for file in $(cd $basedir && find . -type f -name "*_t.*"); do
  mkdir -p $destdir/$(dirname $file)
  mv $basedir/$file $destdir/$file
done

Open in new window

0
 
LVL 1

Author Closing Comment

by:terencepires
ID: 31469409
THANK
YOU !

Cheers
Terence
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linux Search 3 67
Help with a backup script in Linux 4 76
sum28 challenge 31 117
ShiftLeft challenge 21 89
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question