Beginner - scripting help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Beginner - scripting help!
# 8  
Old 07-22-2015
Thanks, that worked a little, this is what happened: i have two directories, one named test and one named moved.

Data was copied from test to moved succesfully.

Two files were renamed inside the moved folder, they were renamed moved1.jpg and moved3.jpg.

Inside the moved folder there is another folder called test, the files in here were not renamed unfortunately. there is also another folder within test named data, which again was unchanged.

console says:
Code:
cp: omitting directory /root/test/data

any ideas? Thanks for your help

Last edited by highland; 07-22-2015 at 02:47 PM..
# 9  
Old 07-22-2015
There's no reason moved2.jpg should be missing except the file that was encountered in the second place was not a normal file. Directories in the moved folder will NOT be touched; only files copied from the original directory will be renamed when copied.

The message that you post (alas again without code tags) does not match the directory structure that you talked about. I'd propose you elaborate carefully your situation and requirements.
# 10  
Old 07-22-2015
The structure is different as i'd like the script to work whatever the environment, there could be many subdirectories or only one subdirectory for example, i was just testing the script in different variables.
The files within test and data (copied already) did not rename according to the directory names, however the parent directory seemed to work fine!

---------- Post updated at 12:53 PM ---------- Previous update was at 12:46 PM ----------

perhaps the renaming can be achieved during the copy process? Or copying first then renaming after the more reasonable option?

thanks again
# 11  
Old 07-22-2015
Some relevant information is missing.
Files in the "test" sub folder, should they be named "test..."? (Or "moved..." i.e. the parent folder?)
Should the "test" sub folder be created if not yet present?
# 12  
Old 07-22-2015
should be named test... and also yes it should be created SmilieSmilie

---------- Post updated at 04:07 PM ---------- Previous update was at 04:01 PM ----------

I have written up a more detailed explanation of what i need to get done : Image
# 13  
Old 07-23-2015
find is a very good tool for recursion into directories.
The following implementation meets most of your requirements
Code:
#!/bin/bash
# strip a trailing /
src=${1%/}
dest=${2%/}
export PATH=/bin:/usr/bin:/usr/sbin:/sbin
# only a cd and pwd does a reliable pathname resolving
dpwd=$(cd "$dest"&& pwd)
spwd=$(cd "$src" && pwd)
if [ "$spwd" = "$dpwd" ]; then
  echo "src and dest are identical ($dpwd)"
  exit 1
fi

#find "$src" -depth -type f \( -name "IMG*" -o -name "*.???" \) -print |
# -o is or
# -a is default and can be omitted
# here \( -name "IMG*" -name "*.???" \) can be simplified
find "$src" -depth -type f -name "IMG*.???" -print |
while IFS="" read -r sname
do
  # strip the leading src and replace with dest
  ddir=$dest${sname#$src}
  # strip the trailing filename
  ddir=${ddir%/*}
  if [ "$ddir" != "$pddir" ]; then
    # "new" folder: 'find' can visit a folder several times
    pddir=$ddir
    echo mkdir -p "$ddir"
  fi
  # get the filename extension
  ext=${sname##*.}
  # compose the base dest filename
  dname=$ddir/${ddir##*/}
  # find a free number for dname
  CNT=0; while [ -e "$dname$((++CNT)).$ext" ]; do :; done
  echo cp "$sname" "$dname$CNT.$ext"
done

The comments will help you to understand and further adapt this.

Last edited by MadeInGermany; 07-23-2015 at 12:35 PM.. Reason: bugfix: [ -d must be [ -e
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Beginner at bash scripting - need help with passing arguments

I at the moment, making a simple bash script, capable of setting up an workspace for me, so i don't have to do it manually.. Problem is though i can't seem to provide the bash script any argument, without running into my error checks, checking for input... Here is the code: #!/bin/bash... (7 Replies)
Discussion started by: kidi
7 Replies

2. Shell Programming and Scripting

Beginner Bash Scripting Question

Hello, I am new to Linux and studying to become a Unix System Admin. I am taking a course in which I was practicing creating a bash script to ping a particular IP address. The script can be found below: #/bin/bash echo "Enter the IP address" read ip if then ping -c 1 $ip if ;... (3 Replies)
Discussion started by: shah9250
3 Replies

3. Shell Programming and Scripting

Beginner Scripting for school

Hello people, I am new to the forum and to scripting and I'm honored to be a part of the Forum :) At the moment I'm learning to do basic scripting for school. Now I got 2 assignments that I do not understand. The case scripting I have mastered a bit. But now I have an assingment to make with... (1 Reply)
Discussion started by: hulsi88
1 Replies

4. Shell Programming and Scripting

beginner scripting questions User variables

If there's anywhere to look this up, it would be just as helpful. I googled and really couldn't find anything relative to this. ok... General Variables 1) When creating a script I made a file "prog1.sh" does it matter if the end is .sh or is this what has to be done like prog.bash or... (4 Replies)
Discussion started by: austing5
4 Replies

5. OS X (Apple)

scripting beginner

hi i have been given the task of sorting some scripting issues , i first of all need to decifer the ones that are being used is there anybody can do this for me :rolleyes: (4 Replies)
Discussion started by: aguiness
4 Replies

6. Shell Programming and Scripting

[Bash] Beginner at scripting

Hi, I'm a beginner at shell scripting, just started scripting in bash a few days ago. I want to test if the command ls *.jpg returns exit code 2, and if yes I want to execute a new command ls *.jpeg, doing a test on it... and pretty much repeat the procedure. Is this correct? #!/bin/bash... (1 Reply)
Discussion started by: Utherr
1 Replies

7. Shell Programming and Scripting

Need help for scripting beginner

hy guys, I have perl script provided to me but i need to convert it into shell .Can you help me in this using sed shell command. cat /etc/passwd |perl -ne '/^(\w+):\w+: (\w+)/ and print "$1, $2\n";' (1 Reply)
Discussion started by: singh_king
1 Replies

8. Homework & Coursework Questions

Beginner Scripting Issue

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The object is to enter a number, then have another classmate guess the entered number. 2. Relevant commands,... (6 Replies)
Discussion started by: Jjohn1987
6 Replies

9. Shell Programming and Scripting

Beginner bash scripting - a few problems

Hey Guys, I am creating a bash script on my freeBSD box, the script should basically ask the user to enter a username and domain. The script will take this information and basically append alot of information to config files so the user can receive email from that domain and create a web site at... (1 Reply)
Discussion started by: traxy
1 Replies
Login or Register to Ask a Question