LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-23-2012, 08:18 PM   #1
shiftomatic
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Rep: Reputation: Disabled
find file then copy parent directory


Hello,

I would like to search for a file and then copy the file's parent directory and all of its contents. I'm testing in cygwin.

If we have...
/cygdrive/c/dir1/key_file

and there are many other directories with the key_file, I want to copy all directories containing key_file to /cygdrive/c/dat

I thought this would do it...
find . -iname key_file -exec cp "../" /cygdrive/c/dat

But it returns...

cp: target `./dir1/key_file' is not a directory

Any ideas?

Thanks!
 
Old 09-23-2012, 08:26 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,454

Rep: Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052
How about:

Code:
find . -iname key_file | while read file; do
   dir=$(dirname "$file")
   cp -r "$dir" /cygdrive/c/dat/
done
 
2 members found this post helpful.
Old 09-23-2012, 11:23 PM   #3
shiftomatic
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks suicidaleggroll,

That works well. I did see one exception that you may be able to help with...
Where:
$ find . -iname key_file
./archive/microburn/dir1/key_file
./archive/microburn/randy/key_file
./archive/dir1/key_file
./archive/tacobell/dir1/key_file
./mail/dir1/key_file
./dir1/key_file
./randy/key_file

Code:
find . -iname key_file | while read file; do
dir=$(dirname "$file")
cp -r "$dir" /cygdrive/c/dat/
done

Only copied one instance of dir1 and one instance of randy.

I'm guessing it copied all of them but overwrote each only leaving the last instance found. What would it take to append a unique value to dir, tar it, then mv the tar ball? Am I over thinking this?
 
Old 09-23-2012, 11:43 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,454

Rep: Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052
In that case, you just need to decide what you want the end result to look like, and then you can look into the best way of accomplishing it. Do you want to append a number to the end of the directory if one of the same name has already been backed up? Do you want to append the original path to the name of the directory after copying? Do you want to copy the entire original path into the backup directory (eg: ./archive/microburn/dir1 is copied to /cygdrive/c/dat/archive/microburn/dir1). There are many ways of handling this, so it's just up to you how you want it to look after running the script with multiple directories of the same name.

Last edited by suicidaleggroll; 09-23-2012 at 11:44 PM.
 
Old 09-24-2012, 12:41 AM   #5
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,848
Blog Entries: 36

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
You could simply use a number and increment it with each loop. Include that number as part of the target folder name. While that's not the most elegant method it will get you what you want quick and dirty. Otherwise, the find piped while loop solution is a good one. e.g.

Code:
num=1
find ... | while ...;do
  #add $num as part of folder name.
  dir=$(dirname "$file")
  target=$(basename $dir)
  mkdir -p "/cygdrive/c/dat/${target}${num}"
  cp -r "$dir" "/cygdrive/c/dat/${target}${num}"
  #say where the source folder came from so you know which is which; SOURCE is a file
  echo "$file" >> "/cygdrive/c/dat/${target}${num}/SOURCE"
  num=$(($num+1)
done
Alternatively, mktemp is good for situations like this as well. (following code inside a while loop)
Code:
dir=$(dirname "$file")
target=$(basename $dir)
target=$(mktemp -t "${target}XXX")
mkdir -p "/cygdrive/c/dat/${target}"
cp -r "$dir" "/cygdrive/c/dat/${target}"
#say where the source folder came from so you know which is which; SOURCE is a file
echo "$file" >> "/cygdrive/c/dat/${target}/SOURCE"
I would also recommend specifying type file in your find command so that you don't accidentally match against directories, links, and other inode types.

Code:
find . -type f -iname key_file | while ...
SAM

Last edited by sag47; 09-24-2012 at 01:36 AM.
 
Old 09-24-2012, 08:55 PM   #6
shiftomatic
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
In that case, you just need to decide what you want the end result to look like, and then you can look into the best way of accomplishing it. Do you want to append a number to the end of the directory if one of the same name has already been backed up? Do you want to append the original path to the name of the directory after copying? Do you want to copy the entire original path into the backup directory (eg: ./archive/microburn/dir1 is copied to /cygdrive/c/dat/archive/microburn/dir1). There are many ways of handling this, so it's just up to you how you want it to look after running the script with multiple directories of the same name.
Yes append a number, no original path appended, no copy original path.

Thanks!
 
Old 09-24-2012, 09:14 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,454

Rep: Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052
Quote:
Originally Posted by shiftomatic View Post
Yes append a number, no original path appended, no copy original path.

Thanks!
In that case, something like this should work:

Code:
#!/bin/bash

destdir=/cygdrive/c/dat/

if [[ ! -d $destdir ]]; then
   mkdir -p "$destdir"
fi

find . -iname key_file | while read file; do
   srcdir=$(dirname "$file")
   srcloc=$(dirname "$srcdir")
   srcname=$(basename "$srcdir")

   dest="${destdir}/${srcname}"

   if [[ -d "$dest" ]]; then
      num=1
      while [[ -d "${destdir}/${srcname}_$(printf %02d $num)" ]]; do
         let num++
      done
      dest="${destdir}/${srcname}_$(printf %02d $num)"
   fi

   cp -r "$srcdir" "$dest"
done
Note that this will only work up to 100 duplicate directories, if you expect more than that you can change the printf to %03d or higher to allocate more digits to the number. You might also want to put a check in there to break if num exceeds 99.

Last edited by suicidaleggroll; 09-24-2012 at 09:16 PM.
 
Old 09-24-2012, 11:05 PM   #8
shiftomatic
LQ Newbie
 
Registered: Sep 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you both sag47 and suicidaleggroll,

This is all fantastic! Time to do some testing... I'm grateful for the help!
 
  


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
Get first file of directory then copy to other directory andd rename the file Faye Linux - Software 4 01-30-2011 09:16 AM
bash: recursive find, create parent folder, moving file moocan Programming 8 12-19-2010 12:16 AM
Rename file to parent directory name ExWizzard Linux - Newbie 2 12-11-2009 04:19 PM
inherit permissions of the parent directory in newly created file abhijit_mohanta Linux - Newbie 1 09-10-2009 06:45 PM
Run script on every file contained within a parent directory and its subdirectories General Programming 4 05-15-2006 02:12 AM


All times are GMT -5. The time now is 07:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration