Solved

renaming all files in a directory

Posted on 2004-08-10
9
549 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
notReplace  challenge 53 153
sum28 challenge 31 117
pre4 challenge 19 116
Problem to App 4 145
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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