Deleting files with preserving directories

  • Thread starter Thread starter Telmerk
  • Start date Start date
  • Tags Tags
    files
AI Thread Summary
To clean a directory structure in Linux without deleting the directories themselves, the command `find /path/to/interest/ -type f -exec rm "{}" \;` can be used to remove all files. It is advised to first test the command with `echo` to ensure the correct files are targeted. Caution is necessary, especially regarding symbolic links, as deleting files can break these links. An alternative approach is to use `find /tmp -type f -mtime +2 -exec rm -f {} \;` to remove old files, which can be scheduled with cron for regular cleanup. Additionally, users may consider monitoring large files with a script that emails the list periodically. The discussion emphasizes the importance of backing up files before deletion and being aware of potential issues with symlinks.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top