Answer the question
In order to leave comments, you need to log in
Problems with mv being called from a script?
There is a script that transfers files to ftp and then moves them to a separate folder
Due to the fact that file names can unpredictably have spaces and all sorts of special characters in the name, the file name is taken in single quotes for convenience by find's output
From the mv command line ' file1' 'file2'
target_folder succeeds with filenames containing any characters in the name, including spaces
files_xml=$(find $FROM -maxdepth 1 -name "*.xml" -mmin +1 -printf "'%f' ")
if [ -n "${files_xml}" ]; then
echo "XML Files found, transferring"
sendToFTP "$files_xml_escaped" && mv $files_xml $SENTPATH
else
echo "XML files not found"
fi
Answer the question
In order to leave comments, you need to log in
when you write on the Bash mv 'file.xml' 'new-file.xml'
command line, it is Bash that "expands" the quotes and starts mv already with parameters in them:
when you call in the scriptmv $file ${new_file}
,
where $file='file.xml' (contains quotes)
then mv starts with a file name containing quotes, but there is no such file ... (actually, this is what abcd0x00 said )
I didn’t really understand what the script does from the question, but I think this example contains the necessary:
- find -print0 ... xargs -0 guarantees the correct processing of special characters
- a separate function - process input data
send_and_move()
{
stat "[email protected]" && echo mv "[email protected]" /tmp
}
export -f send_and_move
find -print0 | xargs -0 --no-run-if-empty bash -c 'send_and_move "[email protected]"'
If there is a possibility of any special characters in the file names, then it is worth executing the script in perl or python. Well, if it's important, then you can do this (but there should not be single quotes in the file name):
files_xml=$(find $FROM -maxdepth 1 -name "*.xml" -mmin +1 -printf "'%f' ")
if [ -n "${files_xml}" ]; then
echo "XML Files found, transferring"
sendToFTP "$files_xml" && echo $files_xml | sed "s/'\s'/\n/g" | tr -d "'" | xargs -I{} mv {} $SENTPATH
else
echo "XML files not found"
fi
Here's an example of how it's usually done.
You find the files you need, and then sed to compose a command (one or more) with them. When the command is ready, pipe it to sh.
[[email protected] t]$ ll
итого 4
-rw-rw-r--. 1 guest guest 0 сен 30 13:08 a a.txt
-rw-rw-r--. 1 guest guest 0 сен 30 13:06 a.txt
-rw-rw-r--. 1 guest guest 0 сен 30 13:06 b.txt
-rw-rw-r--. 1 guest guest 0 сен 30 13:06 c.txt
drwxrwxr-x. 2 guest guest 4096 сен 30 13:19 d
[[email protected] t]$
[[email protected] t]$ ls
a a.txt a.txt b.txt c.txt d
[[email protected] t]$
[[email protected] t]$ files=$(find -name '*.txt')
[[email protected] t]$
[[email protected] t]$ echo "$files" | sed 's/.*/mv "&" d/'
mv "./b.txt" d
mv "./a.txt" d
mv "./c.txt" d
mv "./a a.txt" d
[[email protected] t]$
[[email protected] t]$ echo "$files" | sed 's/.*/mv "&" d/' | sh
[[email protected] t]$
[[email protected] t]$ ls
d
[[email protected] t]$ ls d
a a.txt a.txt b.txt c.txt
[[email protected] t]$
[[email protected] t]$ ll d
итого 0
-rw-rw-r--. 1 guest guest 0 сен 30 13:08 a a.txt
-rw-rw-r--. 1 guest guest 0 сен 30 13:06 a.txt
-rw-rw-r--. 1 guest guest 0 сен 30 13:06 b.txt
-rw-rw-r--. 1 guest guest 0 сен 30 13:06 c.txt
[[email protected] t]$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question