Home > database >  how to create for loop for symbolic link
how to create for loop for symbolic link

Time:11-24

i want for loop to create symbolic link for All the files under /root/Desktop/match/jars/44a65820/* to be under this path /root/Desktop/match/jars/ as symbolic link without version 44a65820 for example

match@match:~/Desktop/match/jars# ls -ll
total 4
/root/Desktop/match/jars/match-five.jar -> /root/Desktop/match/jars/44a65820/match-five-44a65820.jar
/root/Desktop/match/jars/match-four.jar -> /root/Desktop/match/jars/44a65820/match-four-44a65820.jar
/root/Desktop/match/jars/match-one.jar ->  /root/Desktop/match/jars/44a65820/match-one-44a65820.jar
/root/Desktop/match/jars/match-three.jar -> /root/Desktop/match/jars/44a65820/match-three-44a65820.jar
/root/Desktop/match/jars/match-two.jar -> /root/Desktop/match/jars/44a65820/match-two-44a65820.jar
root/Desktop/match/jars/match-six.jar -> /root/Desktop/match/jars/44a65820/match-six-44a65820.jar

i don't know how to do?

CodePudding user response:

Use parameter expansion to strip prefix and suffix.

for f in /root/Desktop/match/jars/44a65820/*; do
  link="${f%-44a65820.jar}.jar";
  ln -s "$f" "/root/Desktop/match/jars/${link##*/}";
done
  • Related