Rename All Files In Directory To Replace Whitespaces Link to heading
for file in *.md; do
mv -- "$file" "${file// /-}"
done
This script loops through all files in the current directory with the “.md” extension, and renames each file by replacing all whitespace characters with a hyphen ("-"). The ${file// /-} syntax uses parameter expansion to replace all occurrences of the space character with a hyphen in the file name.
The double dash (–) option before the file name is used to indicate the end of options to the mv command, which ensures that any file names starting with a dash (-) are not interpreted as options.
Rename all files in directory and recursively in subdirectories to replace white spaces Link to heading
#!/bin/bash
#shopt -s globstar
for file in **/*.md; do
if [[ -f "$file" ]]; then
mv -- "$file" "${file// /-}"
fi
done
This script uses the globstar option to enable recursive file globbing, and loops through all files in the current directory and its subdirectories that have the “.md” extension. The -f test is used to ensure that the loop only processes regular files (i.e., not directories or other special files).
The mv command inside the loop is the same as the above, replacing all whitespace characters with hyphens in the file name.
Note that the shopt -s globstar command must be run before the loop to enable recursive globbing with the ** pattern.