LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-31-2014, 11:16 PM   #1
Ajay jones
LQ Newbie
 
Registered: Mar 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, 01:03 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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, 01:05 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 08: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, 08:07 PM   #5
Ajay jones
LQ Newbie
 
Registered: Mar 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, 02:17 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 12:11 AM   #7
Ajay jones
LQ Newbie
 
Registered: Mar 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 12:15 AM.
 
Old 04-03-2014, 03:00 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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


Reply



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 05: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 01:40 PM
Changing the default icon for Files with some extention me4linux Linux - Software 2 08-14-2007 02:17 PM
make java prog extention .exe Abdux Programming 3 01-29-2006 09:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:31 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
Open Source Consulting | Domain Registration