New answers tagged

0

Try this is a separate folder -- for i in SLX*; do val=$(echo $i | grep -oE "50."); val=$((val-504+1)); new=$(printf "400-%03d" $val); mv $i $new; done


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: ...


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; ...


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 ...


3

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" ...


0

I think cron is a good idea! Here some input for your script: #!/bin/bash smbdir="@sambadir@" # change the @sambadir@ variable by hand smbsubdirs=(A B C D) smbprefix="@sambaprefix@" # for example for sd in ${smbsubdirs[@]}; do ssd=$smbdir/$sd && [ -d "$ssd" ] || continue for f in $(find $ssd -cnewer $ssd -type f); do if [[ "$(basename $f)" ...


-1

With the util-linux rename, no, this one only does basic string replacement. With the Perl-based rename, yes, see choroba's answer. With zsh's zmv: autoload -U zmv # put this in your ~/.zshrc zmv '(*)_(*).(*)' '${2}_$1.$3' zmv -w '*_*.*' '${2}_$1.$3' The two zmv invocations above are equivalent. To act in subdirectories as well, you can use zmv -w ...


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" ...


0

I'm using this shell script in the root of a directory packed with files to move them all to a year/month-like structure: #!/usr/bin/env bash if [ ! $1 ]; then echo "Usage: ./pictures.sh jpg" exit 1 fi for f in *."$1"; do FILENAME="$f" YEAR=`date -j -f "%s" $(stat -f "%m" "$FILENAME") +"%Y"` MONTH=`date -j -f "%s" $(stat -f "%m" ...


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


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: $ ...


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 ...


0

For example having this cat list-of-files path1/file1 path2/file1 we can do that. Better reset a variable every time: a=0; cat list-of-files | while read -r f; do echo mv -v "$f" $((++a)).jpg done mv -v path1/file1 1.jpg mv -v path2/file1 2.jpg Echo for checking. Delete it for actual removing.


1

I tend to use sed to do the replacement and test before making changes: for i in */pre.nii; do j=$(echo $i | sed 's/\//_/') echo $j done then run it for i in */pre.nii; do j=$(echo $i | sed 's/\//_/') mv -v $i $j done


4

To replace one / (escaped with \) by _: for i in */pre.nii; do echo mv "$i" "${i/\//_}"; done If everything looks fine, remove echo.


3

Without rename: for file in *.mp3 do no_extension=${file%.mp3} the_date=$(echo "${no_extension}" | cut -d ' ' -f 1) year=20${the_date:6} month=${the_date:0:2} day=${the_date:3:2} date_part=${year}-${month}-${day} dj_part=$(echo "${no_extension}" | cut -d ' ' -f 2-) new_file="${dj_part} ${date_part}.mp3" mv "${file}" "${new_file}" done ...


3

cd to the directory then run the following (using perl-rename). This is a "dry-run" first. rename -n 's/^([0-9]{2})\.([0-9]{2})\.([0-9]{2}) (.*)\.mp3$/$4 20$3-$1-$2.mp3/' * 02.04.11 DJ Kilbot.mp3 -> DJ Kilbot 2011-02-04.mp3 If you are happy with the output, then run it for real. rename 's/^([0-9]{2})\.([0-9]{2})\.([0-9]{2}) (.*)\.mp3$/$4 ...


0

Assume that the files are named strictly that way. for file in "*.mp3";do date="${file:0:8}" #get the date in the filename. dj="${file%.mp3}" #strip the extension off the filename. dj="${dj:8}" # get dj name. mv -nv $file "$dj $date.mp3" # -n don't overwrite files. done


1

You can use sed to remove the duplicates from string: for f in *; do r=$(echo $f | sed -r "s/(.*)(\{Sci-Fi\}|\{Science Fiction\})(.*)(\{Sci-Fi\}|\{Science Fiction\})(.*)/\1\2\3\5/g"); echo $r; done Replace echo $f with mv "$f" "$r" if you like the output. The above sed line will take the first matching word and remove the second, if you want to ...


3

At the shell, if you've got the Perl rename installed (sometimes called prename): rename -v 's/$/.bad/' * If you have too many files for the shell * glob to handle them all you can mix'n'match with find like this (also replace + with \; if necessary): find . -maxdepth 1 -exec rename -v 's/$/.bad/' {} + For Perl, just use move from a standard module: ...


2

Assuming no spaces in the file names: for file in *; do mv "${file}" "${file}.bad"; done


1

If you have the Perl rename (sometimes called prename) you can do this: rename -v 's!(\d+)(.*)\.jpg$!sprintf "%d%s.png", $1+36, $2!e' *.jpg This takes each filename, splits off the leading digits, the rest of the filename component, and the trailing .jpg. It then adds 36 to the numeric part, adds on the remainder of the original filename, and replaces the ...



Top 50 recent answers are included