LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-21-2005, 01:13 PM   #1
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Rep: Reputation: 30
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
 
Old 10-21-2005, 01:43 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Use curly braces: FILE="/home/simpsonh/i think i saw beer.txt"; [ -e "${FILE}" ] && echo $LOGNAME spotted beer"
 
Old 10-21-2005, 02:10 PM   #3
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Original Poster
Rep: Reputation: 30
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
 
Old 10-21-2005, 02:17 PM   #4
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Original Poster
Rep: Reputation: 30
here is the solution:

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

hamish
 
Old 10-22-2005, 12:02 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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> "{}" \;

Last edited by jschiwal; 10-22-2005 at 12:03 AM.
 
Old 10-22-2005, 09:25 AM   #6
hamish
Member
 
Registered: Aug 2003
Location: Edinburgh
Distribution: Server: Gentoo2004; Desktop: Ubuntu
Posts: 720

Original Poster
Rep: Reputation: 30
Hey

thank you very much for the explaination!

Have a good weekend.

hamish
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
putty display loops on listing directories with many files, through expect script vineet.k Linux - General 5 03-13-2007 09:44 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
Changing to a directory with a space in the filename? guitargeek Linux - General 5 02-13-2003 05:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration