LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2014, 12:16 AM   #1
Ajay jones
LQ Newbie
 
Registered: Apr 2014
Posts: 3

Rep: Reputation: Disabled
Angry how to make a script...of changing extention


i have four files
file1 file2.old file3old file4

i want to change search for file extention if.. theres already .old then no change.. otherwise change the file with .old extention..

so output should be..
file1.old file2.old(remain unchanged bcz already have it) file3old.old file4.old

>> i want to run script like
scriptname dir
putoldext testdir
so it will change the testdir directories files with old ext.

i tried this..
for file in 'ls .1$'
do echo $file | grep \.old$ > /dev/null
if [ $file != 0 ]
then mv $file $file.old
else echo $file
fi
done

plz help
 
Old 04-01-2014, 02:03 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,109

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
use [code]here comes your script[/code] to keep formatting
please post your script, not only parts, also use set -vx to see what's happening.
Code:
#!/bin/bash
set -xv
for file in 'ls .1$'
  do echo $file | grep \.old$ > /dev/null
  if [ $file != 0 ]
    then mv $file $file.old
  else echo $file
  fi
done
Please try to explain those lines:
what is ls .1$ good for?
and this: echo $file | grep \.old$ > /dev/null
what about this: if [ $file != 0 ] (you ought to use if [ "$file" != 0 ])
 
Old 04-01-2014, 02:05 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Firstly, please use [code][/code] tags around code and data to make them more readable.

As for your script, I think you are over-thinking a little, although the actual code would need to be a little longer.

Looking at your requirements:

pseudo code:

Code:
loop through directory names passed in as arguments
  test if dir is dir, proceed for true or skip to next if not
  loop over contents of dir
    test found item is a file and does not end in '.old', on true add new '.old' extension
I believe all this can be done in bash with the only external command being mv
 
Old 04-01-2014, 09:50 AM   #4
nithinsugathan
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Rep: Reputation: 0
If I am not mistaken you can do this with find command
Code:
find /path/to/files -type f ! -name '*.old' -exec mv {} {}.old \;
 
Old 04-01-2014, 09:07 PM   #5
Ajay jones
LQ Newbie
 
Registered: Apr 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
thanks Pan64!! i got it and it works for me..
thank you so much
 
Old 04-02-2014, 03:17 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
hmmm ... pan64 did not provide a solution so I am curious as to what worked for you?

Please post solution so others may learn from you as well
 
Old 04-03-2014, 01:11 AM   #7
Ajay jones
LQ Newbie
 
Registered: Apr 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
haha... grail..

and somehow i done by myself.. but i got way through his script..
but i appreciated whatever he posted so.. ya i did say it works and actually it does..
thanks again.. pan64

Last edited by Ajay jones; 04-03-2014 at 01:15 AM.
 
Old 04-03-2014, 04:00 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
So again, are you going to post the working version so people may learn as you did?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
compute cluster runs old script after changing the script Reignfire Linux - Server 1 09-27-2012 06:35 AM
how to move 10 files of .dat extention to .txt using single command or script sushil.cool Linux - Newbie 6 11-22-2010 02:40 PM
Changing the default icon for Files with some extention me4linux Linux - Software 2 08-14-2007 03:17 PM
make java prog extention .exe Abdux Programming 3 01-29-2006 10:55 PM


All times are GMT -5. The time now is 05:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration