LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
LinkBack Search this Thread
Old 11-21-2005, 09:50 AM   #1
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 1,951

Rep: Reputation: 158Reputation: 158
rm files only - retain directory structure


hi, i am trying to recurse into directories and delete only files.

Code:
 cat set-share.bash
#!/bin/bash

for line in "`find nov`"
 do
# echo "$line"
 rm "$line"
 done
 set-share.bash
...
nov/stuff/folder - two/link to file.lnk
nov/Projects/name - of - directory
nov/Projects/RE  access.msg
nov/Security: A file or path name is too long.
 find nov | wc -l
     235
# to find the longest fine-name
 find nov | cut -b 50- | more
nov/Projects/very long project name/very long project name - very long project requirements description_11-21-05.xls
 longname="nov/Projects/very long project name/very long project name - very long project requirements description_11-21-05.xls"
 rm "$longname"   #works
can someone explain why this is failing or have a better idea,
thanks,

Last edited by schneidz; 11-21-2005 at 09:52 AM.
 
Old 11-21-2005, 10:22 AM   #2
Mad Scientist
Member
 
Registered: May 2003
Posts: 167

Rep: Reputation: 30
This might be a show of my ignorance, but does

Code:
rm -r
not work?
 
Old 11-21-2005, 10:26 AM   #3
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 1,951

Original Poster
Rep: Reputation: 158Reputation: 158
no, that will blast away files as well as the directory names they are under. i want to preserve directories.

thanks amways,
 
Old 11-21-2005, 10:33 AM   #4
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 1,951

Original Poster
Rep: Reputation: 158Reputation: 158
because of the quotes around "`find nov`" it considers all 235 lines one long file-name. but without the quotes file-names with spaces will get truncated. is there another way to 'wrap' file-names that contain spaces?
 
Old 11-21-2005, 11:10 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Just this does the trick, without problems with filenames containing spaces:
Code:
#!/bin/bash

find nov -type f -exec rm -v '{}' \;
(it's the option "type -f" that tells "find" to only output regular files, i.e. not directories)
 
Old 11-21-2005, 11:32 AM   #6
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 1,951

Original Poster
Rep: Reputation: 158Reputation: 158
that did it hko,

my 'rm' doesns't have '-v' - i dunno what that is. (works without it)

later,
 
Old 11-21-2005, 01:37 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
'-v' just makes 'rm' output what it's doing (removing files)
 
Old 11-22-2005, 02:37 PM   #8
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
If you used zsh, you could simply do rm -f **/*(.).
 
Old 11-26-2005, 07:24 PM   #9
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 30
Maybe it's too late and you already solved it, but if not, here is my solution:
Code:
find >zzz
while read x
do
  rm `file $x | grep -v "directory" | cut -d":" -f1` 2>/dev/null
done <zzz
You just have to copy this is the directory and it will erase all the files in that directory, including itself!
 
Old 11-27-2005, 01:08 PM   #10
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 32
Quote:
Originally posted by zahadumy
Maybe it's too late and you already solved it, but if not, here is my solution:
Code:
find >zzz
while read x
do
  rm `file $x | grep -v "directory" | cut -d":" -f1` 2>/dev/null
done <zzz
You just have to copy this is the directory and it will erase all the files in that directory, including itself!
You like to complicate things, don't you? Hko's one-liner is all you need. Use the right tools for the job, and use them properly.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy an entire directory structure except certain files? thanhvn Programming 9 01-27-2012 11:41 AM
Directory Structure fedix Linux - Newbie 3 10-14-2005 02:48 PM
"WARNING: Circular directory structure" error when deleting directory pistonbrew Linux - Software 5 02-03-2005 06:05 AM
HELP! installing 9.0 from HD, what files do i transfer in what directory structure? diabolik808 Red Hat 1 01-30-2005 03:47 PM
Program files and directory structure Carel Linux - Newbie 5 04-11-2001 10:57 AM


All times are GMT -5. The time now is 05:15 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration