I wanted to write a bash script that will read the output of 'ls' in a directory and rename each file that matched a specific pattern. However I soon found out that using
for line in lswas no good as it returned the output word by word.
The solution:
The problem was solved after a bit of googling. The code that I needed was:
ls | while read lineVarWhat this does:
do
...
...
done
1. ls pipes its output into "while read" which read line by line and stores each line into $lineVar (you don't need the dollar initially).
2. In the while/do loop I can do the pattern matching using sed and renaming.
The full script (currently with no terminal input - it's all hardcoded):
#!/bin/bashHope this helps
ls | while read lineVar
do
echo OLD: $lineVar
new=`echo $lineVar | sed -e 's/^.....//g'`
echo Replacing $lineVar with $new
mv "$lineVar" "$new"
done
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου