Deleting the files with spaces


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Deleting the files with spaces
# 1  
Old 06-17-2014
Deleting the files with spaces

Hi All,
The shell script is currently using the below command to find the seven days older files from a directories and delete them.

Issue occurs when the file name consists of space in them. eg: abc def 1245.txt
I need to delete the files names with space also. Please help.
Thanks.

Below is the command which is used:
Code:
find . -type f -mtime +$7 | xargs rm -f


Last edited by abhi_123; 06-17-2014 at 01:19 PM.. Reason: code tagged
# 2  
Old 06-17-2014
Highlight your code and press the code button to mark it as such. It will show up in the editor panel as something like:-
[CODE]my code here[/CODE]

Anyway, the problem here is that the files get passed as plain text to xargs with reads them as such and parses them based on the field separator, which is space, tab or other whitespace by default.

Try this:-
Code:
find . -type f -mtime +$7 -exec rm -f {} \;


Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 06-17-2014
Consider this for goofy file names

Code:
find . -mtime+7 | while read fname 
do
   rm "$fname"
done

# 4  
Old 06-18-2014
If you have GNU find/xargs:
Code:
find . -type f -mtime +$7 -print0 | xargs -0 rm -f

or
Code:
find . -type f -mtime +$7 -exec rm -f {} +

# 5  
Old 06-18-2014
Useful to know, but I have a problem where there is more than one file:-
Code:
# touch "abc 123"

# touch "abc 124"

# ls -l a*
-rw-r--r--. 1 root root    0 Jun 18 12:48 abc 123
-rw-r--r--. 1 root root    0 Jun 18 12:48 abc 124

# ls abc*|xargs rm
rm: cannot remove `abc': No such file or directory
rm: cannot remove `123': No such file or directory
rm: cannot remove `abc': No such file or directory
rm: cannot remove `124': No such file or directory

# ls abc*|xargs -0 rm
rm: cannot remove `abc 123\nabc 124\n': No such file or directory

# find . -name "abc*"
./abc 124
./abc 123

# find . -name "abc*"|xargs -0 rm
rm: cannot remove `./abc 124\n./abc 123\n': No such file or directory

The -0 option in my manual page (RHEL 6) states this:-
Quote:
-0 Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.
I think that it's a good thing to know, but the multiple filenames just get treated as one because the text from the ls or find is taken as a full, literal string. The find option -print0 doesn't help either.




Robin
# 6  
Old 06-18-2014
xargs -0 expects NULL-terminated filenames - which is what find -print0 produces. Naturally it's not going to work if you pass in a list separated by something else...

Code:
$ find . -name "abc*" -print0 | xargs -0 ls -l
-rw-r--r-- 1 carlo.m a 0 Jun 18 13:07 ./abc 123
-rw-r--r-- 1 carlo.m a 0 Jun 18 13:07 ./abc 124


Last edited by CarloM; 06-18-2014 at 10:24 AM..
This User Gave Thanks to CarloM For This Post:
# 7  
Old 06-18-2014
Maybe I should have read the full command first.......Smilie

Yes, the find with -print0 piped to xargs -0 works for me with multiple files too. Smilie


Robin Smilie
This User Gave Thanks to rbatte1 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

2. Shell Programming and Scripting

Deleting newline and making output in single line with spaces

HI I have a file line vi Input 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

3. Shell Programming and Scripting

AIX system.... deleting files in remote directory after retrieving files

Hi Friends, I am new to this , I am working on AIX system and my scenario is to retrive the files from remote system and remove the files from the remote system after retreving files. I can able to retrieve the files but Can't remove files in remote system. Please check my code and help me out... (3 Replies)
Discussion started by: vinayparakala
3 Replies

4. Shell Programming and Scripting

Need help comparing two files and deleting some things in those files!

So I have two files: File1 pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2 pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2 ref4948 1.1 treehouse.txt 1.6 ref8573 1.5 ref3284 1.4 ref5838... (24 Replies)
Discussion started by: linuxkid
24 Replies

5. Shell Programming and Scripting

Deleting lines that contain spaces in a txt file

I need some help deleting lines in a file that contain spaces. Im sure awk or sed will work but i dont know much about those commands. Any help is appreciated :D (7 Replies)
Discussion started by: r04dw4rri0r
7 Replies

6. UNIX for Dummies Questions & Answers

Deleting lines starting with spaces then non-numerals

I did a search but couldn't find a thread that seemed to answer this but my apologies if it has been answered before. I have some text files and I need to remove any line that does not start with a number (0-9). In actuality every line like this starts with a 'T' (or 't') but there are a... (5 Replies)
Discussion started by: skray
5 Replies

7. UNIX for Dummies Questions & Answers

deleting white spaces in a file

Hello Guys, I am a newbie to unix. I am having a requirement. Please help me for finding a solution for this, I am having a file as mentioned below: $ cat shank ackca acackac akcajc akcjkcja akcj ckcklc I want to delete all the white spaces in this file, I tried... (2 Replies)
Discussion started by: mraghunandanan
2 Replies

8. Shell Programming and Scripting

csh script for deleting extra spaces in text file

I am new to scripting and I needed to know if there would be an easy way to delete extra spaces in a text file. I have a file with three rows with 22 numbers each, but there is extra spaces between the numbers when it gets output by this program AFNI that I am using. What script would help delete... (2 Replies)
Discussion started by: hertingm
2 Replies

9. Shell Programming and Scripting

Deleting end line spaces for along file

How can i clear all space characteres for a long file at the end of each line? (3 Replies)
Discussion started by: osymad
3 Replies

10. UNIX for Dummies Questions & Answers

deleting white spaces

How would I delete white spaces in a specified file? Also, I'd like to know what command I would use to take something off a regular expression, and put it onto another. ie. . . . expression1 <take_off> . . . expression2 (put here) . . . Any help would be great, thanks! (10 Replies)
Discussion started by: cary530
10 Replies
Login or Register to Ask a Question