Solved

renaming all files in a directory

Posted on 2004-08-10
9
556 Views
Last Modified: 2013-12-26
Hi,
I am using  Korn as my shell. I need a loop that will read all the files in my current directory and rename them by attaching the extension .log. I need to do this pretty soon, so any help early would be greatly appreciated. This might be a trivial problem, but for a newbie like me, its a huge task, and i will learn a lot.

Thanks,
idiottoo
0
Comment
Question by:miteshn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 11

Accepted Solution

by:
avizit earned 50 total points
ID: 11769093
#!bin/ksh
for file in *
do
    mv $file $file.log
done
0
 
LVL 11

Expert Comment

by:avizit
ID: 11769101
basically it loops through all the files in the current directory  ( for  file in * )
and moves each file to file.log  ( mv $file $file.log )

0
 
LVL 38

Assisted Solution

by:yuzh
yuzh earned 50 total points
ID: 11770414
If you want to rename all files undercurrent dir only (excluding subdir), you can do:

find ./* -pune -type f | xargs -i mv {} {}.log

Put in a script:

#!/bin/ksh
cd /mydir
find ./* -pune -type f | xargs -i mv {} {}.log
exit

To rename all the files under current dir (including subdir), you do:

#!/bin/ksh
cd /mydir
find .  -type f | xargs -i mv {} {}.log
exit

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 100 total points
ID: 11772362
# just in case you have mmv:
mmv '*' '#1.log'
0
 
LVL 6

Assisted Solution

by:Talmash
Talmash earned 50 total points
ID: 11775046
hi ,

ahoffmann , what is mmv , nice functions I see .

avizit script may change subdirectories' names .

to avoid , use Yuzh's solution with this fix (add maxdepth)


#!/bin/ksh
cd /mydir
find .  -type f -maxdepth 1| xargs -i mv {} {}.log
exit

tal
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 100 total points
ID: 11777310
mmv - multiple move
mainly available ob Linux (not shure for *BSD)
0
 
LVL 38

Expert Comment

by:yuzh
ID: 11779895
"to avoid , use Yuzh's solution with this fix (add maxdepth)"

Not all *nix "find" support "maxdepth",
    find ./* -pune -type f
will work!
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11781054
s/-pune/-prune/g

;-)
0
 
LVL 38

Expert Comment

by:yuzh
ID: 11781145
ahoffmann,
     Thanks for the correction, I kown how I miss the "r" in the 1st place, the 2nd one is
just a stupid CUT and PAST!
     ok, let's run:
     s/-pune/-prune/g

     Cheers!
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question