Copying files to new dir structure.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying files to new dir structure.
# 1  
Old 05-22-2012
Copying files to new dir structure.

I am trying to figure out a way to script copying specific files from one dir structure to another.

I have a dir structure like this:
Code:
dira/author 1/book 1/file a.epub
             /book 2/file b.epub
    /author 2/book 1/file c.epub
    /author 3/book 1/file d.epub
             /book 2/file e.epub
             /book 3/file f.epub
<etc....>

The result I am looking for is:
Code:
dirb/author 1/file a.epub
             /file b.epub
    /author 2/file c.epub
    /author 3/file d.epub
             /file e.epub
             /file f.epub
<etc....>

Currently I just use a quick script to cp the files up one level in the "dira" file structure, dupe it to "dirb" and then clean it up manually:
Code:
find . -type f -name "*.epub" |while read file
do
        cp "$file" "${file%/*}"/../
done

I could do something like this to create the new dir structure:
Code:
find . -type f -name "*.epub" -printf "%P\n" > $books
cd dir2
awk -F / '{ print $2 }' $books | xargs $DEBUG mkdir -p

but I am stumped on copying the files into that structure. Smilie
# 2  
Old 05-22-2012
Code:
user@ubuntu:~/programs$ tree dira
dira
├── author1
│   ├── book1
│   │   └── filea.epub
│   └── book2
│       └── fileb.epub
├── author2
│   └── book1
│       └── filec.epub
└── author3
    ├── book1
    │   └── filed.epub
    ├── book2
    │   └── filee.epub
    └── book3
        └── filef.epub

9 directories, 6 files
user@ubuntu:~/programs$
user@ubuntu:~/programs$ cat test.sh
#! /bin/bash

find . -type f -name "*.epub" | \
while read x
do
    y=${x%/*}; y=${y%/*}; y=${y/dira/dirb}
    mkdir -p $y
    cp $x $y
done
user@ubuntu:~/programs$ ./test.sh
user@ubuntu:~/programs$ tree dirb
dirb
├── author1
│   ├── filea.epub
│   └── fileb.epub
├── author2
│   └── filec.epub
└── author3
    ├── filed.epub
    ├── filee.epub
    └── filef.epub

3 directories, 6 files
user@ubuntu:~/programs$

These 2 Users Gave Thanks to balajesuri For This Post:
# 3  
Old 05-23-2012
That works! Thanks for the help!

I also came up with this as another option:

Code:
#!/bin/bash
sdir=<path>/dira
ddir=<path>/dirb

cd "$sdir"; 

IFS='\
'
for author in `ls -1`; 
do 
(mkdir "$ddir/$author"; find "$author" -type f -name "*.epub" -exec cp "{}" "$ddir/$author" \;); 
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files from various folders to similar folder structure in another location

Hi, I need to write a script the has to copy the files from folders and subfolders to the same folder structure located in another location. Ex: mainfolder1 file1,file2,file3 subfolder1(file1,etc) subfolder2(file1,etc) to another folder location of same folder structure. rsync is not... (7 Replies)
Discussion started by: Raji Perumal
7 Replies

2. Shell Programming and Scripting

PERL - Copying ONLY files from one dir to another

I'm writing a Perl script which has its 1st step as to copy files from one directory to another directory. The Source directory has got files with extension, without extension, directories etc. But I want to copy ONLY files with no extension. The files with extensions and directories should not get... (2 Replies)
Discussion started by: jhamaks
2 Replies

3. Shell Programming and Scripting

Issue with copying files into dir inside for loop

Hi , I'm trying to move/copy the files inside the loop into a directory . I tried the below code and the issue is the data is not copying into the created directory but the files are copying into another file file_path="/home/etc" Last_Day=20130930 mkdir $file_path/ARC_${Last_Day} ... (3 Replies)
Discussion started by: smile689
3 Replies

4. UNIX for Dummies Questions & Answers

copying the dir/subdir structure from one server to another?

Hi All, I want to copy the dir/subdir structure from SERVER-A to SERVER-B without copying all the files in each dir. Is it possible using SCP / SFTP command? For example, SERVER-A has following two dir/subdirectories and files under each subdir. ... (1 Reply)
Discussion started by: Hangman2
1 Replies

5. Shell Programming and Scripting

Copy files from input file with dir structure

hi, I want to copy files from source directory based on input file (or output of previous command) and i want to have the SAME DIRECTORY STRUCTURE. Note that i will have other files and directories which i dont want to copy to destination. For example, dir source has following content:... (22 Replies)
Discussion started by: dragon.1431
22 Replies

6. Shell Programming and Scripting

Copying specific files from one dir to another

Hi Folks, I have one curious case. There are list of following files placed in one directory such as... And updated each month. files.JAN09.csv files.FEB09.csv files.MAR09.csv ..... Now, I need to move a specific files; i.e, For this month, I need to move only OCT09, NOV09, DEC09,... (1 Reply)
Discussion started by: Jerald Nathan
1 Replies

7. Shell Programming and Scripting

Copying a directory structure with the latest versions of files

Hello I have three directory structures for code releases. Each directory structure looks like this: bash-3.00$ ls -R | more .: Test_Release_1 Test_Release_2 Test_Release_3 ./Test_Release_1/dbcode: rp_online_import_srdp.pkb-1 srdp_ar_validation.pkb-1... (1 Reply)
Discussion started by: Glyn_Mo
1 Replies

8. Shell Programming and Scripting

Script to run a command on all txt files present in a dir structure

Hi, I have a directory structure like the one given below root\a\b1 root\a\b2 root\b\b1 root\b\b2 . . . root\j\b1 root\j\b2 Now, there are a txt files in each dir and subdir, there is a root.txt I have to write a script where in i have to run a command called "genrb <filename>"... (6 Replies)
Discussion started by: vikramsinghnegi
6 Replies

9. UNIX for Advanced & Expert Users

copying of files by userB, dir & files owned by userA

I am userB and have a dir /temp1 This dir is owned by me. How do I recursively copy files from another users's dir userA? I need to preserve the original user who created files, original group information, original create date, mod date etc. I tried cp -pr /home/userA/* . ... (2 Replies)
Discussion started by: Hangman2
2 Replies

10. Shell Programming and Scripting

copying files to new dir

Hello, I'm new to shell scripting so I don't really understand what I'm doing wrong. The script I'm trying to do saves all the files (*.c) on the current dir to a list and, one by one, copies them to a new one called Backup. The thing is, if there are already other versions of the files I'm... (6 Replies)
Discussion started by: G3nn
6 Replies
Login or Register to Ask a Question