ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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 %%)
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.
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.
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
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.