LinuxQuestions.org
Help answer threads with 0 replies.
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 01-23-2012, 11:03 AM   #1
iwitham
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Rep: Reputation: Disabled
Bash - to remove files with spaces in name


I have written a script that I run as a cron job that is supposed to read the files of a directory and remove those older then 48 hours. The script removes most of the files just fine except for files with spaces in their names. Here is the script:

#! /bin/bash
set -o errexit
set -o nounset

# Get a list of files that meet the critera for removal.


for file in `find /My_directory/ -type f -print0 -mtime +2`;
do rm -f "$file";
done

Can anyone explain to me what I have missed?

Iry
 
Old 01-23-2012, 11:36 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
The problem is that bash will split each filename with spaces into multiple arguments.
Doesn't something like this work?

Code:
find /My_directory/ -type f -print0 -mtime +2 -exec rm -f {} \;

Last edited by millgates; 01-23-2012 at 12:10 PM. Reason: typo
 
Old 01-23-2012, 11:43 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Yes, it's the space in the name...

either find ... -exec rm -f {} \;
or find ... -print0 | xargs -0 rm -f
 
Old 01-23-2012, 12:14 PM   #4
iwitham
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thans for the help. I modified my script to find /My_directory/ -type f -print0 -mtime +2 -exec rm -f{} \; and it works great.
 
Old 01-23-2012, 12:52 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


The problem with your original attempt is explained in detail here:

http://mywiki.wooledge.org/DontReadLinesWithFor
http://mywiki.wooledge.org/BashFAQ/001

And to be sure that you understand how the shell handles arguments and whitespace:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Last edited by David the H.; 01-23-2012 at 12:53 PM. Reason: added link
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to handle files with spaces in their names in bash? cgcamal Programming 6 04-02-2011 05:42 AM
Bash Command to Remove Spaces CincinnatiKid Linux - General 16 09-18-2010 09:46 AM
Bash script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 12:28 PM
Remove white spaces in HTML files using shell : Possible???????? cpanelskindepot Programming 2 08-05-2004 11:14 PM

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

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