Deleting nested directories

bananalama

New Member
Joined
Jul 17, 2023
Messages
4
Reaction score
2
Credits
43
Hi everyone,

I'm trying to delete some nested directories which seem to be recursive to infinity (if that's possible).
The directories were created by a recovery tool in windows and written to a veracrypt container.

If I want to delete them with rm -rf there is only the mount.ntfs process at full cpu load but nothing happens.
I found some suggestions like using find to not follow links and use -depth but this results in

sudo lsof | grep find
find . -depth -type d -exec rm -rf {} \;
find 181731 foo 3r DIR 253,0 0 2128914 /media/veracrypt1/recov/andotherfoldernames/SPUNINST/SPUNINST/SPUNINST
find 181731 foo 4 unknown /proc/181731/fd/4 (readlink: File name too long)
find 181731 foo 6 unknown /proc/181731/fd/6 (readlink: File name too long)
find 181731 foo 7 unknown /proc/181731/fd/7 (readlink: File name too long)
find 181731 foo 9 unknown /proc/181731/fd/9 (readlink: File name too long)
find 181731 foo 10 unknown /proc/181731/fd/10 (readlink: File name too long)
find 181731 foo 11 unknown /proc/181731/fd/11 (readlink: File name too long)

With 7zip:
7z a -t7z test.7z recov/ -sdel

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=de_DE.UTF-8,Utf16=on,HugeFiles=on,64 bits,12 CPUs Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz (A0653),ASM,AES-NI)

Scanning the drive:

ERROR:
fillin_CFileInfo - internal error - MAX_PATHNAME_LEN

Also no success with rimraf.

Any suggestions on how to remove that junk?
 


Perhaps you could try a find command that pipes into xargs, something like the form:
find .... | xargs rm
This sort of command finds what find is configured to find, and then xargs will remove each found file individually as find moves along. If the filenames have spaces, best to use the -0 switch with xargs ... see the man page.
 
You can't delete any files under /proc.
 
Hmm, would tar be able to create a single file of those directories, then delete the tar file?
 
Yes thanks! tar is the only option so far that shows me the files and not just the directory names (with error message).

rm-rf is actually deleting files it just takes a long while to get through the directories..... df shows me increasing free space.

You can't delete any files under /proc.
Thats the output from lsof, I don't delete from proc.
 
Yes thanks! tar is the only option so far that shows me the files and not just the directory names (with error message).

rm-rf is actually deleting files it just takes a long while to get through the directories..... df shows me increasing free space.


Thats the output from lsof, I don't delete from proc.
Ah, I was wondering what that output was about. So that was the output from piping the output of lsof to find. Which is pretty pointless as that output is in completely the wrong format for find.
The first few columns of output are completely irrelevant to the find command. The only column of output that is important is the /path/filename of the currently open files.
But there's no point in passing that to find - you already have the path/filename of the open file in the output from lsof... Why would you need to find the file? You already know where it is!

Also, as you've used lsof - that shows you which processes have files open.
And if you're trying to delete files that other processes are using, you could run into serious trouble, if the system will even let you delete files that are currenty in use by other processes.

If you want to recursively delete a directory that has a massive, complex series of sub-directories in it, normally you just need to recursively remove the top-level directory.
So if the directory is somewhere like /path/to/somedir, you'd just run
Bash:
rm -rf /path/to/somedir
and that should remove the directory.

If a directory fails to delete because another process has a file open, which resides somewhere inside that directory, or in one of it's subdirectories - you'll need to close that process before attempting to delete the top-level directory.

So if you know what the top-level directory is that you want to delete - e.g. /path/to/somedir:
First run lsof | grep -i /path/to/somedir/
That will filter the output of lsof and will list information about any processes that have files open that are somewhere in the tree of the top-level-directory that you want to remove.

If that turns up empty - there are no processes using files inside that top-level-directory, so you should be ok to recursively remove that directory using rm.

But if it does list any processes that have files open in that directory - you will need to use the kill command to kill those running processes, that will release the files, so they can be deleted.
After attempting to kill the process, or processes you should run the lsof | grep .... command again, to ensure that the unwanted processes are definitely all dead, before trying to remove the top level directory again.
 
That "lsof | grep find" was just to see what files find has in use (that's what my amateur brain tells me).

Unfortunately the directories are not empty. The first directory contains some files and a directory with the same name containing the same files and the same directory again.

I identified three of these directory trees. One took half an hour to delete the top-level directory to free ~ 400Mb.
 
That "lsof | grep find" was just to see what files find has in use (that's what my amateur brain tells me).

Unfortunately the directories are not empty. The first directory contains some files and a directory with the same name containing the same files and the same directory again.

I identified three of these directory trees. One took half an hour to delete the top-level directory to free ~ 400Mb.
Sorry, my bad. For some reason, I read that as lsof | find. I didn’t see the grep in there somehow?! Was the post edited at any point, to add the grep? Or did my eyesight just fail me completely?! Hmmm…. Odd!

If you have a large directory tree with lots of sub-directories, it sometimes takes a long time to delete them. Especially if there are lots of small files in there too. The more file system objects there are, the longer it will take. So I’m not surprised!
 
I may be over simplifying things but... If you want to remove a directory structure, I've always just used:
rm -R folder
This will remove the "folder" and all sub-folders.
To test this, I created a temp folder in my current folder.

mkdir temp
mkdir temp/tmp1
touch temp/tmp1/file1
mkdir temp/tmp1/tmp2
touch temp/tmp1/tmp2/file2
mkdir temp/tmp1/tmp2/tmp3
touch temp/tmp1/tmp2/tmp3/file3
You can list the files in many ways, the below find command shows it in minimal form

find temp/* -type f

rm -r temp/*
find temp/*

Just my 2¢
 


Latest posts

Top