LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2006, 11:48 AM   #1
ANU
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Solaris 10
Posts: 78

Rep: Reputation: 15
shell programming - removing files with spaces


im trying to make a script that runs in crontab and removes the older file if the directory exceeds a specified amount of space

targetdir=/var/big/

in this directory i have some logs that have also names with spaces and this i cant delete with the method i have tried
i have done something like this
Code:
nofiles=`ls | wc -l`
if [ $nofiles -ne 0 ] ; then
listf="`ls -tr | head`"       #to get the older files, and then 
echo "  - deleting $listf"
/bin/rm -fr $listf
else
echo "blabla"
fi
so if i have some files with spaces in there names they wont be removed and the script will hang
how can i make it delete this files to?
at first i was thinking to put the filename between "", but its not working

thanks in advance
BR
 
Old 08-07-2006, 12:34 PM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I think it's better to replace "ls -tr" with "ls -1tr", and then the following should do:
Code:
echo "$listf" | xargs -d $'\n' /bin/rm -fr
 
Old 08-07-2006, 12:40 PM   #3
exman
LQ Newbie
 
Registered: May 2006
Location: Germany, BS
Distribution: Debian Kernel 2.6.15, Kubuntu Dapper
Posts: 24

Rep: Reputation: 15
Hi
try to surround the filename with '. like rm -rf 'foo bar'
exman
 
Old 08-08-2006, 03:47 AM   #4
ANU
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Solaris 10
Posts: 78

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by spirit receiver
I think it's better to replace "ls -tr" with "ls -1tr", and then the following should do:
Code:
echo "$listf" | xargs -d $'\n' /bin/rm -fr
i dont know the -d option from xargs... and i dont have it ...
xargs: illegal option -- d

you gave me the idee with the xargs, and i came up to this
Code:
echo "$listf" | xargs -I {} -t /bin/rm {}
what do you think .. is there a better way?

Last edited by ANU; 08-08-2006 at 04:30 AM.
 
Old 08-08-2006, 04:42 AM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Quote:
Originally Posted by ANU
i came up to this
Code:
echo $listf | xargs -I {} -t /bin/rm {}
As you see from the -t switch, this causes /bin/rm being executed for each argument separately, but there's nothing wrong with that of course.
An alternative would be to use either the "-Q" or "--quoting-style=shell" switch of ls.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting and Files With Spaces Matir Programming 12 08-17-2005 01:43 AM
problems in removing white spaces from string of text monil Programming 7 03-08-2005 11:28 AM
Remove white spaces in HTML files using shell : Possible???????? cpanelskindepot Programming 2 08-05-2004 11:14 PM
Removing spaces from string qcoder Programming 3 07-05-2004 12:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:55 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