Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Deleting files with preserving directories

  1. Dec 17, 2007 #1
    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!
  2. jcsd
  3. Dec 17, 2007 #2
    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.
  4. Dec 17, 2007 #3

    jim mcnamara

    User Avatar

    Staff: Mentor

    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 (Text):

    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 (Text):

    for path in /tmp /var/tmp /someplace /anotherplace
         find $path -type f -size +10000 -exec ls -l {} \;
    done | mailx -s "really big files"  somebody@someplace.com
  5. Dec 18, 2007 #4
    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. . . :uhh:

    Kindest regards & merry christmas,
    T. the M.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook