LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2013, 07:08 AM   #1
khaled al-sowadi
LQ Newbie
 
Registered: Jun 2013
Posts: 2

Rep: Reputation: Disabled
change file name in bash


dears.

i have some files come to my server (linx)
these files came with ;1 For example (TTFILE006057;1). the ;1 i don't need it.
so i made a script for it
rename.sh

#!/bin/bash

for i in rename.sh;
do mv "/oracledb/mediation/khaled/TTFILE006057;1" "/oracledb/mediation/khaled/TTFILE006057"
done

it's working but there is a lot of files with same beginning but with different end.
for example (TTFILEXXXXXX;1) the xxxxxx is changing number.
so can any one modif it please.

and i want this script keep working all time and work as loop.

also i want to add the file name after changeing to another .txt file

please help
 
Old 06-20-2013, 08:14 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
hmmm ... what is the purpose of your for loop? As it stands it has no use as the move (mv) command does not call the 'i' variable and you are looping based on the name of the script you are in.

You can maybe see that this is not of any real use to you.

If you assume the location does not change then you can actually call your loop with the path to your files and then call the move command and reference your variable as it will be the name
of your file at the end of the path you list. Then you can alter variable to remove the end portion, for this I would suggest parameter substitution, found here (look for the section that refers to % and %%)

Let us know how you go?
 
Old 06-20-2013, 08:41 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Use utility find to find the files in question:

Code:
find path -name 'TTFILE*;1' | while read oldname; do
(process one file)
done
(For extreme filenames:
Code:
find . -name 'TTFILE*;1' -print0 |  while read -d '' -r oldname; do
...
done
)

Last edited by NevemTeve; 06-20-2013 at 08:45 AM.
 
Old 06-20-2013, 08:53 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I prefer grail's hint

Code:
path="/oracledb/mediation/khaled"
ext="\;1"
for i in $path/*$ext;do
    ...
done

Last edited by Firerat; 06-20-2013 at 08:59 AM.
 
Old 06-20-2013, 09:38 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Off: Try it with lots of files.
 
Old 06-20-2013, 11:26 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Quote:
Originally Posted by NevemTeve View Post
Off: Try it with lots of files.
I am curious, why would the number of files make any difference to the for loop??
 
Old 06-21-2013, 04:23 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
You're right, in this context number of files isn't a problem, it works even when commands like '/bin/echo *' give error message "The parameter or environment lists are too long". So I was wrong, sorry.
 
Old 06-23-2013, 09:03 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The only time I would avoid using shell globbing is in certain cases where the resulting expanded list can end up being longer than the kernel's ARG_MAX setting. But that setting is generally large enough now that it's rather uncommon to encounter it. My current ARG_MAX setting is 2097152, or two megabytes of text.

I'm getting "Argument list too long". How can I process a large list in chunks?
http://mywiki.wooledge.org/BashFAQ/095


That said, however, bash at least appears to be able to safely use globbing patterns with it's own built-ins without limitation. It's only when it has to execute an external application with a direct list of arguments that the ARG_MAX becomes a problem. Since the for loop is a built-in function, bash has no problem looping over even very large lists with it.

e.g. Here's an example of what I get with a huge file list (everything in my /lib, three times over).

Code:
#using bash's built-in printf
$ printf '%s\n' /lib/**/* /lib/**/* /lib/**/*
<A honkin' big list of file names>

#with the external printf
$ /usr/bin/printf '%s\n' /lib/**/* /lib/**/* /lib/**/*
bash: /usr/bin/printf: Argument list too long

#using a for loop
$ for fname in /lib/**/* /lib/**/* /lib/**/*; do echo "$fname"; done
<The big list again>

#Confirm that the size of the list > ARG_MAX:
$ printf '%s\n' /lib/**/* /lib/**/* /lib/**/* | wc -c
2834793
 
1 members found this post helpful.
Old 06-24-2013, 03:18 AM   #9
khaled al-sowadi
LQ Newbie
 
Registered: Jun 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
thank you guys

i just made the command to read only the 12 frist diget of file name and rename it
i did this bash and it work will

please check


#!/bin/bash
cd /oracledb/mediation/md_instance/dataProcessors/DataProcessorBS/runContainer/data/input_isc3
for x in `ls TTFILE*\;1`
do
mv $x ${x:0:12} ;
done
 
Old 06-24-2013, 04:50 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Please use [code] and [/code] tags. (You can edit your post if you use the [Edit] button.)

Then replace the cycle:
Code:
don't: for x in `ls TTFILE*\;1`
do:    for x in TTFILE*\;1
Also you can be more flexible in renaming:
Code:
mv -- "$x" "${x%;1}"

Last edited by NevemTeve; 06-24-2013 at 06:34 AM.
 
Old 06-24-2013, 11:18 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
What NevemTeve posted. But to clarify why, as written your loop demonstrates three common pitfalls:

1) Don't Read Lines With For! for loops are safe to use with globbing patterns, but the output of commands or files needs to be processed with a while+read loop.

2) Don't parse ls for filenames or metadata. Not only is it difficult to safely and correctly read the output, but it's also just plain redundant in this case. The shell is the one that's producing the list of filenames with the globbing pattern, and ls is simply acting here as an unnecessary and less-efficient form of echo/printf.

3) QUOTE ALL OF YOUR VARIABLE EXPANSIONS.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

This mostly relates to how the shell processes whitespace and breaks up strings into their final arguments. This is the main problem behind point one as well.


And to finish it off, one more suggestion:

4) $(..) is highly recommended over `..`.

Last edited by David the H.; 06-24-2013 at 11:20 AM.
 
  


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
[SOLVED] bash :: change file value on /sys directory finaccio Programming 4 06-13-2012 09:14 AM
Change executable file color in Bash casela Linux - Newbie 1 03-18-2012 07:30 PM
BASH: How to change a line in file? gmitra Programming 4 01-28-2005 07:26 AM
Change Text-File through bash script arkus Programming 2 12-17-2004 08:48 PM
[Bash] Find occurences in a file, then change them fr0st Linux - General 2 04-05-2004 09:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:48 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