ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I understand that I must use the mv command to change the file extension. But I am asking about egrep and expr to change me.cpp to me.out . I do not want to use the mv command. The man command haven't given me any valuable information to assist me with coding. Thanks pixellany
And I told you that grep and expr are not used for changing things!!!
Again, what problem are you trying to solve? ie WHY do you want to do something the hard way? Someone suggested homework......
This is really meta-help: I think what he is saying that he is using (or wants to use) egrep, etc., to help him do the transformation of the name from the old to the new form, not to do the actual renaming.
In any event, we can all think of ways to essentially rename a file without using mv: we could use any of a number of commands to transfer data to a new file name, then use the new name -- cp, cat, etc. -- all could be pressed into service for that.
I think at this point, however, it would be useful for the OP to explain a bit more about his motives ... cheers, makyo
Why everybody assuming it's a homework question. If it was a homework question, I would post the whole problem. I'm asking for a specific thing. The reference books that I have does not explain text substitution/manipulation. I want to incorporate expr or egrep in the code. I know that I can use rename and mv but that is not what I'm asking. I want to change a file me.cpp to me.out using expr or egrep to find the file and changing it.
rename \.cpp \.out *\.cpp # it will affect every file that matches in the directory.
Three arguments to the rename command, the first is the pattern to change
second it the pattern that it is to change to
third is the filename to change.
I have escaped the dot, knowing that your interest seems to be in three character filename extensions characterised by their dot. The inclusion of the dot and the escaping of it should save any accidents in case the pattern cpp should appear in the filename other than after the dot.
I suggest that you practice your skills on individual commands and mechanisms, quietly getting an idea for how they work and explore them. But when you want to perform a useful function, I think you are obliged to respect the fact that most people don't set out to create Heath Robinson code, but to do the job cleanly, efficiently and with as few keystrokes as possible so as not to spill our beer/coke/coffee/tea into the keyboard.
The advice that the Gurus give you here probably reflects best practice so you should heed it and not try to produce laborious code to inefficiently do things that can be done with half a dozen keystrokes. Get into the habit of doing things as efficiently as you are able. Your mechanisms are your own personal programming toys and like personal habits are probably best kept private unless you need help because something has come off in your hand.
Here is my code and I am still having problems getting it to work.
mv "$fname" "$n"
Ok I'm officially confused on what you want to do. Your code uses the 'mv' command. I think we've given you every (or almost every) sane answer there is. egrep and expr do not rename files. That's not the function of those programs (as far as I know). My advice is to just not dwel on this too much, since it seems like a hypothetical situation (from a textbook). Even if you figure out some weird way to pull this off with egrep and expr, you'll never do it that way again (at least I would hope).