How can I rename files in recursive folders and the move the files all to one directory?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
How can I rename files in recursive folders and the move the files all to one directory?
I have 100 directories which are full of photos. There are also sub directories full of photos. I want to rename all photos with a number (example 1-10000) prefixed onto the current filename. Then I want all photos moved into one directory.
I tried krename and after I figured out its idiosyncrasies I found that it does not dig down into the recursive directories, unless I just haven't used it properly.
I can add directories into krename and it will process the files in it but if there is a sub directory in the directory it will not process the file in it.
Is there a solution here or a better software?
I managed to rename files in a directory with a command line script but when it comes to multiple directories and recursive directories its just too complicated for me at this time.
Leave the -n there until it is displaying the result you want. The ** method requires globstar or the equivalent in your shell. Bash supports that, but not all the others do.
Good point, though I am always skeptical of changes to $IFS. I thought about the overflow problem but the method above seems to work with at least 30,004 files. How can the upper limit be estimated without a lot of fiddling?
#!/bin/bash
shopt -s globstar
topdir=$HOME
outputdir="/tmp"
p="0000"
i=1
cd $topdir
for f in **/*.{jpg,jpeg,raw}; do
q=$p$i
echo mv "$f $outputdir/${q: -5}${f##*/}"
((i++))
done
If you have raw files with sidecar JPEGs, you need to keep the names matching - any script which simply loops and increments a counter doesn't do that.
To avoid collisions I use exiv2 to rename with the capture datetime prefixed to the existing name - unless you shoot with two cameras at the same time, that'll give you unique names without losing sidecar associations. (If you do concurrently use two cameras, you can probably get serial number from EXIF/IPTC to solve that.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.