Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Created October 29, 2020 10:38
Show Gist options
  • Select an option

  • Save luckylittle/1103204fada13db167b2d88bcc55bd4c to your computer and use it in GitHub Desktop.

Select an option

Save luckylittle/1103204fada13db167b2d88bcc55bd4c to your computer and use it in GitHub Desktop.
Safely rename files inside directories with the same name as the parent directory name
#!/bin/bash
# Thu Oct 29 10:34:58 UTC 2020
# Lucian Maly, lmaly@redhat.com
for pathname in $(ls -d1 */);
do
cd "${pathname}" || exit
for file in *
do
mv -iv "${file}" "$(basename "${pathname}").${file##*.}"
done
cd ..
done
# Before:
# Active_Directory_Administration_Cookbook/9781789806984-ACTIVE_DIRECTORY_ADMINISTRATION_COOKBOOK.pdf
# Active_Directory_Administration_Cookbook/9781789806984_Code.zip
# Active_Directory_Administration_Cookbook/9781789806984.epub
# Active_Directory_Administration_Cookbook/9781789806984.mobi
# After:
# Active_Directory_Administration_Cookbook/Active_Directory_Administration_Cookbook.pdf
# Active_Directory_Administration_Cookbook/Active_Directory_Administration_Cookbook.zip
# Active_Directory_Administration_Cookbook/Active_Directory_Administration_Cookbook.epub
# Active_Directory_Administration_Cookbook/Active_Directory_Administration_Cookbook.mobi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment