Skip to content

Instantly share code, notes, and snippets.

@NateWeiler
Created December 27, 2020 12:04
Show Gist options
  • Save NateWeiler/7796502426f180e70a26e2666c4c661d to your computer and use it in GitHub Desktop.
Save NateWeiler/7796502426f180e70a26e2666c4c661d to your computer and use it in GitHub Desktop.
Commands to recursively find files or folders, and rename them.

Commands to recursively find files or folders, and rename them.


Find Files

$ find . -type f -name "*.md" -print > 'Some File.txt'
$ find . -depth -type f -name "*.md" -print > 'Some File.txt'
$ find . -maxdepth 10 -type f -name "*.md" -print > 'Some File.txt'

add to a file by adding a >>

Find empty sized files

find . -type f -empty

Rename Files

$ find . -type f | while read FNAME; do mv "$FNAME" "${FNAME//Old/New}"; done
$ find . -depth -type f | while read FNAME; do mv "$FNAME" "${FNAME//Old/New}"; done 
$ find . -maxdepth 10 -type f | while read FNAME; do mv "$FNAME" "${FNAME//Old/New}"; done

Recursively Find & rename Files by name

$ find . -name '*.md' -exec sh -c 'mv "$0" "${0%.md}.txt"' {} ;
$ find . -depth -name '*.md' -exec sh -c 'mv "$0" "${0%.md}.txt"' {} ;
$ find . -maxdepth 10 -name '*.md' -exec sh -c 'mv "$0" "${0%.md}.txt"' {} ;

Recursively Find & rename Files by -name & -type f

$ find . -type f | while read FNAME; do mv "$FNAME" "${FNAME//.git/DOTgit}"; done
$ find . -depth -type f | while read FNAME; do mv "$FNAME" "${FNAME//.git/DOTgit}"; done
$ find . -maxdepth 10 -type f | while read FNAME; do mv "$FNAME" "${FNAME//.git/DOTgit}"; done

Find Folders(Directories)

$ find . -type d -name ".git" -print > 'Some File.txt'
$ find . -depth -type d -name ".git" -print > 'Some File.txt'
$ find . -maxdepth 10 -type d -name ".git" -print > 'Some File.txt'

add to a file by adding a >>

Find empty directories

find . -type d -empty

Rename Folders(Directories)

$ find . -type d | while read FNAME; do echo "$FNAME" "${FNAME//Old/New}"; done
find . -depth -type d | while read FNAME; do echo "$FNAME" "${FNAME//Old/New}"; done
find . -maxdepth 10 -type d | while read FNAME; do echo "$FNAME" "${FNAME//Old/New}"; done

Recursively Find & rename Folders(Directories) by -name & -type d

$ find . -type d | while read FNAME; do mv "$FNAME" "${FNAME//.git/DOTgit}"; done
$ find . -depth -type d | while read FNAME; do mv "$FNAME" "${FNAME//.git/DOTgit}"; done
$ find . -maxdepth 10 -type d | while read FNAME; do mv "$FNAME" "${FNAME//.git/DOTgit}"; done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment