Reputation: 571
I'm writing a Linux script to copy files from a folder structure in to one folder. I want to use a varying folder name as the prefix of the file name.
My current script looks like this. But, I can't seem to find a way to use the folder name from the wildcard as the file name;
for f in /usr/share/storage/*/log/myfile.log*; do cp "$f" /myhome/docs/log/myfile.log; done
My existing folder structure/files as follows and I want the files copied as;
>/usr/share/storage/100/log/myfile.log --> /myhome/docs/log/100.log
>/usr/share/storage/100/log/myfile.log.1 --> /myhome/docs/log/100.log.1
>/usr/share/storage/102/log/myfile.log --> /myhome/docs/log/102.log
>/usr/share/storage/103/log/myfile.log --> /myhome/docs/log/103.log
>/usr/share/storage/103/log/myfile.log.1 --> /myhome/docs/log/103.log.1
>/usr/share/storage/103/log/myfile.log.2 --> /myhome/docs/log/103.log.2
Upvotes: 4
Views: 2613
Reputation: 531858
You could use a regular expression match to extract the desired component, but it is probably easier to simply change to /usr/share/storage
so that the desired component is always the first one on the path.
Once you do that, it's a simple matter of using various parameter expansion operators to extract the parts of paths and file names that you want to use.
cd /usr/share/storage
for f in */log/myfile.log*; do
pfx=${f%%/*} # 100, 102, etc
dest=$(basename "$f")
dest=$pfx.${dest#*.}
cp -- "$f" /myhome/docs/log/"$pfx.${dest#*.}"
done
Upvotes: 4
Reputation: 14454
for f in /usr/share/storage/*/log/myfile.log*; do cp "$f" "$(echo $f | sed -re 's%^/usr/share/storage/([^/]*)/log/myfile(\.log.*)$%/myhome/docs/log/\1\2%')"; done
Upvotes: 0
Reputation: 16807
One option is to wrap the for loop in another loop:
for d in /usr/share/storage/*; do
dir="$(basename "$d")"
for f in "$d"/log/myfile.log*; do
file="$(basename "$f")"
# test we found a file - glob might fail
[ -f "$f" ] && cp "$f" /home/docs/log/"${dir}.${file}"
done
done
Upvotes: 2