Solved

renaming all files in a directory

Posted on 2004-08-10
9
534 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 92
Define unique primary key 9 93
Complete beginner needs help making a cron job 9 117
haveThree challenge 22 118
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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