I have a directory structure that contains a number of files which are named the same as in: dir1/test dir2/test dir3/test I want to find each test file and copy it to another directory with a unique (arbitrary) file name so I can analyze the data they contain. There could be a large number of these similarly named files contained several layers deep in the directory structure. I can use find to locate the files but the renaming (using find) is alluding me. Can anyone provide guidance on this issue?
Asked
Active
Viewed 890 times
2 Answers
1
- source_dir is root directory form where you want to search the files.
- destination_dir is destination directory to where you want to copy or move your files
- *.doc is your file name or file pattern to search for.
You can copy files with following command! note that --backup=numbered will create numerically increased suffixed file if file with that name is already in destination directory.
find /source_dir -type f -name *.doc | xargs -I '{}' cp --backup=numbered {} /destination_dir
You can move files with following command! note that --backup=t will create numerically increased suffixed file if file with that name is already in destination directory.
find /source_dir -type f -name *.doc | xargs -I '{}' mv --backup=t {} /destination_dir
You can also consider using these commands with sudo if you might have in search destination matching files belonging to other user.
If you want verbose output of these commands add add -v after --backup=numbered or --backup=t.
mkungla
- 111
- 4
0
You can replace the / in the path with another character and use that as the new file name:
#!/bin/bash
for f in /dir*/*
do
cp "$f" "${f//\//.}"
done
Mark
- 166
-
Can I integrate your answer into a find command? As in, find . -name FNAME -exec bash -c 'for f in {} do; cp {} "$f" "/tmp/somedir/${f////.}" ; done ;. I tried it and received modifier failed. Should this work? – Charles Jul 09 '15 at 16:57