I would not do a for loop because of the ability to overload your command line. Command lines have a limited length, and if you overload it, it will simply turn off the excess without giving you any warnings. It can work if your search returns 100 files. It may work if it returns 1000 files, but may be unsuccessful if your search returns 1000 files and you will never know.
The best way to handle this is to direct the search into the read expression as glenn jackman .
The sed command only works with STDIN and files, but not with file names, so if you want to change the file name, you need to do something like this:
$newname="$(echo $oldname | sed 's/old/new/')"
to get the new file name. The $() construct executes the command and puts the results of the command in STDOUT.
So your script will look something like this:
find /home/devel/stuff/static/ -name "*.pdf" | while read $file do echo $file; newfile="$(echo $file | sed -e 's/static/changethis/')" newfile="$newfile.xml" echo $newfile; done
Now, since you are renaming the file directory, you need to make sure that the directory exists before you move or copy:
find /home/devel/stuff/static/ -name "*.pdf" | while read $file do echo $file; newfile="$(echo $file | sed -e 's/static/changethis/')" newfile="$newfile.xml" echo $newfile;
Pay attention to the quotation marks to handle in this case a space in the file name.
By the way, instead:
if [ ! -d "$dirname" ] then mkdir -p "$dirname" fi
You can do it:
[ -d "$dirname"] || mkdir -p "$dirname"
|| means the following command is executed only if the test is invalid. Thus, if [-d "$ dirname"] is a false statement (the directory does not exist), you run mkdir .
This is a fairly common shortcut when viewing shell scripts.