0

There are several files I need to copy which are stored in different directories. The file names will all have two common fields that are distinctive for a particular date and time. I can usually refer to them with something like 1155.006
Basically what I want to do is go to the first directory, tell it to copy the file with 1155.006 to my home directory, change to the next directory and do the same thing etc. I will need to name about 5 specific directories so I can't do a blanket grab from all of the sub-directories since that would also net me about 30 files I don't want. I would also like to get the full name of the files that were copied and write those names to a text file.

I know this is a newbie scripting question so if you can also recommend a good place to start learning scripting commands please send that along.

4
  • Hello and welcome on stackexchange site web dedicated to linux and unix system. As you will realize, you will probably have very few answer without showing a bit of work. to ask a correct question, you should describe what you want to do, what you have done, what is the expected result, and what is the result you get. You can edit your question to add more detail and you can consult the help to ask a better question or directly this article
    – Kiwy
    Feb 9, 2015 at 18:15
  • If your dirnames are also unique you can use $CDPATH
    – mikeserv
    Feb 9, 2015 at 18:19
  • @ott - that suggestion is unsafe for several reasons - but first and foremost it is to do with $IFS.
    – mikeserv
    Feb 9, 2015 at 18:32
  • @ott-- Filename wildcards aren't regular expressions.
    – Barmar
    Feb 9, 2015 at 21:18

1 Answer 1

0

You want something like this:

for dir in dir1 dir2 dir3 dir4 dir5
do
    cp $dir/*1155.006* $HOME
done

If you want to log the names of files:

for dir in dir1 dir2 dir3 dir4 dir5
do
    for file in $dir/*1155.006*
    do
        cp $file $HOME
        echo "Copied $file" >> log.txt
    done
done
1
  • 1
    Or just add a -v to the cp command in the first for loop. :) It'd be a good idea to add a -b to the cp either way, so it'll create backup files if two happen to have the same name.
    – dannysauer
    Aug 14, 2018 at 22:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .