Solved

renaming all files in a directory

Posted on 2004-08-10
9
540 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.

820 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