Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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
mv ABLIMITS.TXT "ABLIMITS_${date}.TXT"
share|improve this answer
add comment

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 *
do
  read -p "Enter date for $i: " d
  mv "$i" "${i%.*}_$(date +%Y-%m-%d -d "$d").${i##*.}"
done

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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