-2

I have a files in directory with names like: Afs23 Afs28 Afs33 And I want to replace it with: Afs23 as sb1 Afs28 as sb2 Afs33 as sb3 Where sb1, sb2 and sb3 are stored in another txt file Is there any possible way to do this in shell script?

  • You can be running different shells, like `ksh` or `bash`. Can you tell us your shell (test it with `ps`) ? Maybe we can use https://stackoverflow.com/a/11395181/3220113 – Walter A Oct 29 '18 at 19:36
  • Yes I am using bash for my shell scripting – Somnath Ghosh Oct 29 '18 at 20:24
  • Yes, it's possible. Try for yourself, then [edit] your question and post your code as a [mcve]. Tell us what happens when you run it and what you expected to happen instead. – Robert Oct 30 '18 at 02:55

1 Answers1

0

I added a function mv, so you can test this code before you really move things.
Remove the function when your satisfied.

# Test function
mv () {
   echo "Command: mv \"$1\" \"$2\""
}

# newnames.txt is a file with lines like sb1 and sb2
readarray -t a < newnames.txt
i=0

# Look for filenames starting with Af
for f in Af*; do
   mv "$f" "${a[i]}"
   (( i++ ))
   # Additional check: Do you have more Af* files than newnames?
   (( i == ${#a[@]} )) && { echo "Not enough new filenames"; break; }
done
Walter A
  • 19,067
  • 2
  • 23
  • 43