Sed: Difference between revisions

No edit summary
m Text replacement - "<(\/?)source" to "<$1syntaxhighlight"
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{Notice|Don't forget the <code>-i</code> option to make your file edit "in place"}}
== Move Nested Files to Flat Date Structure ==
== 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?
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.
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">
<syntaxhighlight 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/"%'
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>
</syntaxhighlight>


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.
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.