[SOLVED] How to Write Python Script to Move Empty Folders to Target with Incrementing Name
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)?
#!/bin/bash
while IFS= read -r -d '' dirname; do
echo "$dirname"
# your code here
done < <(find /path-of-directories -type d -empty -print0)
It would be much easier using bash. The number of "." does not necessarily matter since string manipulation will find the "last" match.
Pursuant to the foregoing, I have tested the following code
Code:
while IFS= read -r -d ‘’ dirname; do
echo "$dirname"
mv --backup=numbered $dirname /Volumes/TM3gbBu/testEmptyFolderTrashScript/
done
upon execution of which it just hangs, with a greater-than symbol where the prompt goes in the terminal.
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 !!
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.
A dollar sign is used to read from a variable, and should almost always be quoted. When the variable is a path (file and/or directory), it must always be quoted when reading that variable.
ShellCheck is a tool which can highlight this and other issues (such as you incorrectly changing the single quotes to unicode characters).
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.
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.
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 |
while IFS= read -r -d ‘’ dirname
do
echo mv --backup=numbered "$dirname" /Volumes/TM3gbBu/testEmptyFolderTrashScript/
done
Or with a redirection from a special file that in turn is fed by the find command.
Code:
while IFS= read -r -d ‘’ dirname
do
echo mv --backup=numbered "$dirname" /Volumes/TM3gbBu/testEmptyFolderTrashScript/
done < <( find . -type d -empty -print0 )
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 |
while IFS= read -r -d ‘’ dirname
do
echo mv --backup=numbered "$dirname" /Volumes/TM3gbBu/testEmptyFolderTrashScript/
done
Or with a redirection from a special file that in turn is fed by the find command.
Code:
while IFS= read -r -d ‘’ dirname
do
echo mv --backup=numbered "$dirname" /Volumes/TM3gbBu/testEmptyFolderTrashScript/
done < <( find . -type d -empty -print0 )
Remove the echo to really run the mv
Thank you all so much for your incredibly helpful insights and suggestions.
What finally worked was
Code:
find . -type d -empty -print0 | while IFS= read -r -d '' dirname; do mv -nv "$dirname" /Volumes/TM3gbBu/testEmptyFolderTrashScript/; done
I used spellcheck, which gives actionable error messages -- like replacing smart single quotes with unicode. For this, I copied single quotes from this page to a text editor.
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.
Last edited by ericlindellnyc; 08-20-2022 at 12:00 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.