Loop over files (Bash): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
(Nieuwe pagina aangemaakt met 'This is cool: You can loop over files in a directory and do stuff with it! == Simple example == <pre> #!/bin/bash cd ~/images-tmp for file in * do echo "File...') |
|||
Regel 1: | Regel 1: | ||
This is cool: You can loop over files in a directory and do stuff with it! | This is cool: You can loop over files in a directory and do stuff with it! | ||
− | == | + | == Examples == |
+ | |||
+ | Start simple: | ||
<pre> | <pre> | ||
Regel 11: | Regel 13: | ||
echo "File name: $file" | echo "File name: $file" | ||
done | done | ||
+ | </pre> | ||
+ | |||
+ | Create links to all files in this folder, and put those links in another folder: | ||
+ | |||
+ | <pre> | ||
+ | for file in *.jpg | ||
+ | do | ||
+ | |||
+ | source="${PWD}/${file}" | ||
+ | link="/var/www/media.example.com/tmp2/$file" | ||
+ | |||
+ | echo " Path+file: $source - Destination: $link" | ||
+ | |||
+ | ln -s "$source" "$link" | ||
+ | |||
+ | done | ||
</pre> | </pre> | ||
Versie van 8 aug 2022 08:08
This is cool: You can loop over files in a directory and do stuff with it!
Examples
Start simple:
#!/bin/bash cd ~/images-tmp for file in * do echo "File name: $file" done
Create links to all files in this folder, and put those links in another folder:
for file in *.jpg do source="${PWD}/${file}" link="/var/www/media.example.com/tmp2/$file" echo " Path+file: $source - Destination: $link" ln -s "$source" "$link" done