2

I would like to assign a symbolic link for each text files from the same folder. Be more specific, let's say there are four text files in a folder.

AAAA.txt
BBBB.txt
ABAB.txt
BABA.txt

I would like to assign a symbolic link for each. For example

ln -s AAAA.txt sample1.txt
ln -s BBBB.txt sample2.txt
ln -s ABAB.txt sample3.txt
ln -s BABA.txt sample4.txt

How can I use forloop to do this?

for f in "$dir"/.txt; 
do
ln -s $f sample[1,2,3,4].txt (I know this is not right !!!)
done
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
user3631848
  • 433
  • 1
  • 6
  • 14
  • You are right, the way of using `[1,2,3,4]` would _never_ work in `bash` as it is not a valid construct at all. Perhaps you were trying to use brace expansion? `{1..4}` even then in your case it would have expanded to `ln -s $f sample1.txt sample2.txt sample3.txt sample4.txt` which is quite incorrect as per the given requirement – Inian Mar 05 '19 at 08:41
  • 1
    You can do it without a `for` loop by the way, with **GNU Parallel** like this `parallel ln -s {} sample{#}.txt ::: *txt` – Mark Setchell Mar 05 '19 at 09:23

1 Answers1

2
i=1;
for f in "$dir"/*.txt; 
do
    ln -s "$f" "sample{$i}.txt";
    ((i+=1));
done
Reda Meskali
  • 275
  • 3
  • 9