I need script to copy on cron basis a list of files. Files selected on name/datetime pattern and to name of file destination must by appended data like ddmmyyy. It is not problem copy files or directory, but problem to change name of each file according to its data. May be exists some open source solution? Thanks.
Asked
Active
Viewed 2,932 times
1 Answers
4
You haven't provided enough information for me to give you real working code; but you can do something like this:
file=dated_log.log
ddmmyyyy=$(read -r < "$file" ; echo "${REPLY:1:8}")
cp "$file" "$file.$ddmmyyyy"
The above will copy dated_log.log
to data_log.log.30102011
, assuming that the first line of dated_log.log
starts with 30102011
.
The Bash Reference Manual will hopefully help you adjust the above to suit your needs.

ruakh
- 175,680
- 26
- 273
- 307
-
Instead of `ddmmyyyy=$(read -r < "$file" ; echo "${REPLY:1:8}")` you could `read -r -n 8 ddmmyyyy < "$file"`. – gniourf_gniourf Nov 02 '13 at 21:57
-
@gniourf_gniourf: Yes, good point. But I think my answer is better the way it is, because I rather doubt that the OP's file format *actually* starts with the exact eight right characters, and I think the code that I posted is likely to be more easily tweaked into whatever form the OP needs. (Suppose that the OP actually needs characters 30 through 37. How would you adjust `read -r -n 8 ddmmyyy` to retrieve them?) – ruakh Nov 03 '13 at 04:03
-
1for characters 30 through 37: `read -r -n 37 ddmmyyyy < "$file"; ddmmyyyy=${ddmmyyyy:30}` will do. Or even directly: `read -r ddmmyyyy < "$file"; ddmmyyyy=${ddmmyyyy:30:8}`. Or `read -r ddmmyyyy; printf -v ddmmyyyy "%.8s" "${ddmmyyyy:30}"` or... the goal is just to avoid a subshell (I personally find `$( ... echo )` of rather bad style. `:)`. – gniourf_gniourf Nov 03 '13 at 08:22