Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 652

Answer by Kamil Maciorowski for merge numbered files from different directories into same directory in correct order

$
0
0

it works fine but only if all pictures are png and if there is no whitespace in the folder names.

The latter flaw is a common one, the solution is known: quote right. Get used to double-quoting.

To make the script work well with multiple extensions, enable nullglob, nocaseglob and (most importantly) extglob; then use a pattern like *.@(jpg|png|bmp).

To identify the extension later, use ${parameter##*.}.

The below code also uses -- for cp, in case any $folder expands to a string starting with -.

Your script will become this:

#!/bin/bashshopt -s extglob nullglob nocaseglobcounter=1destination="$1"for folder in "${@:2}"do    for picture in "$folder"/*.@(jpg|jpeg|png|bmp|gif|tiff)    do        cp -- "$picture" "$destination/$counter.${picture##*.}"         let counter=counter+1;    donedone

Note that the order of results from * in a globbing pattern (like your *.png or our *.@(jpg|jpeg|png|bmp|gif|tiff)) depends on LC_COLLATE, but it's always lexicographic (I mean in Bash, e.g. Zsh can be smarter). GUI file managers may care about numbers and sort 1.png before 18.png, but our script will sort 18.png (from a source directory like your FOO) before 1.png. This means in your example the file DESTINATION/1.png will be a copy of FOO/11.png, not of FOO/1.png.

Your original script was similarly "flawed", still you did not see this as a problem, so my script does not attempt to "fix" this.


Viewing all articles
Browse latest Browse all 652

Trending Articles