Open main menu

Changes

1,534 bytes added ,  2 April
m
Add reminder at the top
{{Notice|Don't forget the <code>-i</code> option to make your file edit "in place"}}
== Move Nested Files to Flat Date Structure ==
Just like in the [[Organize music files]] article, you might need to organize your Photo files. Let's say you use [[Shotwell]] to organize your files on import, but inadvertently switch the structure of imported files from YYYY-MM-DD to YYYY/MM/DD Big difference. How do you quickly move hundreds of files from the latter to the former structure?
 
The following command will use <code>find</code> to find all '''files'''; ignoring any hidden files or files in hidden directories. Then sed just turns the results into a command that uses back-references to put things in the right place. Note that the command as shown doesn't actually do anything but print out the commands that we use <code>sed</code> to construct -- so you can refine it if it's not correct. By adding an '''e''' at the end like <code>%e'</code> in the final invocation, it instructs sed to execute those as commands.
<source lang="bash">
find /home/greg/Pictures/2017/ -type f | grep --perl-regex -v '/\.' | sed 's%/home/greg/Pictures/2017/\([0-9][0-9]\)/\([0-9][0-9]\).*%mkdir -p "/home/greg/Pictures/2017-\1-\2" \&\& mv "&" "/home/greg/Pictures/2017-\1-\2/"%'
</source>
 
When you're done with that, you'll have some detritus left over in your filesystem. <code>find /home/greg/Pictures/2017/ </code> will show you what's there, and adding the <code> -delete</code> option to find will delete those files.
 
== Resources ==
# Tutorial: http://www.grymoire.com/Unix/Sed.html
[[Category:System Administration]]
[[Category:Bash]]
[[Category:Files]]