[英]how to create for loop for symbolic link
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我希望循环为
/root/Desktop/match/jars/44a65820/*
下的所有文件创建符号链接,例如,在这个路径/root/Desktop/match/jars/
下作为没有版本 44a65820 的符号链接
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?我不知道该怎么做?
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
Given the simplified tree for testing给定用于测试的简化树
shell> tree /tmp/match
/tmp/match
└── jars
└── 44a65820
├── one.jar
├── three.jar
└── two.jar
In Ansible, declare the variable在Ansible,声明变量
match_files: "{{ st.files|map(attribute='path')|list }}"
- find:
path: /tmp/match
file_type: file
patterns: '*.jar'
recurse: true
register: st
gives给
match_files:
- /tmp/match/jars/44a65820/two.jar
- /tmp/match/jars/44a65820/one.jar
- /tmp/match/jars/44a65820/three.jar
Create the links in the loop在循环中创建链接
- file:
state: link
src: "{{ item }}"
dest: "{{ path }}/{{ file }}"
loop: "{{ match_files }}"
vars:
arr: "{{ item.split('/') }}"
path: "{{ arr[:-2]|join('/') }}"
file: "{{ arr[-1] }}"
gives给
shell> tree /tmp/match
/tmp/match
└── jars
├── 44a65820
│ ├── one.jar
│ ├── three.jar
│ └── two.jar
├── one.jar -> /tmp/match/jars/44a65820/one.jar
├── three.jar -> /tmp/match/jars/44a65820/three.jar
└── two.jar -> /tmp/match/jars/44a65820/two.jar
Example of a complete playbook for testing用于测试的完整剧本示例
- hosts: localhost vars: match_files: "{{ st.files|map(attribute='path')|list }}" tasks: - find: path: /tmp/match file_type: file patterns: '*.jar' recurse: true register: st - debug: var: match_files - file: state: link src: "{{ item }}" dest: "{{ path }}/{{ file }}" loop: "{{ match_files }}" vars: arr: "{{ item.split('/') }}" path: "{{ arr[:-2]|join('/') }}" file: "{{ arr[-1] }}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.