Deleting files with preserving directories

  • Thread starter Thread starter Telmerk
  • Start date Start date
  • Tags Tags
    files
Click For Summary

Discussion Overview

The discussion revolves around the problem of deleting files within a directory structure on a Linux system while preserving the directories themselves. Participants explore potential Unix commands and considerations regarding symbolic links and file management strategies.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using the command find /path/to/interest/ -type f -exec rm "{}" \; to delete files while keeping directories intact, cautioning about the potential dangers of the rm command.
  • Another participant raises concerns about the impact of deleting files on symbolic links, noting that this could break links if they point to deleted files and suggesting a more cautious approach to file cleanup.
  • A further suggestion includes using a command to find and delete files older than a certain number of days in temporary directories, along with an idea to monitor large files and email the list periodically.
  • The original poster expresses intent to back up files to a DVD before deletion and acknowledges the presence of symbolic links that may complicate the process.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to deleting files while preserving directories, with differing views on the implications of symbolic links and file management strategies remaining evident.

Contextual Notes

Participants highlight the importance of understanding the implications of symbolic links when deleting files, as well as the potential risks associated with using the rm command without caution. The discussion includes various strategies for file management without resolving the best method.

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!
 
Technology 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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
6K
  • · Replies 24 ·
Replies
24
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
7
Views
3K