user981116
user981116

Reputation: 55

Linux: copy one file from several directories

I have a situation where I want to copy a specific file "transaction.log" from thousands of directories under linux and build a tar file containing those same specific files.

Example: I have thousands of directories under /user/foo/dirs/

/user/foo/dirs/dir1
/user/foo/dirs/dir2
/user/foo/dirs/dir3
..
..
..
/user/foo/dirs/dir50000

In each directories, there are several files and a transaction.log file. I would like to copy this transaction.log file from all the 50000 directories and store them in a tar file.

Can you please help me if there is a way to do so?

Thanks.

SOLUTION:

Okies, i found the issue, since there were some sympolic links to other disks find was not working well. solution is to use with -follow option with find to follow the symbolic links. thanks. I use the following command

find . -follow -type f -name "transaction.log" | tar --create --files-from - > /foo/Stats_transaction_Object.tar.gz

Upvotes: 1

Views: 228

Answers (4)

njsf
njsf

Reputation: 2739

find /user/foo/dirs -name transaction.log | tar -cv -T- -f /tmp/stuff.tar

Upvotes: 0

TOC
TOC

Reputation: 4446

You can use this commands:

cd /user/foo/dirs/
find . -type f -name "transaction.log" | xargs tar -zcvf transaction_logs_backup.tar.gz

Upvotes: 1

dstromberg
dstromberg

Reputation: 7167

The one with {} isn't bad, but it'll run a separate tar process for each file. This is a single tar process, so it's quite a bit faster:

find /where/ever -name transaction.log -print | tar --create --files-from - > /somewhere/else/foo.tar

Upvotes: 2

teppic
teppic

Reputation: 8195

Assuming you want to store the directory path as well:

find /user/foo/dirs/ -name 'transaction.log' -exec tar rf /tmp/stuff.tar {} \;

This will store every transaction.log file found within that directory structure in /tmp/stuff.tar.

Upvotes: 0

Related Questions