[SOLVED] Shell Script to rename and move files to a different folder in Windows
Linux - NewbieThis 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!
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.
and then later in the script we have a simple move command;
mv $fileName* $archiveFolder
What I need to do is try and change the filename before moving the file to it's new location, by adding the text "CYCLIC_" to the start of the filename, so for the example above the filename would be;
If I understand correctly I should be able to use the mv command to change the name of the file, and then use the mv command again to move the file to the new location. However I'm not able to get the mv command to successfully change the filename.
I've tried a lot of different things today, none of which worked. Instead of going through all my failed attempts I'd like to ask if anyone can show me how they would accomplish this?
I'd appreciate any information anyone could provide.
Cannot create directory `CYCLIC_//cygdrive/z/XXXX': No such file or directory
(where XXXX represents the remaining filepath extension which I've left out of this post)
I've tried to set the new filename as a variable, using the existing filename variable with the additional text in front of it and use the new variable in the mv command, but it seems I'm not doing this correctly because the original filename is not appended to the text CYCLIC_ in the error message.
Can I actually create a variable by concatenating text with the value of another variable?
The message seems like it's trying to use the new filename as the directory to move the file to, how do I get it to treat this as the new filename?
I'd appreciate any advice you have about this. Thanks again.
so first try to not mix filename and fileName and Filename and FileName, they are all different.
second you need to use "$var" instead of just $var, so
mv "$basepath/$filename" "$archiveFolder/$filenameNew" can help you
1. Expanding on pan64; Linux (all 8nix) is case sensitive for just about everything(!).
2. here's a good cmd line tutorial that may help http://rute.2038bug.com/index.html.gz
3. to see what's really going on, amend top of shell prog to use
The 2nd line shows you what the parser is doing
Incidentally, the 'sh' shell is a bit more limited than 'bash', which is what most people use. You need to be aware in case someone suggests a cmd that doesn't seem to work for you; they may be assuming bash.
There's no harm in trying it with bash instead thus
Also, it would be good to know exactly what version of Linux you have.
Can you run these
I'll take a look at the cmd line tutorial today, I'm sure I will need it moving forward.
I'm not sure how to chech which version of Linux I have, since the shell scripts are being run through Windows Command Prompt. Would I just create a new shell script with that code and run it in the command prompt? Sorry if that's a silly question, as I mentioned I really don't know very much about this topic at all!
for i in `ls A[1,2,3,4]_*[S,M,D,9].PS 2> /dev/null`
Never parse ls for filenames, and never ever read lines with for. If your file list isn't too long, then you can simply use the globbing patterns straight. But if there are too many, the expanded list might overrun the capacity of the shell to handle them, and you'll have to use something else, such as storing them in an array first, and then looping over the list with a c-style for loop. Or perhaps find and a while+read loop instead.
For modifying the filenames, there are quite a few features available to the shell:
In general, you'll probably want to go through these steps, at a minimum:
1. Generate/read the list of filenames you want to process.
2. Split the name string into separate path and filename variables.
3. Modify the filename to suit your requirements.
4. Create/check the target directory you want to move them to.
5. Move the file from oldpath/oldfilename to newpath/newfilename.
I recommend the Bash Guide here for a good, readable primer on all the basics. The related faq and pitfall pages are also very useful for avoiding specific problems.