How to Write Python Script to Move Empty Folders to Target with Incrementing Name
I have an enormous number of empty folders interspersed through my 6TB HDD.
I would like to keep the folders as a precaution against accidentally deleting something I need. So I've tried both python and bash. Haven't gotten too far with either. I have this python script to remove empty directories Code:
for p in Path(target_path).glob('**/*'): Code:
def uniquify(path, sep = ''): In bash I've gotten this far Code:
find . -type d -empty Also, the total number of "dots" in a folder name may be 0, 1, 2 or more. So the code that assumes an extension exists doesn't always apply. Any assistance much appreciated !! BTW -- if I'm on the wrong forum for python, I'll try to transfer this to another forum. |
Code:
#!/bin/bash |
Why Python?
mv has a "--backup=numbered" option. But if you're only dealing with empty directories, do you even need that? Surely all you need is a text file record of the dirs that existed before you deleted them, then - if there are any issues - you mkdir -p the relevant path(s)? |
something like this (?):
Code:
find <where> -type d -empty -print -delete |
If it's important to keep a record of owner, group and timestamp metadata for the dir then using cpio might be the best bet.
Something like this: Code:
find /dir -type d -empty -print0 >/tmp/empty.list0 \ |
find also has a -ls option (instead of -print) which will show the permission/ownership settings. But cannot be used directly to restore those dirs.
|
Thank you all so much for your helpful suggestions. I will be trying them out in the near term and hope to have results to post here.
|
Quote:
Code:
while IFS= read -r -d ‘’ dirname; do I'm not versed in the subtleties of IFS, and the echo in the first line of the while-loop doesn't execute to show me how far it's gotten. I'm using dirname three times . . once with a dollar sign, once with quotes and a dollar sign, and once with neither. I'm not sure if this is consistent usage. I've implemented what appears to be two single-quotes in the while-loop condition as a pair of single directional quotes, with nothing between them. I also copied and pasted everything into a text editor and then back into my editor, to lose any extraneous characters. I'm using the mv --backup=numbered option to avoid name collisions. I'm running the script from the same directory where the empty directories are -- and then moving them to a directory at the top level on the same drive. Not sure why it's hanging. Help much appreciated !! |
Quote:
You missed the redirection, which was the input for that while cycle: Code:
done < <(find /path-of-directories -type d -empty -print0) |
Quote:
ShellCheck is a tool which can highlight this and other issues (such as you incorrectly changing the single quotes to unicode characters). You can consult the Bash manual (e.g. built-in read command) to understand what a command/option does). Also, Wooledge's BashFAQ has plenty of common tasks (and mistakes to avoid), and can also give a clue how a command might look like. |
oh yes, dirname and basename are executables in linux, therefore you need to avoid them in a shell script (as variables). But you can use them in a python script.
|
Quote:
|
Your find must have -print0
(print null-terminated) in order to work with the read -r -d ‘’ that reads a null-terminated line. And the while loops until all lines are read. You feed the find output to the loop, either with a pipe Code:
find . -type d -empty -print0 | Code:
while IFS= read -r -d ‘’ dirname |
Quote:
What finally worked was Code:
find . -type d -empty -print0 | while IFS= read -r -d '' dirname; do mv -nv "$dirname" /Volumes/TM3gbBu/testEmptyFolderTrashScript/; done Spellcheck said "do" had no matching done, so I put ; before done. Strangely, this script moves some 0-byte FILES -- despite -type d option. This happens only for files with RTFD extension. I tried other EXTs, including 4 letter ones. I can live with this, though ideally I'd like to remove 0-byte files as a separate process -- and modify the script to work variously with FN's starting with tilde or other cruft. |
All in one line??
Yes then you must put a semicolon before the done. You mean shellcheck not spellcheck. What is a 0-byte file? Is it named *.rtfd? Then you can augment your find command with ! -name "*.rtfd" or -not -name "*.rtfd" |
All times are GMT -5. The time now is 08:39 PM. |