LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   for script: listing files which have space in filename (https://www.linuxquestions.org/questions/linux-general-1/for-script-listing-files-which-have-space-in-filename-375485/)

hamish 10-21-2005 01:13 PM

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

unSpawn 10-21-2005 01:43 PM

Use curly braces: FILE="/home/simpsonh/i think i saw beer.txt"; [ -e "${FILE}" ] && echo $LOGNAME spotted beer"

hamish 10-21-2005 02:10 PM

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

hamish 10-21-2005 02:17 PM

here is the solution:

cat /tmp/files.txt | while read i; do ls -lah "$i"; done

hamish

jschiwal 10-22-2005 12:02 AM

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
    for i in `cat /tmp/files.txt`; do ls -lah "$i"; done

Using the -print0 of find in combination with the -0 option of xargs may also be the way to go. You would need to use the --target-directory option of mv.
Code:

find ./ find /home/hamish/ -type f -mtime 0 -print0 | xargs --max-lines=100 mv --target-dirctory <target-directory>
Using the -print0 and the (--null/-0) options you can have names with whitespace, quotes and backslashes according to the xargs manpage. The --max-lines/-l option sets the maximum number of lines handled at one time. This can prevent out-of-memory errors when working with very large directories.

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> "{}" \;

hamish 10-22-2005 09:25 AM

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.