find in all files with name “xyz” in all subdirectories and add .disabled at the end

find all file*(s) and rename to file_renamed


find . -type f -name 'file*' -print0 | xargs --null -I{} mv {} {}_renamed

especially useful if you want to mass-rename wordpress’s xmlrpc.php to xmlrpc.php.disabled (wordpress reinstalls this file with every update)

# tested, works like a charm
# xmlrpc.php is the original-filename
# .disabled will be added to the end of the original-filename
find /home/admin/web -type f -name 'xmlrpc.php' -print0 | xargs --null -I{} mv {} {}.disabled

every once and a while you would like to save some disk space… by re-encoding your *.jpg and *.mp4 from your mobile phone…

rename “xyz” and add string to beginning of filename

renames all files in the current directory and adds “prefix_” to the start of their filenames.

also works with spaces in filename:

for file in *; do mv -v "${file}" "prefix"_"${file}"; done

only files in the current directory

rename ALL files in the current directory

rename 's/^/THIS_WILL_BE_AT_THE_BEGINNING_OF_ALL_FILES_IN_THE_CURRENT_DIRECTORY/' *

remove specia chars from filenames

kindle pukes on chars like ‘|’ but they appear when some website articles are printed to pdf (yes GNU Linux Debian + Firefox can do this 🙂

vim /scripts/rename_website_article_remove_special_chars_filenames.sh 

#!/bin/bash
echo "=== remove | from filenames of website articles to suit kindle ==="
for file in *; do mv "${file}" "${file/|/}"; done
for file in *; do mv "${file}" "${file/#/}"; done

# remove wordtoberemoved from filename
for file in *; do mv "${file}" "${file/wordtoberemoved - /}"; done

remove text in the middle of a file name with korn shell:

only rename specific files, from video_x264.mp4 to video.mp4

apt-get update;
apt-get install ksh; # install KornShell

# i have a bunch of files
-rw-r--r-- 1 user user 451M Apr 23 04:04 VID_20170115_111358_x264.mp4
-rw-r--r-- 1 user user 8.4M Apr 23 04:05 VID_20170213_172301_x264.mp4
-rw-r--r-- 1 user user  66M Apr 23 04:12 VID_20170220_145626_x264.mp4
...
# i would like to have them renamed to:

VID_20170115_111358.mp4 # <- original filename without the _x264
# the pink number is where it starts to cut 1 (start position)
# the yellow number is how many chars it will cut
# experiment with those numbers
ls | awk -F. '{printf "mv %s %s.%s\n",$0,substr($1,1,length($1)-5),$2 ;}'; # preview
mv VID_20160725_182919_x264.mp4 VID_20160725_182919.mp4; # looks good

# what you will need to modify if you need to remove more or less chars before the dot .
# is the number 5 here

# 5 = would remove = _x264
# 6 = would remove = 4_x264
# 7 = would remove = 14_x264

ls | awk -F. '{printf "mv %s %s.%s\n",$0,substr($1,1,length($1)-5),$2 ;}' | ksh;# do the job

# works pretty damn fast :)
# another example:
# my filenames contained spaces so i added double quotes
ls | awk -F. '{printf "mv \"%s\" \"%s.%s\"\n",$0,substr($1,15,length($1)-10),$2 ;}'

what is ksh?

manpage KornShell: ksh.man.txt

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin