Anda tidak perlu nama path panjang jika chdir
masuk ke direktori dan cukup gunakan path relatif ke rmdir
.
Atau, jika Anda memiliki shell POSIX yang diinstal, atau port ini ke setara DOS:
# untested code, didn't bother actually testing since the OP already solved the problem.
while [ -d Folder1 ]; do
mv Folder1/Folder1/Folder1/Folder1 tmp # repeat more times to work in larger batches
rm -r Folder1 # remove the first several levels remaining after moving the main tree out
# then repeat to end up with the remaining big tree under the original name
mv tmp/Folder1/Folder1/.../Folder1 Folder1
rm -r tmp
done
(Menggunakan variabel shell untuk melacak di mana Anda mengganti nama untuk kondisi loop adalah alternatif lain untuk membuka gulungan loop seperti yang saya lakukan di sana.)
Ini menghindari overhead CPU dari solusi KenD, yang memaksa OS untuk melintasi pohon dari atas ke n
tingkat th setiap kali level baru ditambahkan, memeriksa izin dll. Sehingga memiliki sum(1, n) = n * (n-1) / 2 = O(n^2)
kompleksitas waktu. Solusi yang memotong sebagian dari awal rantai seharusnya O(n)
, kecuali Windows perlu melintasi pohon ketika mengganti nama direktori induknya. (Linux / Unix tidak.) Solusi yang chdir
semuanya turun ke bawah pohon dan menggunakan jalur relatif dari sana, menghapus direktori saat chdir
dicadangkan, juga harus O(n)
, dengan asumsi OS tidak perlu memeriksa semua Anda direktori induk setiap panggilan sistem, ketika Anda melakukan hal-hal saat CD di suatu tempat.
find Folder1 -depth -execdir rmdir {} +
akan menjalankan rmdir saat CD ke direktori terdalam. Atau sebenarnya, -delete
opsi find berfungsi pada direktori, dan tersirat -depth
. Jadi find Folder1 -delete
harus melakukan hal yang persis sama, tetapi lebih cepat. Ya, GNU temukan di Linux turun dengan memindai direktori, memindahkan CD ke subdirektori dengan jalur relatif, lalu rmdir
dengan jalur relatif, lalu chdir("..")
. Itu tidak memindai ulang direktori saat naik, sehingga akan mengkonsumsi O(n)
RAM.
Itu benar-benar sebuah pendekatan: strace
menunjukkan itu SEBENARNYA menggunakan unlinkat(AT_FDCWD, "tmp", AT_REMOVEDIR)
, open("..", O_DIRECTORY|...)
dan fchdir(the fd from opening the directory)
, dengan sekelompok fstat
panggilan dicampur dalam, juga. Tetapi efeknya sama jika pohon direktori tidak dimodifikasi saat find sedang berjalan.
sunting: Hanya untuk iseng, saya mencoba ini pada GNU / Linux (Ubuntu 14.10, pada Core2Duo CPU generasi pertama 2.4GHz, pada sistem file XFS pada WD 2.5TB Green Power drive (WD25EZRS)).
time mkdir -p $(perl -e 'print "annoyingfoldername/" x 2000, "\n"')
real 0m1.141s
user 0m0.005s
sys 0m0.052s
find annoyingfoldername/ | wc
2000 2000 38019001 # 2k lines / 2k words / 38M characters of text
ll -R annoyingfoldername
... eventually
ls: cannot access ./annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername/annoyingfoldername: File name too long
total 0
?????????? ? ? ? ? ? annoyingfoldername
time find annoyingfoldername -delete
real 0m0.054s
user 0m0.004s
sys 0m0.049s
# about the same for normal rm -r,
# which also didn't fail due to long path names
(mkdir -p membuat direktori dan komponen path yang hilang).
Ya, benar-benar 0,05 detik untuk operasi 2k rmdir. xfs cukup bagus dalam batching operasi metadata bersama dalam jurnal, karena mereka memperbaiki operasi data meta yang lambat seperti 10 tahun yang lalu.
Pada ext4, buat take 0m0.279s, hapus dengan find masih butuh 0m0.074s.
/MIR
sebagai gantinya:ROBOCOPY /MIR C:\temp\EmptyDirectory C:\Storage\Folder1
juga mungkin layak dijalankanchkdsk
hanya untuk cekikikan.