Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux bash How to use a result of a wildcard as a file name in a copy command

Tags:

linux

bash

cp

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
like image 371
hoz Avatar asked Oct 19 '25 00:10

hoz


2 Answers

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
like image 130
chepner Avatar answered Oct 21 '25 13:10

chepner


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
like image 42
jhnc Avatar answered Oct 21 '25 13:10

jhnc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!