LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2022, 07:34 PM   #1
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 132

Rep: Reputation: Disabled
Add Incrementing Number to Filename in MacOS BSD


Code:
find . -type d -empty -print0 | while IFS= read -r -d '' dirname; do mv -nv "$dirname" /Volumes/sourceDrive/sourceDir/; done
Above script moves empty folders to a target directory.
But not if target contains folder with same name.
How to append incrementing number to the filename?
In "regular linux," use --backup-numbered option of mv command --
not available in BSD.
Assistance much appreciated !!
 
Old 08-23-2022, 08:39 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,480

Rep: Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091
Code:
#!/bin/bash
i=1
while IFS= read -r -d '' name; do
    # separate path from file name
    ndirname=${name##*/}
    echo "$ndirename"
    incrFN=${ndirename}_$i
    echo "$incrFN"
    ((i+=1))
    mv $name /path/$incrFN
done < <(find /path-of-directories -type d -empty -print0)
Untested. Based on your python empty file script and my example in your other thread.

Last edited by michaelk; 08-23-2022 at 09:01 PM.
 
Old 08-23-2022, 08:52 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,636

Rep: Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024
???

In your previous threads, you were trying to do this in Python and you had gotten past that part.

Last edited by dugan; 08-24-2022 at 11:40 AM.
 
Old 08-23-2022, 09:07 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,480

Rep: Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091Reputation: 5091
If you want an auto-incrementing type suffix then
Code:
#!/bin/bash

while IFS= read -r -d '' name; do
    # separate directory from path
    ndirname=${name##*/}
    echo "$ndirname"
    n=1
    while [ -d "/path/${ndirname}_$n" ]; do ((n++)); done
    echo "mv $name /path/${ndirname}_${n}"
done < <(find ~/ -type d -empty -print0)

Last edited by michaelk; 08-23-2022 at 09:43 PM. Reason: typo
 
Old 08-30-2022, 08:47 PM   #5
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 132

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Code:
#!/bin/bash
i=1
while IFS= read -r -d '' name; do
    # separate path from file name
    ndirname=${name##*/}
    echo "$ndirename"
    incrFN=${ndirename}_$i
    echo "$incrFN"
    ((i+=1))
    mv $name /path/$incrFN
done < <(find /path-of-directories -type d -empty -print0)
Untested. Based on your python empty file script and my example in your other thread.
This worked !!
Thank you !!
 
Old 08-31-2022, 12:20 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 18,996

Rep: Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449
It was already solved in your other thread.
https://www.linuxquestions.org/quest...5/#post6374831

Why do you open 4 or 5 threads with the same topic?

https://www.linuxquestions.org/quest...or-4175715999/
https://www.linuxquestions.org/quest...os-4175715967/
https://www.linuxquestions.org/quest...me-4175715765/
 
1 members found this post helpful.
Old 08-31-2022, 11:03 AM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 5,950
Blog Entries: 23

Rep: Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937
@ericlindellnyc - Indeed, your recent threads have mostly been variations or subtasks all related to the same overall problem. Please post your questions only once, and post related or follow-on questions in the same thread. This will keep the context available for those offering help and also for future visitors who find your thread and solutions when searching for solutions to their own problems.

Please review the Site FAQ for guidance in asking well formed questions and proper forum usage.

In future, similar threads will be merged or closed at moderator discretion.
 
Old 09-13-2022, 05:08 PM   #8
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 132

Original Poster
Rep: Reputation: Disabled
I will look more at those other threads to try to trace what I was doing and when.

For now, let me say that in one case, I was trying to add an incrementing number to a filename or directory name.
In another case, I was trying to determine path length. Once found, I planned to add incrementing number before moving it, but I wasn't specifically looking for that info in that post.
I also think I gave up on bash at some point -- either for finding path length or adding incrementing number -- not sure which right now -- and may have then asked how to do the same thing in python.

But I will review these old threads to try to find a more definite answer to your question.
Thanks for the reminder.

I'll just add that the policy regarding follow-up or related questions has been made clear to me, and I plan on following it.

Last edited by ericlindellnyc; 09-13-2022 at 07:36 PM. Reason: make addition
 
Old 09-13-2022, 07:33 PM   #9
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 132

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
???

In your previous threads, you were trying to do this in Python and you had gotten past that part.
This post is from 2017. I don't believe I made any posts prior to that.
However, I've gotten good guidance on how to ask follow-up or related questions in the same thread,
and that's how I'll be doing it from now on.
Thanks.
 
Old 09-13-2022, 07:39 PM   #10
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 132

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
@ericlindellnyc - Indeed, your recent threads have mostly been variations or subtasks all related to the same overall problem. Please post your questions only once, and post related or follow-on questions in the same thread. This will keep the context available for those offering help and also for future visitors who find your thread and solutions when searching for solutions to their own problems.

Please review the Site FAQ for guidance in asking well formed questions and proper forum usage.

In future, similar threads will be merged or closed at moderator discretion.
Got it.
Thanks for the clarification
 
Old 09-22-2022, 06:32 PM   #11
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 132

Original Poster
Rep: Reputation: Disabled
I eventually succeeded with this in python, as follows. I will try to post to the other thread that deals explicitly with python
Code:
import os 
import shutil
counter = 500
for root, dirs, files in os.walk("/Volumes/TM3gbBu/doxx"):
    for dirName in dirs:/Users/ericlindell/pythonScriptTestPathLength.py
        dirPath = os.path.join(root, dirName)
        # if len(dirPath) == 0 and not os.path.isfile(dirPath)
        if os.path.isdir(dirPath) and os.path.exists(dirPath) and len(dirName) < 200 and len(os.listdir(dirPath)) == 0:
            counter += 1
            incrDN = dirName + '_' + str(counter)
            print('incrDN is ', incrDN)
            print('fileSize is ', len(os.listdir(dirPath)))
            print(' ')
            shutil.move(dirPath, "/Volumes/TM3gbBu/destDirsMTsPy/" + incrDN)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Replacing string by incrementing number mzh Programming 7 06-01-2011 05:43 AM
LXer: Add an Auto-Incrementing Build-Number to Your Build Process LXer Syndicated Linux News 0 07-09-2008 01:00 AM
Number incrementing using regular expressions fudam Linux - General 3 11-28-2006 12:58 AM
Automaticly incrementing Version Number BoldKiller Programming 7 03-26-2005 12:08 PM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration