Shell Script to Rename & Move files in Unix

Problem Statement: I have few files in source directory. I have to move them to another destination directory. The source directory have *.txt files that needs to be moved to destination directory. The destination filename should be *.txt.backup.

So how to accomplish the above task in Unix Shell Script?

Well the solution is simple:

cd source/
for file in *.txt
do
   mv "${file}" ../destination/"${file}.backup"

Run a loop for each txt file in source/ directory and call mv command with parameters.

Does the above problem have a simpler solution than this?

Get our Articles via Email. Enter your email address.

You may also like...

3 Comments

  1. Jadu Saikia says:

    for file in *.txt

    will fail if ‘number of arguments to ls’ is more than its limit. For that

    http://unstableme.blogspot.com/2009/02/handling-argument-list-too-long-bash.html

  2. seen says:

    find . /source -name “*.txt” -exec mv {} ../destination/{}.backup ;\

    we can use this command also..right

  3. yoel says:

    We can use this command also ..right

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.