Deleting files with preserving directories

  • Thread starter Telmerk
  • Start date
  • Tags
    files
In summary, the conversation discussed a simple problem of deleting files within a directory structure on a Linux PC without removing the directories. A solution was suggested using the "find" command and caution was given about potential harm. The idea of using symbolic links and a safer approach of cleaning up old files was also mentioned. The conversation ended with gratitude and holiday wishes.
  • #1
Telmerk
44
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
  • #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.
 
  • #3
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
 
  • #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. . . :rolleyes:

Kindest regards & merry christmas,
T. the M.
 

Related to Deleting files with preserving directories

1. How do I delete a file in a specific directory without deleting the entire directory?

To delete a file while preserving its directory, you can use the command "rm" followed by the file path. This will only delete the file and leave the directory intact. For example, "rm /home/user/documents/file.txt" will only delete the file named "file.txt" in the "documents" directory.

2. Can I delete multiple files in different directories at once?

Yes, you can delete multiple files in different directories by using the "rm" command followed by the file paths of each file you want to delete. For example, "rm /home/user/documents/file1.txt /home/user/downloads/file2.jpg" will delete both files "file1.txt" in the "documents" directory and "file2.jpg" in the "downloads" directory.

3. How can I delete all files in a directory but keep the directory itself?

To delete all files in a directory while preserving the directory, you can use the command "rm" followed by the directory path and the wildcard symbol "*". This will delete all files in the specified directory. For example, "rm /home/user/downloads/*" will delete all files in the "downloads" directory but leave the directory itself.

4. Is it possible to delete a directory and all of its contents at once?

Yes, you can delete a directory and all of its contents at once by using the "rm" command followed by the directory path and the "-r" flag. This will recursively delete all files and subdirectories within the specified directory. For example, "rm -r /home/user/documents" will delete the entire "documents" directory and all of its contents.

5. What happens if I accidentally delete a file or directory?

If you accidentally delete a file or directory, it will be permanently removed from your system. However, if you have a backup of the file or directory, you can restore it from the backup. It's always a good idea to have a backup of important files and directories to avoid losing important data.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
Replies
19
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Computing and Technology
Replies
7
Views
624
  • Computing and Technology
Replies
3
Views
1K
  • Computing and Technology
Replies
24
Views
7K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top