Hot answers tagged

4

You could maybe use shuf (from the GNU coreutils package), which generates permutations rather than individual random samples - something like for f in *; do read i; echo mv -- "$f" "file-$i"; done < <(shuf -i 1-10) or (perhaps better) shuffle the filenames - and then simply rename them sequentially i=1; shuf -z -e -- * | while IFS= read -rd '' f; ...


4

Here is a solution built around the inotifywait utility. (You could use incron too, but you'd still need code similar to this.) Run this at boot time, for example from /etc/rc.local. #!/bin/bash # cd /path/to/samba/folder # Rename received files to this prefix and suffix prefix="some_prefix" suffix="pdf" inotifywait --event close_write --format "%f" ...


3

If the filename is in a variable, the canonical way to remove a preffix is: removepre="foo bar " filename="foo bar XXX doo par.jpg" filename="${filename#"$removepre"}" echo "$filename" The problem with the suffix you present is that there is an extension that you want to preserve, so it becomes a bit longer: removesuf=" doo par" filename="foo bar XXX doo ...


3

If you have the rename implementation with Perl regexes (as on Debian/Ubuntu/…, or prename on Arch Linux), you need $1 instead of \1. Also, no backslashes on capturing parentheses: rename 's/(.*)_(.*)/$2_$1/' *_* If not, you have to implement it yourself. #! /bin/bash for file in *_* ; do left=${file%_*} right=${file##*_} mv "$file" ...


3

Since you are flexible about the final file names, and these are photos from a modern smartphone, I recommend exiftool to automatically organize and rename photos for you: first, make a copy/back up your ~/Pictures/iPhone so you always have an something to fall back on if needed run the exiftool on your command prompt with these options: $ exiftool -P -r ...


2

#!/bin/bash appledir="$HOME/Pictures/DCIM/101APPLE" jpgname="5003.JPG" for dir in "$appledir"/* do if [[ -d "$dir" ]] then newfile="$appledir/${dir##*/}" mv "$dir"/5003.JPG "$newfile.tmp" && rmdir "$dir" && mv "$newfile.tmp" "$newfile" fi done With an initial tree as this: $ ...


2

With prename: Setup: $ mkdir test && cd test $ > "foo bar XXX doo par.jpg"; > "foo bar YY YY doo par.jpg" Action: $ rename -n 's/^foo bar //; s/ doo par(\.[^.]*)$/$1/' * foo bar XXX doo par.jpg renamed as XXX.jpg foo bar YY YY doo par.jpg renamed as YY YY.jpg (Remove the -n to have those moves actually performed)


1

This script may help: for file in *-min.png; do echo mv "$file" "${file%-min.png}.png" done Remove the echo to actually execute the command.


1

That depends on your shell. I use zsh, and this is the way I do it: for i in *-min.*; do mv $i "${i%-*}.${i#*.}"; done I advice checking out the manpage of your shell, and checking out a section similar to "parameter expansion" (as it is called in the zsh man page). Similar solutions are available for bash and other shells as well (you did not specify ...


1

So, running this script inside the directory that has folder1, folder2, folder3, etc etc, it will look inside each folder and rename each of the files files present to <directory_<filename>. I believe this is what you were looking for, let me know if it is not what you were looking for. #!/bin/bash for x in $(ls `pwd`); do if [ -d $x ]; ...


1

this is how I'd do it using bash only cd movie_directory ls -1 | while read line do year=$(echo ${line} | awk '{print $1}') name=$(echo ${line} | cut -d " " -f 3- | cut -d"." -f 1) ext=$(echo ${line} | cut -d " " -f 3- | cut -d"." -f 2) newname=${name}" ("${year}")."${ext} mv "${line}" "${newname}" done assumptions: file names do not contain . ...


1

If you have the Perl based rename (sometimes known as prename) this is indeed possible. If you understand Regular Expresssions it's even straightforward. rename -n 's!^(\d+) - (.*)\.(...)$!$2 ($1).$3!' *.avi What this does is split the source filename into three components. Using your first example these would be 1937 Snow White and the Seven Dwarves ...


1

If you want your file names in order (ie. File-1 -> File-n) in the end, you should do them sequentially. -- count=0; for i in * ; do mv $i file-${count}; ((count++)); done


1

Random might return duplicate value. How about generating numbers in sequence ? count=0 for name in *; do count=`expr $count + 1` ; echo mv $name File-$count;done


1

pre=file ; for old in * ; do new=`xxd -l 16 -ps /dev/urandom` ; while [ -f "$pre-$new" ] ; do new=`xxd -l 16 -ps /dev/urandom` ; done ; mv "$old" "$pre-$new" ; done ; xxd is a binary which usually comes with: vim-common package. xxd -l attribute refers to the random length you want. xxd -ps is to get postscript plain hexdump style names, for example: ...


1

You can use it all in one line: find -name '*.JPG' | awk 'BEGIN{ i=1 }{ printf "mv \"%s\" %04d.jpg\n", $0, i++ }'| bash Use it from /Pictures folder for example. You search for all the JPG files, after that, the awk pipe gets the source and increases it in a 4 digit counter. We send to bash this pipe



Only top voted, non community-wiki answers of a minimum length are eligible