Deleting files with preserving directories

  • Thread starter Thread starter Telmerk
  • Start date Start date
  • Tags Tags
    files
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
Telmerk
Messages
43
Reaction score
0
Dear Forumers,

Simple problem: I have a nice directory structure on my PC (Linux). I would like to clean it, I mean to delete all the files within the directory structure, but I don't want to loose my directories.
Is there any simple solution for it? Some unix commands?
Thank you so much in advance!
 
Physics news on Phys.org
Solution. . ?

After some "googling":

find /path/to/interest/ -type f -exec echo rm "{}" \;

.. and if you find everything allright, then just remove echo from the command line:

find /path/to/interest/ -type f -exec rm "{}" \;

Take care! rm can do a lot of harm!

Source is here: http://forums.macosxhints.com/archive/index.php/t-20717.html

Comments are highly appreciated,

T. the M.
 
One comment:

Do you understand symbolic links?

If you do that and there are links in the /path/of/interest or say in /home/telmerk, then you've just broken the link when it points to one of the deleted files.

That kind of thing okay on say/tmp or /var/tmp, but you might want to make it something
that cleans up old files rather than nukes everything:

Code:
find /tmp -type f -mtime +2 -exec rm -f {} \;
Place that in crontab and run it every day. Another appraoch is to list really large files and email it to yourself once a day, or once a week:

Code:
for path in /tmp /var/tmp /someplace /anotherplace
do
     find $path -type f -size +10000 -exec ls -l {} \;
done | mailx -s "really big files"  somebody@someplace.com
 
Dear Mr. McNamara,

Thank you for the comments! Basically my plan was to save my old files onto a DVD. After this I would like to delete them, but leaving the directory structure untouched. I have some symlinks that may cause problems. . . :rolleyes:

Kindest regards & merry christmas,
T. the M.