We help IT Professionals succeed at work.

renaming files in a directory with a shell script

2,881 Views
Last Modified: 2013-12-26
I need to create a UNIX shell script that takes a directory as an argument, identifies all the files with 0 bytes, prompts the user if it is ok to rename the file, and copies the file adding a .old extension.

So far I know just pieces:

#!/bin/sh

for filename in `ls $Dir'
do

if [ -s $filename ]
then

echo 'do you want to change $filename to $filename.old?"
read answer

if [ "$answer" = y ]
then
cp  $Dir/$filename $Dir/$filename.old

fi

done


Thanks for your help!
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
If $file is 0 bytes, then cp $file $file.old could also be rm $file.old && touch $file.old
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.