Inside a directory, there are 150 files named like ABLIMITS.TXT. How do I rename them to ABLIMITS_date.txt prompting the user for date?


Your question is a little unclear (how can 150 files in a single parent dir all have the same filename?), so here is an example of doing it with a single file named ABLIMITS.TXT, which you can extrapolate as necessary:

In a POSIX shell:

Using the current date

mv ABLIMITS.TXT "ABLIMITS_$(date '+%Y-%m-%d').TXT"

Prompting the user for a date

read -rp "Please enter date: " date

I'm going to assume that you want to take the user input, and make sure it follows a standard format (e.g. YYYY-MM-DD). So let's try something like this:

for i in *
  read -p "Enter date for $i: " d
  mv "$i" "${i%.*}_$(date +%Y-%m-%d -d "$d").${i##*.}"

You can read the man page for date to get more information on the format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.