5W site logo 5W - мой домашний сайт-блог с картинками, кино и плюшками.

batch-rename.txt

#!/bin/sh

########################################################
#changed all "IMG_" to "Fireworks"
#vyvod budet viden v konsoli
#posle togo, kak ubedites, schto vse norm
#save vyhlop to file, run chmod a+x file, and run file
#gotovo
for filename in IMG_*;
do echo mv \"$filename\" \"${filename//IMG_/Fireworks}\";
done

################################################W
#remove all "image" from files names
for f in *.png; do mv "$f" "${f#image}"; done

###################################
#remove all "0-9" from names
for f in *; do mv "$f" "${f#[0-9]*-}"; done

################################################
# mv + awk + sed
ls -1 *foo* | awk '{print("mv "$1 " " $1)}' | sed 's/foo/bar/2' > rename.txt
# The command works as follows:
# ls -1 *foo* lists all the files in the current directory with
#foo in the file name. It lists one filename per line.
# The ouptut is piped to awk '{print("mv "$1 " " $1)}' command.
#This produces new output where each line is mv FILENAME FILENAME,
#with FILENAME being the corresponding filename.
# The output from the awk command is piped to sed 's/foo/bar/2',
#which replaces the second instance of foo in a line with bar. The
#second instance of foo corresponds to the second FILENAME in the mv
#FILENAME FILENAME lines generated by the awk command. This creates
#output of the form mv FILENAME NEWFILENAME, where the new filename
#is desired filename with foo replaced with bar.
# Finally, the entire output is saved to rename.txt for user
#review to ensure that the rename commands are being generated
#correctly. As with the main batch rename technique, you could pipe
#the output to /bin/bash but this is not recommended.

###################################################
#paste + sed
paste <(ls -1 *foo* | sed 's/^/mv "/;s/$/"/') <(ls -1 *foo* | sed 's/^/"/;s/$/"/;s/foo/bar/g') -d ' ' > rename.txt

# How it works:
# The commands in <( … ) are processed first. This is a process
#substitution in Bash and the output of the command inside is given
#to paste.
# The first process substitution lists all files named *foo* and
#sed prepends mv ” and appends a double quote to the end. This
#surrounds the file name with double quotes to take care of spaces.
# The second process substitution again lists all the files named
#*foo* and then uses sed to surround the file name with double
#quotes and replace “foo” with “bar” in the file name.
# The paste command concatenates the output of the two process
#substitutions line by line, and uses a space as a delimiter. This
#produces a list of mv commands that can be saved to a file or piped
#to /bin/bash for execution.