diff --git a/README.md b/README.md index 8c701e6..0a44635 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1703 TILs and counting..._ +_1704 TILs and counting..._ See some of the other learning resources I work on: @@ -1716,6 +1716,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Produce A Lowercase V4 UUID](unix/produce-a-lowercase-v4-uuid.md) - [Provide A Fallback Value For Unset Parameter](unix/provide-a-fallback-value-for-unset-parameter.md) - [Remove A Directory Called `-p`](unix/remove-a-directory-called-dash-p.md) +- [Rename A Bunch Of Files By Constructing mv Commands](unix/rename-a-bunch-of-files-by-constructing-mv-commands.md) - [Repeat Yourself](unix/repeat-yourself.md) - [Replace Pattern Across Many Files In A Project](unix/replace-pattern-across-many-files-in-a-project.md) - [Run A Command Repeatedly Several Times](unix/run-a-command-repeatedly-several-times.md) diff --git a/unix/rename-a-bunch-of-files-by-constructing-mv-commands.md b/unix/rename-a-bunch-of-files-by-constructing-mv-commands.md new file mode 100644 index 0000000..a9df631 --- /dev/null +++ b/unix/rename-a-bunch-of-files-by-constructing-mv-commands.md @@ -0,0 +1,51 @@ +# Rename A Bunch Of Files By Constructing mv Commands + +I downloaded a bunch of bank statements as PDFs. On the upside they all were +consistently named. On the downside they used an unhelpful date format. With a +date format that puts year before month before day, the files easily sort +alphanumerically. However, these filenames used a date format that put month +before day before year. + +Here is a subset of the files + +```bash +$ ls *.pdf +'012524 Statement.pdf' +'012725 Statement.pdf' +'022624 Statement.pdf' +'022625 Statement.pdf' +'032524 Statement.pdf' +'032525 Statement.pdf' +``` + +Notice they are named with `MMDDYY Statement.pdf`. I would instead like for them +to be named as `YYYY-MM-DD-statement.pdf`. + +I can generate a series of `mv` statements that then get piped to `sh` which +_evaluates_ them. But first, let's do a dry run of a `sed` statement that +rearranges the date parts. + +```bash +$ ls *.pdf | sed 's/\(..\)\(..\)\(..\) Statement\.pdf/mv "&" "20\3-\1-\2-statement.pdf"/' +mv "012524 Statement.pdf" "2024-01-25-statement.pdf" +mv "012725 Statement.pdf" "2025-01-27-statement.pdf" +mv "022624 Statement.pdf" "2024-02-26-statement.pdf" +mv "022625 Statement.pdf" "2025-02-26-statement.pdf" +mv "032524 Statement.pdf" "2024-03-25-statement.pdf" +mv "032525 Statement.pdf" "2025-03-25-statement.pdf" +``` + +The way this works is that all the `pdf` files in the current directly get +listed out. That gets piped to a `sed` statement that matches on capture groups +against the first three pairs of characters (the date parts) in the filenames. +It matches on the rest of the filename (` Statement.pdf`). This is then replaced +by a `mv `, the full match of the original filename (`&`), and then the new +filename made up of the rearranged date parts. + +I can then pipe it to `sh` to run those `mv` commands. + +```bash +$ ls *.pdf | + sed 's/\(..\)\(..\)\(..\) Statement\.pdf/mv "&" "20\3-\1-\2-statement.pdf"/' | + sh +```