Sed: Difference between revisions

adds tutorial and second cheatsheet
m Text replacement - "<(\/?)source" to "<$1syntaxhighlight"
 
(5 intermediate revisions by 2 users 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 ==
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.
<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/"%'
</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.
== Resources ==
== Resources ==
# Tutorial: http://www.grymoire.com/Unix/Sed.html
# Tutorial: http://www.grymoire.com/Unix/Sed.html
# Another Cheatsheet: http://www.grymoire.com/Unix/SedChart.pdf
# Another Cheatsheet: http://www.grymoire.com/Unix/SedChart.pdf
# Eric Pement's pages on sed: http://www.student.northpark.edu/pemente/sed/index.htm
== One-liners ==
== One-liners ==
<pre>
<pre>
Line 731: Line 743:
[[Category:System Administration]]
[[Category:System Administration]]
[[Category:Bash]]
[[Category:Bash]]
[[Category:Files]]