Shell script help!
hi all, I am currently trying to create a simple script and having difficulty. The scripts job is to copy all files from a folder in my USB drive into a folder on my Linux virtual machine. Also if the folder in Linux is called "FLD" i'd like the files to be renamed FLD1, FLD2, FLD3 etc etc. Any help would be greatly appreciated. Very new to Linux and need this to work before I can get started on what i'd like to do.
Thanks! |
At first copy the files and next, after that go to directory where files were copied and rename all the files. Renaming "on the fly", I think is not best idea. .
|
Quote:
Code:
TARGET=whatevertargetfolderyouneed The for loop iterates over all files in the current directory. Each file is copied to the target folder, to a file named after the target folder plus a number. The number is then incremented. $(( ...)) is the arithmetic substitution syntax of the bash shell; if you need other shells to interpret this, you may need a different way of incrementing. If your files can start with a dot, the head of the for loop must be altered. |
You may also have a look at this
http://www.linuxquestions.org/questi...ly-4175545992/ paticularly useful can be "rename" small Perl script (assuming you have Perl installed). |
Thanks for your reply guys! Unfortunately the above script didnt work, any ideas why? When i done the exec in console, the console disappeared and nothing happens to my target folders. My directories are called fdp and fdp2 which are located in my root directory:
TARGET=~/fdp i=1 for file in * do cp $file $fdp/$fdp2$i i=$((i+1)) done is there a mistake in the algorithm somewhere? I really appreciate the help guys. |
Quote:
"exec" will essentially replace your interactive shell with the program. When the program is done, the window naturally closes. |
Shell script tips
A couple of tips when creating shell scripts:
Replace the command (i.e. cp above) with echo and run the script to see what it would do. set -x on your new scripts to watch what they actually do Play around to build up your confidence Last, but most important of all Enjoy yourself learning the power of the command line and shell scripts When you get stuck, ask here |
Still no luck getting the script to run :(. I'd like to try getting it done by first running a script to copy all contents from one directory onto the other, and this should be achieved by using
$script ~/FDP ~/FDP2 and then a second script which runs automatically which will rename all files within the directory according to the directory name. Sorry for being such a noob! |
Try this:
#!/bin/bash if [ "$#" -ne 2 ] || ! [ -d "$1" ] || ! [ -d "$2" ]; then echo "Usage: $0 fromDirectory toDirectory" exit -1 fi num=1; for original in $1/*; do extension=${original##*/} ext=${extension##*.} echo copying from "$original" to "$2/$2$num.$ext" #cp "$original" "$2/$2$num.$ext" (( num = $num + 1 )) done exit 0 Sorry, posts lose all indentation :-( When you're happy, uncomment the cp line |
thanks norman, looks like were getting somewhere! however now i am getting error: CP: cannot create regular file /root/fdp2//root/fdp21.filefile : no such file or directory. To load the script i entered " script ~/fdp ~/fdp2
It does however say copying from /root/fdp/term (term being one of the files inside that directory) yet it doesnt actually copy it over. strange! |
normanlinux,
If you wrap your program in code tags i.e. your indentation will not be lost. What you are seeing is just "echo copying from "$original" to "$2/$2$num.$ext"" and not an actual status message from the cp command. There are a few problems with the syntax of the cp command as posted which is why you see the error message. A few more steps are required to parse the destination directory and strip out the fdp2 from /root/fdp2. normanlinux and berndbausch appear to be willing to write the script for you but you should learn it so you know how it works... http://www.cyberciti.biz/open-source...for-beginners/ |
Michaelk: Thanks, unfortunately I used the quick reply :-(
Highland: Sorry, I didn't allow for the longer paths :-( You should look up the syntax for pattern operators then you can fix it. I had to look this up myself as I use zsh which has a simpler (better?) way to do this |
Code:
#!/bin/bash Code:
chmod +x transfer.sh |
*One should rather use readline and 'while' loops to be safe: http://mywiki.wooledge.org/BashPitfalls.
|
Thank you so much for all your help guys, learning so much in very little time.
Quick question, say my directory is called test, and I want the .jpg files in there to be renamed test1.jpg, test2.jpg etc, is there a quick way to do it? Rather than having a variable script that changes it accordingly, just a simple few lines of code? Thanks! |
All times are GMT -5. The time now is 12:32 PM. |