Solved

renaming all files in a directory

Posted on 2004-08-10
9
511 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
  • 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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 100 total points
ID: 11772362
# just in case you have mmv:
mmv '*' '#1.log'
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now