0

I have a script that downloads a folder.

The folder has a fixed name with an ever changing number as a suffix, e.g. fixed_name_450.

Each time I run to script the downloaded folder will have a new suffix (new number).

I need to copy a couple of files into this folder but the destination directory will always be different as it's number will always change.

Is there a work around?

  • 1
    Will the old directories be present and you need to copy to the one with the highest index or is there just one directory with a "random" number? In either case: is the number of digits fixed? (dir_009 followed by dir_010 or dir_9 followed by dir_10) – Fiximan Jul 4 at 9:28
  • The old directory gets deleted and replace with a new folder with a new number, usually incremented by 1 but could be anything. Either way, the number is always fixed to 3 digits. – s.lock Jul 4 at 9:33
  • You script that that downloads a folder, can you update it so that it sets a symbolic link? rm -f fixed_name_current; ln -s "$downloaded_folder_name" fixed_name_current -- then you can just cp files fixed_name_current and have the right one. – glenn jackman Jul 4 at 13:27
  • If there is only one directory at a time, then you can just use the shell wildcard to figure it out, i.e. cp firstfile secondfile fixed_name_[0-9][0-9][0-9 but if there is potentially more than one you will need to specify the rule which says which one to use, e.g. use the highest numbered one unless it starts with 9 and there are some which start with zero (so the three digit numbering has wrapped). – icarus Jul 5 at 21:31
0

Since you have a fixed format and given prefix, the approach is pretty simple:

cp files fixed_name_???

or

cp files fixed_name_[0-9][0-9][0-9]

Explanation: A ? replaces any character but specifically one only (in contrast to *), [0-9] stands for one digit in the range 0 to 9. Thus the above ways replace three chracters (or digits).

If there is no other directory like this, the asterisk wildcard (standing for none, one or more characters) will work, too:

cp files fixed_name_*

Be aware that in all cases you might need quotes in case of e.g. spaces in your directory names and that it only works if there is no other directory called fixed_name_XXX.

  • This worked perfectly! Thank you so much! – s.lock Jul 4 at 9:52
  • 1
    Also, shopt -s extglob then the pattern fixed_name_+([0-9]) will match one or more digits. – glenn jackman Jul 4 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.