Rename files in a directory in an edited list fashion

exec 3<&0; ls -1N | while read a; do echo "Rename file: $a"; read -e -i "$a" -p "To: " b <&3 ; [ "$a" == "$b" ] || mv -vi "$a" "$b"; done
just an alternative to #7818
Sample Output
Rename file: 1one
To: 1one.txt
`1one' -> `1one.txt'
Rename file: 2two
To: one plus one
`2two' -> `one plus one'
Rename file: 3three
To: 3three
Rename file: 4four
To: 2 x 2
`4four' -> `2 x 2'
Rename file: 5five
To: 5five.avi
`5five' -> `5five.avi'

By: forcefsck
2011-02-08 21:21:39

1 Alternatives + Submit Alt

  • Instead of tedious manual mv commands and tabbing, this routine creates a file listing all the filenames in the PWD twice, edit the second instance on each line to the new name, then save the file, the routine does the rest. Feel free to replace nano with your holy war editor of choice. You will get a lot of "mv: 'x' and 'x' are the same file" warnings, these could be cleaned up but the routine works.

    ls > ls; paste ls ls > ren; nano ren; sed 's/^/mv /' ren|bash; rm ren ls
    romulusnr · 2011-02-08 17:07:13 7

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: