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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
05-17-2012, 02:51 AM
|
#1
|
LQ Newbie
Registered: May 2012
Posts: 6
Rep:
|
Shell Script to rename and move files to a different folder in Windows
Hi there,
I'm very new to shell scripts and have no formal training, I've just been asked to look at some existing shell scripts at work that were created by an old employee.
After browsing through forums all day I've picked up some basic information but I'm having some real trouble accomplishing what I need to do, which I think is a pretty basic task...
The script I'm dealing with does a number of things, but the part of the script I'm interested in is the move command.
We have files sitting in a directory with a filename like this;
A3_CTRRET_369661_M.PS
Near the beginning of the script there is the following code;
fileName=`basename $i .PS`
archiveFolder=$basePath/archive/cyclic
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;
CYCLIC_A3_CTRRET_369661_M.PS
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.
Many thanks.
|
|
|
05-17-2012, 03:08 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,010
|
mv olddir/oldname newdir/newname should work in one step
|
|
|
05-17-2012, 07:20 AM
|
#3
|
LQ Newbie
Registered: May 2012
Posts: 6
Original Poster
Rep:
|
Thanks for your response pan64
I just tried the following;
filenameNew=CYCLIC_$filename
mv $basepath/$filename $filenameNew/$archiveFolder
I get the following error message;
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.
|
|
|
05-17-2012, 07:25 AM
|
#4
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,010
|
would be better to show your script.
You need to create first the target directory (you can do it by yourself or in the script)
you mixed
Code:
mv $basepath/$filename $filenameNew/$archiveFolder
first comes the folder.
|
|
|
05-17-2012, 07:30 AM
|
#5
|
Member
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854
Rep:
|
Ignore this, misread the OP.
Last edited by fukawi1; 05-17-2012 at 07:36 AM.
|
|
|
05-17-2012, 08:13 AM
|
#6
|
LQ Newbie
Registered: May 2012
Posts: 6
Original Poster
Rep:
|
The entire shell script is pretty long and most of it is not relevant to the problem I have, but here are some extracts from the original code that I think are relevant
Code:
#!/bin/sh
basePath="/cygdrive/$DOC1_DRIVE/prodprint/aaspire/conret/"
for i in `ls A[1,2,3,4]_*[S,M,D,9].PS 2> /dev/null`
do
fileName=`basename $i .PS`
plan=`grep -m 1 ",HEAD," $srtFile | cut -d "," -f 8 | sed s/\ //g`
archiveFolder=$basePath/archive/cyclic/`date +%Y`/`date +%Y-%m`/$plan
mv $fileName* $archiveFolder
done
so from this code I've updated replaced the mv command with the code from my previous post, but as per your feedback I switched the target directory and filename variables so the new code is;
filenameNew=CYCLIC_$filename
mv $basepath/$filename $archiveFolder/$filenameNew
now i'm getting an error message that says;
mv: cannot move `/bin' to `/cygdrive/z/prdprint/aaspire/conret//archive/2012/2012-05-17-LESF/CYCLIC_/bin': device or resource busy
it seems like I've done something wrong when trying to set the variable for filenameNew, because it doesn't include the filename variable concatenated with the new text.
any suggestions?
|
|
|
05-17-2012, 08:35 AM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,010
|
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
|
|
|
05-17-2012, 07:24 PM
|
#8
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,405
|
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
[code]
Code:
#!/bin/bash
set -xv
Also, it would be good to know exactly what version of Linux you have.
Can you run these
Code:
uname -a
cat /etc/*release*
|
|
|
05-17-2012, 08:07 PM
|
#9
|
LQ Newbie
Registered: May 2012
Posts: 6
Original Poster
Rep:
|
Thank you pan64 and chrism01, I'm not surprised I as making such a simple mistake as incorrect case in the variable names.
I was able to get it to work by using the following;
mv "$basePath$fileName".PS "$archiveFolder/CYCLIC_$fileName.PS"
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!
|
|
|
05-18-2012, 03:13 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,010
|
uname -a will tell you "which version of OS" you have, it works on cygwin also.
Happy with solution ... mark as SOLVED
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
|
|
1 members found this post helpful.
|
05-18-2012, 11:50 AM
|
#11
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Next, $(..) is highly recommended over `..`. Please don't use backticks. They're harder to read and harder to use.
Third, this is a no-go.
Code:
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:
string manipulation
parameter substitution
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.
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
Last edited by David the H.; 05-18-2012 at 11:54 AM.
Reason: addendum & corrections
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 04:27 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|