Bash selective copy folders and content to another location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash selective copy folders and content to another location
# 1  
Old 08-13-2014
Bash selective copy folders and content to another location

I'm looking for a bash scrypt to copy some folders and some of the content to another location. I'm a teacher and very noobish with programming language anyway what I'm looking for , I have this director structure

Main director "Students" with subfolders "john";"daisy";"work" etc .. and some of the folders contain a specific file for example "exam.dat" , I need a scrypt that will check every folder in students for the file "exam.dat" and copy that folder and this file to another location, without copying other files inside folders or folders that doesn't contain exam.dat.

Hope I'm explicit enough. Thank you in advance
# 2  
Old 08-13-2014
Code:
cd /path/to/basefolder
tar -cf - */exam.dat | tar -C /path/to/destfolder -xvf -

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-13-2014
This approach will work fine as long as exam.dat is directly under the student folder but if you have john/class_work/exam.dat it will get missed.


This changed approach will work for this case:

Code:
cd /path/to/basefolder
tar -cf - $(find . -name exam.dat -type f) | tar -C /path/to/destfolder -xvf -

Note that if no files exist with the required name, you may get an error from tar along the lines of:
Code:
tar: Cowardly refusing to create an empty archive

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i move folders and its content if folder is older than 1,5 days and keep subdirs in bash?

Hello all, do you know any way i can i move folders and its content if folder is older than 1,5 days in bash? I tried: find /home/xyz/DATA/* -type d -ctime +1.5 -exec mv "{}" /home/xyz/move_data_here/ \;All i got was that Files from DATA /home/xyz/DATA/* ended messed up in... (1 Reply)
Discussion started by: ZerO13
1 Replies

2. Shell Programming and Scripting

How to copy selective list of files to a directory?

Hi I have 3 directories indexes_with_ts indexes_without_ts process_indexes in each directories it contains *.sql how do I accomplish this: for all the files found in indexes_without_ts, copy the corresponding file in indexes_with_ts to process_indexes. i.e. for... (2 Replies)
Discussion started by: jediwannabe
2 Replies

3. Programming

Copying folders from one location to another..

Hi all, I need a suggestion in the following case, I have folder1, folder2 ,folder3 and file1 inside /home/test/source .. I need to copy all the folders and files to another location /home/test/destination Pls suggest any way to program this in C++.. :confused: (1 Reply)
Discussion started by: selvarajvs
1 Replies

4. Shell Programming and Scripting

How to copy a file from one location to another location?

I have file file1.txt in location 'loc1'. Now i want a copy of this file in location 'loc2' with a new file called test.txt. Please help me how to do this in shell script. (1 Reply)
Discussion started by: vel4ever
1 Replies

5. Shell Programming and Scripting

Copy selective lines from text file

Hello, I have a text file which I need to check for presence of certain tags, and then copy a subsequent portion of text into another file. The tag matching canbe done with Grep but I do not know how to copy selective lines from one file to another. Is it possible do that? I checked up some... (8 Replies)
Discussion started by: ajayram
8 Replies

6. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

7. Shell Programming and Scripting

how to merge selective content between files using awk?

Hello, I'm new to awk. I've 2 files where H stands for header and T for trailer. The number following T gives the record count in a file. file 1 looks like this: H|A|B|C 1|2|3 1|2|4 2|3|5 T|3 file 2 looks like this: H|A|B|C 4|5|6 7|8|9 T|2 Need to merge the above 2 files such... (2 Replies)
Discussion started by: skumre
2 Replies

8. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies

9. UNIX for Advanced & Expert Users

copy files from one location to similar location

I need help in forming a script to copy files from one location which has a sub directory structure to another location with similar sub directory structure, say location 1, /home/rick/tmp_files/1-12/00-25/ here 1-12 are the number of sub directories under tmp_files and 00-25 are sub... (1 Reply)
Discussion started by: pharos467
1 Replies

10. UNIX for Dummies Questions & Answers

To compare selective file in different folders

Hello, I am using dircmp -d <folde1> <Folder2> to compare the files from two different foldes, but this command compares for all the files. Is there any option to select only some files for comparision. For example in Folder1: file1.txt file2.txt file3.txt Folder2 file1.txt file2.txt... (0 Replies)
Discussion started by: gmahesh2k
0 Replies
Login or Register to Ask a Question