for script: listing files which have space in filename
Hey
I'm trying to write a script to move files who have modification time of n. I do this my making a file list (using find) and then use a for loop to look through that list and move each file. when I do: find /home/hamish/ -type f -mtime 0 > /tmp/files.txt and then move the files, it works fine for files which have no spaces in the path, eg: "/home/hamish/iskm/admin/clients/idsigns_20050923/iskmltd_richardlee_20051021_pcreport.sxw" But if the path to file has spaces in it, eg "/home/hamish/hamish rocks loads.txt", then when I try to move that entry, the it splits the file into three different files. The error message looks like: ls: /home/hamish/hamish: No such file or directory ls: rocks: No such file or directory ls: loads.txt: No such file or directory Is there anyway that I could enclose the files in quotation marks when I use find so that, when they are read by the for loop, they are enclosed in quotation marks? Thanks hamish |
Use curly braces: FILE="/home/simpsonh/i think i saw beer.txt"; [ -e "${FILE}" ] && echo $LOGNAME spotted beer"
|
What do you mean?
Where should I set FILE= ? At the moment the script looks like: [script] find /home/hamish/ -type f -mtime 0 > /tmp/files.txt for i in `cat /tmp/files.txt`; do ls -lah $i; done [/script] Hamish |
here is the solution:
cat /tmp/files.txt | while read i; do ls -lah "$i"; done hamish |
Notice that both sample scripts in the replies have double quotes around them.
The double quotes prevent the filename from being parsed by the shell as two or more arguments instead of one. This is because a space is the first value of the IFS variable. Code:
for i in `find /home/hamish/ -type f -mtime 0` do Code:
find ./ find /home/hamish/ -type f -mtime 0 -print0 | xargs --max-lines=100 mv --target-dirctory <target-directory> Another thing to try is to have the mv command executed in find using -exec. Notice again the use of double quotes: Code:
find /home/hamish/ -type f -mtime 0 -exec mv -t <dest-directory> "{}" \; |
Hey
thank you very much for the explaination! Have a good weekend. hamish |
All times are GMT -5. The time now is 07:53 AM. |