Problem with find


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with find
# 8  
Old 11-05-2013
I would concentrate on systematic changes, like changing spaces to underscores:
Code:
find . -depth -print |
while IFS="" read -r name
do
  newname=`printf "%s\n" "$name" | sed 's/ /_/g'`
  if [ "$name" != "$newname" ]
  then
    echo mv "$name" "$newname"
  fi
done

Remove the echo to really execute.
# 9  
Old 11-05-2013
If you've got Umlaute from a windows machine, they won't be Umlaute on a *nix machine any more, esp. if that machine uses the unicode encoding. Try sth. like dos2unix, iconv, or recode to reliably change encodings. For those filenames, you better create a listing in a text file, convert that and then use the result for the new filenames in renaming.
# 10  
Old 11-05-2013
Hi,

Code:
If you've got Umlaute from a windows machine, they won't be Umlaute on a *nix machine any more, esp. if that machine uses the unicode encoding.

I thought so too but I could not rename them. I tried
Code:
convmv

but it didn't do the trick. But I think the problem is not the windows machine or unix but the program in which the folders/ files are stored previously. The workaround (which works for renaming the folders) is the list containing all arguments.

But I still have the problem that my script does not work with respect to renaming the file sin the folders.

Quote:
Your 1. mistake is two nested loops: find (that loops thru the files) within a for loop.
I did remove the
Code:
for

loop. But it does not really help.

Quote:
Your 2. mistake: find's exit status is independent of found files. if find ... is nonsense unless output is tested (e.g. with test or grep command).
I don't understand that. I thought it means if there is a file name containing the string "EV" then do something. But obviously this is ignored. But why? I fear I missed some basic to understand. Please explain.

Quote:
Your 3. probable mistake: if two or more files in a directory match one condition, the mv to a common file will replace i.e. lose the previous files.
How can I circumvent it? Using a counter?

Thanks a lot for explanations and help!


tempestas
# 11  
Old 11-05-2013
find will return 0 even if it finds nothing - so if find ... is also true even if it finds nothing. You'll only get an error status if it's invoked incorrectly, or fails when processing some files it actually did find. You could pipe the find output into wc -l and check that's greater than 0, but I'm not sure that's what you want to do anyway.

If I understand your intent correctly, then in your *.pdf for loop you just want to check if the filename contains EV or GV. You don't need find for that - you already have the filename in $i so you can just testecho "$i" | grep EV (or similar).
# 12  
Old 11-05-2013
Try replacing your current rename.sh script with:
Code:
#!/bin/bash
IAm=${0##*/}
cd ../Allfolders || exit 2
if [[ ! -d "$1" ]]
then    printf "%s: % does not exist.  Exiting...\n" "$IAm" "$1" >&2
        echo "check $1" >> ../scripts/logfile.txt
        exit 3
fi
mv "$1/" "$2"
lastname="${2%%_*}"
ymd="${3:6}${3:3:2}${3:0:2}"
cd "$2" || exit 4
ec=0
for i in *.pdf
do      if [[ "$i" == *EV* ]]
        then    if [[ ! -e "xx_EV_${lastname}.pdf" ]]
                then    mv "$i" "xx_EV_${lastname}.pdf"
                else    printf "%s: %s not overwritten by %s\n" "$IAm" \
                                "xx_EV_${lastname}.pdf" "$i" >&2
                        ec=1
                fi
        elif [[ "$i" == *GV* ]]
        then    if [[ ! -e "xx_GV_${lastname}.pdf" ]]
                then    mv "$i" "xx_GV_${lastname}.pdf"
                else    printf "%s: %s not overwritten by %s\n" "$IAm" \
                                "xx_GV_${lastname}.pdf" "$i" >&2
                        ec=1
                fi
        else    if [[ ! -e "xx_V_${lastname}_$4_$ymd.pdf" ]]
                then    mv "$i" "xx_V_${lastname}_$4_$ymd.pdf"
                else    printf "%s: %s not overwritten by %s\n" "$IAm" \
                                "xx_V_${lastname}_$4_$ymd.pdf" "$i" >&2
                        ec=1
                fi
        fi
done
exit $ec

This has not been thoroughly tested, but may provide some insight into something that might work for you.
# 13  
Old 11-06-2013
Hello,

Thanks SO much! It does work!
Unfortunately, I don't understand every step. May I ask for explanations?
In particular: IAm=${0##*/}
I have never seen this. If I echo $IAm the output is the name of the script that is called by the list, i.e. "rename.sh". What are the ##*/ meaning? 0 marks the argument from the list, right?

The other thing I don't get are the exits exit 2; exit 3; exit 4. Why are they numbered?

lastname="${2%%_*}"
2 again stands for the argument, right? What is the %%_* saying?

ymd="${3:6}${3:3:2}${3:0:2}"
First, I thought I understood for the day that the first number in the brackets {3:0:2} refers to the argument from the list, and 0:2 means the first 2 characters of the argument. But this logic does not seem to work on {3:3:2} and {3:6}. What is the logic behind?

And last but not least, the if statements and the counter.
if [[ ! -e "xx_EV_${lastname}.pdf" ]]
asks if the correct named file already exists, i.e. this is to circumvent that an already processed file is overwritten, correct? Why do you use -e instead of -f? What is the advantage?
And the counter. Why is it set to "1" but never tested again? I always thought you would use a counter in order to test whether it is less than or greater than and make it part of the if condition.

Sorry for asking so many questions. But I would like to understand how the script works so that for next purpose I know better where to start.

Thank you SO much again!

tempestas
# 14  
Old 11-06-2013
Quote:
Originally Posted by tempestas
IAm=${0##*/}
I have never seen this. If I echo $IAm the output is the name of the script that is called by the list, i.e. "rename.sh". What are the ##*/ meaning? 0 marks the argument from the list, right?
http://tldp.org/LDP/abs/html/paramet...stitution.html.

${var##pattern} and ${var%%pattern} remove the matching pattern from the left and right (respectively) of var. ${var:pos:len} gives len characters of var starting from index pos.

Quote:
The other thing I don't get are the exits exit 2; exit 3; exit 4. Why are they numbered?
Anything non-zero is failure (usually), but different codes let you implement different handling for specific errors when you check the return value of the script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with find command "find: cannot open"

Hello Guys , I am trying to run below find command in one of my SH script on a HP UX machine. find /tmp -type f -name "MGCA*.log" -prune -exec rm -f {} \; 2>&1 I want this to check my tmp directory and delete MGCA log files .But below error message is printed on Promt :- find: cannot... (2 Replies)
Discussion started by: himanshu sood
2 Replies

2. Shell Programming and Scripting

find problem

Hi I want to find files with size +10000M and specified directorys except when i use more than one `-name` acton is fail I try -a (and) and -o (-or) but results fail :( find . \( -name ./oracle* -prune -o -name "*arc*" -prune -o -name "*oracle*" -prune -o -size +10000k \) -exec ls -lh {}... (7 Replies)
Discussion started by: ygemici
7 Replies

3. Shell Programming and Scripting

find problem

for index in `find /root -name *.jar | grep "/lib"`; do echo "$index"; done sometimes its throwing error saying find: paths must precede expression Usage: find some1 suggested me that : Code: for index in `find /technologies -name '*.jar' | grep "/lib"` do echo "$index"... (2 Replies)
Discussion started by: crackthehit007
2 Replies

4. Shell Programming and Scripting

Problem with find command.

I'm trying to display the full file name (including the full path) and file size of all files whose name (excluding the path) is longer than 10 characters. I came up with find path -type f -name ".{10, }" -printf "%s %p\n", but I'm getting a "find: path: No such file or directory". What's wrong... (2 Replies)
Discussion started by: raidkridley
2 Replies

5. Shell Programming and Scripting

Find command problem

Hi All, I am using following find command to delete the records older than 7 days but getting missing conjuction error.Kindly suggest: The command is: find <complete_dir_path> \(! -name usr -prune \) -type f -name "*.txt" -mtime +6 -print | xargs rm (11 Replies)
Discussion started by: visingha
11 Replies

6. UNIX for Dummies Questions & Answers

problem with find and mtime

I am using HP-UNIX , The below command doesnt display anything although i have changed a file in the directory by toutch -t 200010101800 nfile find /tmp/transfer/ -name "*.*" -mtime +1 Any problrm with the find command i written . .Please help ??.. Thanks, Arun (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

7. UNIX for Dummies Questions & Answers

Problem with find and tar

When I am doing the first command the result shows all the files, links, directories except the ones that contain the word logs find . -type f -o -type l -o -type d | grep -v logs But when I am trying to do this even the logs are getting tarred tar -cvf fdtvision.tar `find . -type f -o -type l... (2 Replies)
Discussion started by: venu_nbk
2 Replies

8. Shell Programming and Scripting

problem with find

Hi, Iam having a strange problem, wandering if soneone can throw some lights. I have statement find . -maxdepth 1 -name 'File1*.tsv' -mtime +1 -print I expect the above statement to print the files older than 1 day or 24 hrs, however it doesn't work that way. When issue above command,... (2 Replies)
Discussion started by: braindrain
2 Replies

9. Shell Programming and Scripting

Problem with `find ...`

Hi all, Have the following snippet of code that I'm having trouble trying to work ... The snippet of code is running on our Production Server and the intent is to copy the second most recent IDE file across from the Development Server. I have the following files defined in $DEVLOC ...... (4 Replies)
Discussion started by: Cameron
4 Replies

10. Shell Programming and Scripting

Another Find Problem

HI All, I am having a bit of trouble using the find command in my shell (korn) script. I was hoping someone could help me. I am trying to build up a dynamic find command based on some parameters. When I execute the command I get the following error: find: incomplete statement I have... (6 Replies)
Discussion started by: sethkor
6 Replies
Login or Register to Ask a Question