{"id":2221,"date":"2014-08-15T00:14:47","date_gmt":"2014-08-14T20:14:47","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=2221"},"modified":"2014-10-09T09:30:47","modified_gmt":"2014-10-09T05:30:47","slug":"shell-scripts-copy-rename-files-resize-crop-images","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=2221","title":{"rendered":"Shell scripts that copy, rename files and resize, crop images"},"content":{"rendered":"<p>I have written a bunch of shell scripts for my Mac, that do a lot of work automatically. <\/p>\n<p>First script renames all files in a folder sequentially and gives the names with numbers starting from 64:<\/p>\n<pre>\r\na=64\r\nfor i in *.jpg; do\r\n    new=$(printf \"%d.jpg\" ${a})\r\n    mv ${i} ${new}\r\n    let a=a+1\r\ndone\r\n<\/pre>\n<p>The second script creates copies of those files and adds &#8220;_mini&#8221; to the file name. It creates future miniatures of images.<\/p>\n<pre>\r\nfor f in *.jpg; do\r\n    cp -v $f ${f%.jpg}_mini.jpg\r\ndone\r\n<\/pre>\n<p>The third script deletes all files in a folder that do not match a pattern with &#8220;_mini&#8221; word: <\/p>\n<pre>\r\nshopt -s extglob\r\nrm !(*_mini.jpg)\r\n<\/pre>\n<p>And the last script crops images to the given size:<\/p>\n<pre>\r\nfor i in *.jpg\r\n\tdo \r\n\t\tconvert $i -gravity Center -crop 287x185+0+0 $(basename $i .jpg).jpg \r\n\tdone\r\n<\/pre>\n<p>It is also possible to resize images in other ways, for example percentage:<\/p>\n<pre>\r\nfor i in *.jpg; do \r\nconvert $i -resize 10% $(basename $i .jpg).jpg; done\r\n<\/pre>\n<p>Here are the <a href=\"http:\/\/www.imagemagick.org\/Usage\/crop\/#crop\" title=\"http:\/\/www.imagemagick.org\/Usage\/crop\/#crop\" target=\"_blank\">full instructions<\/a> for ImageMagick.<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/12355176\/shell-script-to-copy-files-from-one-location-to-another-location\" title=\"http:\/\/stackoverflow.com\/questions\/12355176\/shell-script-to-copy-files-from-one-location-to-another-location\" target=\"_blank\">Here<\/a> is an information on copying files. <\/p>\n<p>And <a href=\"http:\/\/unix.stackexchange.com\/questions\/46670\/deleting-many-files-except-one-type\" title=\"http:\/\/unix.stackexchange.com\/questions\/46670\/deleting-many-files-except-one-type\" target=\"_blank\">here<\/a> is an information on deleting by pattern.<\/p>\n<p>Finally we get something like this:<br \/>\n<a href=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/08\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2014-08-15-\u0432-0.15.56.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/08\/\u0421\u043d\u0438\u043c\u043e\u043a-\u044d\u043a\u0440\u0430\u043d\u0430-2014-08-15-\u0432-0.15.56.png\" alt=\"\u0421\u043d\u0438\u043c\u043e\u043a \u044d\u043a\u0440\u0430\u043d\u0430 2014-08-15 \u0432 0.15.56\" width=\"192\" height=\"182\" class=\"aligncenter size-full wp-image-2223\" \/><\/a><\/p>\n<p>Final script: <\/p>\n<pre>\r\ncd \"\/Users\/Denis\/Desktop\/Cakes\"\r\n\r\n#Renaming\r\na=1\r\nfor i in *.jpg; do\r\n    new=$(printf \"%d.jpg\" ${a})\r\n    mv ${i} ${new}\r\n    let a=a+1\r\ndone\r\n\r\n#Create copies\r\nfor f in *.jpg; do\r\n    cp -v $f ${f%.jpg}_mini.jpg\r\ndone\r\n\r\n#Delete original images\r\nshopt -s extglob\r\nrm !(*_mini.jpg)\r\n\r\n#Resize, create miniatures\r\nfor i in *.jpg\r\ndo\r\n\tconvert $i -resize 287 $i \r\n\tconvert $i -gravity North -crop 287x185+0+0 $i\r\ndone\r\n<\/pre>\n<p>This script rename files with spaces in their file names: <\/p>\n<pre>\r\n#!\/bin\/bash\r\nSAVEIFS=$IFS\r\nIFS=$(echo -en \"\\n\\b\")\r\na=1\r\nfor i in *.mp3; do\r\n\techo \"$i\"\r\n\tprintf \"%d.mp3\\n\" ${a}\r\n    new=$(printf \"%d.mp3\" ${a})\r\n    mv \"$i\" ${new}\r\n    let a=a+1\r\ndone\r\nIFS=$SAVEIFS\r\n<\/pre>\n<p><a href=\"http:\/\/www.cyberciti.biz\/tips\/handling-filenames-with-spaces-in-bash.html\" title=\"http:\/\/www.cyberciti.biz\/tips\/handling-filenames-with-spaces-in-bash.html\" target=\"_blank\">Here<\/a> you can read more about treating spaces in file names in bash scripts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have written a bunch of shell scripts for my Mac, that do a lot of work automatically. First script renames all files in a folder sequentially and gives the names with numbers starting from 64: a=64 for i in *.jpg; do new=$(printf &#8220;%d.jpg&#8221; ${a}) mv ${i} ${new} let a=a+1 done The second script creates [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[90,91,92,89,93],"class_list":["post-2221","post","type-post","status-publish","format-standard","hentry","category-web-development","tag-bash","tag-imagemagick","tag-miniatures","tag-scripting","tag-shell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Shell scripts that copy, rename files and resize, crop images - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=2221\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Shell scripts that copy, rename files and resize, crop images - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"I have written a bunch of shell scripts for my Mac, that do a lot of work automatically. First script renames all files in a folder sequentially and gives the names with numbers starting from 64: a=64 for i in *.jpg; do new=$(printf &quot;%d.jpg&quot; ${a}) mv ${i} ${new} let a=a+1 done The second script creates [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=2221\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2014-08-14T20:14:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-10-09T05:30:47+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2221#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2221\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"Shell scripts that copy, rename files and resize, crop images\",\"datePublished\":\"2014-08-14T20:14:47+00:00\",\"dateModified\":\"2014-10-09T05:30:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2221\"},\"wordCount\":162,\"commentCount\":0,\"keywords\":[\"Bash\",\"ImageMagick\",\"Miniatures\",\"Scripting\",\"Shell\"],\"articleSection\":[\"Web Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2221#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2221\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2221\",\"name\":\"Shell scripts that copy, rename files and resize, crop images - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2014-08-14T20:14:47+00:00\",\"dateModified\":\"2014-10-09T05:30:47+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2221#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2221\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2221#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Shell scripts that copy, rename files and resize, crop images\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Shell scripts that copy, rename files and resize, crop images - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=2221","og_locale":"en_US","og_type":"article","og_title":"Shell scripts that copy, rename files and resize, crop images - Denis Kutlubaev","og_description":"I have written a bunch of shell scripts for my Mac, that do a lot of work automatically. First script renames all files in a folder sequentially and gives the names with numbers starting from 64: a=64 for i in *.jpg; do new=$(printf \"%d.jpg\" ${a}) mv ${i} ${new} let a=a+1 done The second script creates [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=2221","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2014-08-14T20:14:47+00:00","article_modified_time":"2014-10-09T05:30:47+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=2221#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=2221"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"Shell scripts that copy, rename files and resize, crop images","datePublished":"2014-08-14T20:14:47+00:00","dateModified":"2014-10-09T05:30:47+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2221"},"wordCount":162,"commentCount":0,"keywords":["Bash","ImageMagick","Miniatures","Scripting","Shell"],"articleSection":["Web Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=2221#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=2221","url":"https:\/\/blog.alwawee.ru\/?p=2221","name":"Shell scripts that copy, rename files and resize, crop images - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2014-08-14T20:14:47+00:00","dateModified":"2014-10-09T05:30:47+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=2221#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=2221"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=2221#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"Shell scripts that copy, rename files and resize, crop images"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2221"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2221\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}