I have trouble using the find command (I am working on Mac OSX).
Code:
for i in *.pdf
do
if find . -name "EV"
then
mv "$i" E-test.pdf
elif find . -name "GV"
then
mv "$i" G-test.pdf
else
mv "$i" test.pdf
fi
done
As to the use of "$i": unfortunately, the filenames contain spaces (it's not my fault, I am just clearing the mess).
My problem: even if the file name does not contain the string "EV" it is treated as such, i.e. for all the files in that folder only the first "elif" loop is executed. Where is my mistake?
I have trouble using the find command (I am working on Mac OSX).
Code:
for i in *.pdf
do
if find . -name "EV"
then
mv "$i" E-test.pdf
elif find . -name "GV"
then
mv "$i" G-test.pdf
else
mv "$i" test.pdf
fi
done
As to the use of "$i": unfortunately, the filenames contain spaces (it's not my fault, I am just clearing the mess).
My problem: even if the file name does not contain the string "EV" it is treated as such, i.e. for all the files in that folder only the first "elif" loop is executed. Where is my mistake?
Thanks a lot in advance for help!
tempestas
I think you need to explain what you're trying to do.
As long as your have search and read permission in all of the directories in the file hierarchy under your current working directory, the loop that you have is equivalent to:
Code:
for i in *.pdf
do
mv "$i" E-test.pdf
done
which throws away all but last filename in the current directory ending with the string ".pdf" and then renames the last matching filename to be E-test.pdf, except that for each filename ending in ".pdf" it will print a list of the pathnames of all files whose final component is "EV" every time it goes through the loop. (Of course the above analysis assumes that E-test.pdf does not name an existing directory. If it is a directory, you have other problems.)
I think you need to explain what you're trying to do.
I want to rename folders and the respective files in those folders. Problem: both, folders and files, contain funny characters: umlauts and spaces and special characters.
Quote:
As long as your have search and read permission in all of the directories in the file hierarchy under your current working directory
Yes, I do have read and write permissions.
Quote:
, the loop that you have is equivalent to:
Code:
for i in *.pdf
do
mv "$i" E-test.pdf
done
Not exactly. There can be files in these folders containing the string "EV" or "GV", and these files have to be named differently from the other files, i.e. "E-test.pdf" and "G-test.pdf", respectively.
I hope it's now clearer what I am trying to do.
I just don't understand why my first "find" loop does not work.
Your 1. mistake is two nested loops: find (that loops thru the files) within a for loop.
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).
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.
Last edited by MadeInGermany; 11-04-2013 at 09:27 PM..
I want to rename folders and the respective files in those folders. Problem: both, folders and files, contain funny characters: umlauts and spaces and special characters.
Yes, I do have read and write permissions.
Code:
for i in *.pdf
do
mv "$i" E-test.pdf
done
Not exactly. There can be files in these folders containing the string "EV" or "GV", and these files have to be named differently from the other files, i.e. "E-test.pdf" and "G-test.pdf", respectively.
I hope it's now clearer what I am trying to do.
I just don't understand why my first "find" loop does not work.
Any ideas?
Thanks a lot in advance!
tempestas
The loop I showed you above may not be what you want to do, but it is EXACTLY what the code you provided will do!
The command:
Code:
find . -name "EV"
finds and prints the pathname of every file in and under the current directory where the final component of that pathname is EV. (Not a name containing EV; a name that is exactly EV.) And the exit status from that find command will be zero no matter how many pathnames it prints. So, whether or not find finds any files named EV, it won't affect the behavior of the rest of your script.
By convention, a filename ending with .pdf should be a regular file encoded in the portable document format invented by Adobe. If your filenames follow this convention, there is absolutely nothing in your script that will rename any directories. Furthermore, with the code you have shown us, as I said before, every file in the file hierarchy rooted in the current directory whose name ends with .pdf will be moved (one at a time) to E-test.pdf until you only have one filename ending with .pdf and that file will be named E-test.pdf.
Please show us a list of directories and files in those directories and explain what changes to those files you want your script to perform. If we can figure out what you want to do, maybe we can help you write a script that will do it.
But, trust us, the script you showed us will not do anything at all like what you seem to want this script to do.
#!/bin/bash
pathscripts="$PWD"
cd ..
cd Allfolders
if [[ ! -d $1 ]]
then
echo "$1 does not exist. Exiting..."
echo "check $1" >> ../scripts/logfile.txt
exit 1
else
mv "$1/" $2
lastname=`echo "$2" | cut -d'_' -f1`;
d=`echo "$3" | cut -d'.' -f1`
m=`echo "$3" | cut -d'.' -f2`
y=`echo "$3" | cut -d'.' -f3`
cd $2
for i in *.pdf
do
if find . -name "EV"
then
mv "$i" xx_EV_${lastname}.pdf
elif find . -name "*GV*"
then
mv "$i" xx_GV_${lastname}.pdf
else
mv "$i" xx_V_${lastname}_$3_$y$m$d.pdf
fi
done
fi
cd $pathscripts
And the respective file containing the arguments:
Code:
#!/bin/bash
./rename.sh "M++ller Bert" Mueller_Bert 20.02.2013 AB
./rename.sh "N+ñme Firstname + Secondname Secondfirstname" Naeme_Firstname 19.01.2010 CD
./rename.sh "Random Na-me" Random_Name 15.03.2007 CD
./rename.sh "Specialist S+Âhnke" Specialist_Soehnke 17.09.2009 AB
I would have preferred a more general approach just like:
Code:
for folder in Allfolders/*; do
if [[ -d "$folder" ]]; then
newname=`echo "$folder" | sed 's/ /_/g;s/ä/ae/g;s/Ä/Ae/g;s/ü/ue/g;s/Ü/Ue/g;s/ö/oe/g;s/Ö/Oe/g;s/ß/ss/g;s/+/-/g;'`
mv "$folder" "$newname"
fi
done
But this does not work due to encoding problems. I took the folder structure zipped from a windows server. If I unpack it using the Mac OSX tools, it would ignore the umlauts, just convert spaces to "_". So, I unpacked the zip folder on a windows machine and so I can at least convert the umlauts.
I hope I could make it clear what the situation is and what I want to get in the end.
Thank you SO much for advices and help!
tempestas
PS: the .pdf files are created from scans, i.e. these are images.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)