LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-17-2012, 02:51 AM   #1
Simon1984
LQ Newbie
 
Registered: May 2012
Posts: 6

Rep: Reputation: Disabled
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.
 
Old 05-17-2012, 03:08 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,010

Rep: Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627
mv olddir/oldname newdir/newname should work in one step
 
Old 05-17-2012, 07:20 AM   #3
Simon1984
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-17-2012, 07:25 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,010

Rep: Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627
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.
 
Old 05-17-2012, 07:30 AM   #5
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
Ignore this, misread the OP.

Last edited by fukawi1; 05-17-2012 at 07:36 AM.
 
Old 05-17-2012, 08:13 AM   #6
Simon1984
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
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?
 
Old 05-17-2012, 08:35 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,010

Rep: Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627
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
 
Old 05-17-2012, 07:24 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,405

Rep: Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783
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
Code:
#!/bin/sh
set -xv
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*
 
Old 05-17-2012, 08:07 PM   #9
Simon1984
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
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!
 
Old 05-18-2012, 03:13 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,010

Rep: Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627Reputation: 7627
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.
Old 05-18-2012, 11:50 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
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.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Check folder for specific files than move script BlackCrowe Programming 3 11-23-2010 06:59 AM
Write a script to move specific files in various folders to one folder linuxisthedevil Linux - General 13 11-18-2010 07:29 AM
[SOLVED] I need a script that will move all 30 days old files in a folder ericczed Linux - Newbie 3 01-23-2010 09:31 AM
[SOLVED] Bash script needed to move files to another folder. No_one_knows_me Linux - General 28 01-07-2010 11:29 PM
Script to Move and rename files scribbl35 Linux - Newbie 1 07-18-2007 10:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:27 PM.

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