Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My first question on this stackexchange, and I am sure there will be more as I venture farther into the world of what is Linux...

I have a for-loop in a shell-script that batch renames all files to a substring (the last n characters) of its original name.

It will echo every iteration on a new line to eventually produce a list of all files but how do I keep that echo on a single/updating line so it doesn't produce this (sometimes large) list?

echo "- Renaming file..."

for file in `find fldr -type f`
do
  newf=$(echo $file | rev | cut -c -6 | rev)
   mv -f $file fldr/$newpt
  echo "  * $file > $newf"
done

actual output...

- Renaming file...
  * file1a.txt > 1a.txt
  * file2a.txt > 2a.txt
  * file3a.txt > 3a.txt
  * ...

desired output...

- Renaming file...
  * file3a.txt > 3a.txt

I would like to see the one line always changing to show the current file only.

[BONUS] How would I get it to also display the n'th file it is renaming?

- 3 files renamed...
  * file3a.txt > 3a.txt

Where n is a cumulative sum/count of the files renamed.

share|improve this question
up vote 2 down vote accepted

Just change the echo line to this:

echo -ne "\r  * $file > $newf                         "

The spaces on the end clear old output from the line.

share|improve this answer
1  
Umm, I think it should be echo -ne "\r * $file > $newf Otherwise echo will not interpret escape characters. – darkdragn Jun 7 '11 at 11:37
1  
printf is more portable when it comes to escape chars: printf "\r * %s > %-50s" "$file" "$newf" – glenn jackman Jun 7 '11 at 12:25
    
Worked for me. But sure, printf is probably better, but I just did a simple modification to the original here. – Keith Jun 7 '11 at 14:34
    
Thanks! Looks promising. I will get to try it out this afternoon but I can see the logic. Will mark as correct when I get it working. – SaultDon Jun 7 '11 at 18:20
    
@chris You're right, I tried it, and it wouldn't work without the -ne I am doing this is in Bash on Ubuntu 10.04 if that makes any difference... Thanks for the help all. – SaultDon Jun 8 '11 at 1:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .