LinuxQuestions.org
Review your favorite Linux distribution.
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 11-21-2005, 09:50 AM   #1
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,958

Rep: Reputation: 606Reputation: 606Reputation: 606Reputation: 606Reputation: 606Reputation: 606
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-15/ fc-20-live-usb/ aix
Posts: 3,958

Original Poster
Rep: Reputation: 606Reputation: 606Reputation: 606Reputation: 606Reputation: 606Reputation: 606
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-15/ fc-20-live-usb/ aix
Posts: 3,958

Original Poster
Rep: Reputation: 606Reputation: 606Reputation: 606Reputation: 606Reputation: 606Reputation: 606
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,530

Rep: Reputation: 108Reputation: 108
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)
 
1 members found this post helpful.
Old 11-21-2005, 11:32 AM   #6
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,958

Original Poster
Rep: Reputation: 606Reputation: 606Reputation: 606Reputation: 606Reputation: 606Reputation: 606
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,530

Rep: Reputation: 108Reputation: 108
'-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: 31
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: 33
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.
 
Old 05-30-2013, 11:41 AM   #11
vash_uk
LQ Newbie
 
Registered: May 2013
Posts: 2

Rep: Reputation: Disabled
I hope you haven't been waiting for too long

find `pwd` -name '*.*' | xargs rm
 
Old 05-30-2013, 12:11 PM   #12
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,759

Rep: Reputation: 977Reputation: 977Reputation: 977Reputation: 977Reputation: 977Reputation: 977Reputation: 977Reputation: 977
Quote:
Originally Posted by vash_uk View Post
I hope you haven't been waiting for too long

find `pwd` -name '*.*' | xargs rm
Not only is this thread over 7 years old, the suggestion isn't even a good one. `pwd` is unnecessary, just use ".", and directory names can have .'s in them as well...such as hidden directories, "d.name" naming schemes, etc. And to top it all off, piping the output to xargs will break if any of the matches have a space or other "bad" character in the name.

Hko already provided the correct solution in 2005.

Last edited by suicidaleggroll; 05-30-2013 at 12:16 PM.
 
Old 05-30-2013, 12:20 PM   #13
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,958

Original Poster
Rep: Reputation: 606Reputation: 606Reputation: 606Reputation: 606Reputation: 606Reputation: 606
also certain file names dont have '.'s.

resurerrecting this old thread allowed me to rep to hko (i dont think we had rep in 2005).
 
1 members found this post helpful.
Old 05-30-2013, 01:23 PM   #14
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,172
Blog Entries: 54

Rep: Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809
And since we're necro-posting anyway 'tar -cf /dev/null /some/path --remove-files' should preserve the directory structure as well.
 
2 members found this post helpful.
Old 05-30-2013, 04:01 PM   #15
vash_uk
LQ Newbie
 
Registered: May 2013
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Not only is this thread over 7 years old, the suggestion isn't even a good one. `pwd` is unnecessary, just use ".", and directory names can have .'s in them as well...such as hidden directories, "d.name" naming schemes, etc. And to top it all off, piping the output to xargs will break if any of the matches have a space or other "bad" character in the name.

Hko already provided the correct solution in 2005.
Why is pwd unnecessary?
if you try to rm a directory you will get an error message and the directory won't be deleted. Who uses file names with spaces or bad characters?

I posted a reply because there was no "one liner" and this was the first thing that came up when i googled the same question. I wonder why I went out of my way to post a reply.
 
  


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
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 09:21 PM.

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 Google+: linuxquestions
Open Source Consulting | Domain Registration