Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Renaming files is a common task that users often need to perform, whether for organizing files, batch processing, or simply correcting file names. On macOS, this can be efficiently done using Terminal commands, which provide powerful and flexible ways to handle file operations. This article will guide you through the process of renaming files using Terminal, highlighting its importance for advanced users and system administrators who seek automation and efficiency.
Examples:
Renaming a Single File:
To rename a single file, you can use the mv
(move) command in the Terminal. The basic syntax is:
mv old_filename.txt new_filename.txt
Example:
mv document.txt renamed_document.txt
This command will rename document.txt
to renamed_document.txt
.
Batch Renaming Files:
For batch renaming, you can use a loop in a shell script. Here’s an example that renames all .txt
files in a directory by adding a prefix "new_":
for file in *.txt; do
mv "$file" "new_$file"
done
This script loops through all .txt
files and renames them by adding "new_" to the beginning of each file name.
Using rename
Command:
The rename
command is not available by default on macOS, but you can install it via Homebrew:
brew install rename
Once installed, you can use it to rename files with more complex patterns. For example, to replace spaces with underscores in all .txt
files:
rename 's/ /_/g' *.txt
This command will replace all spaces in the filenames with underscores.
Renaming Files with find
and mv
:
To rename files in subdirectories, you can combine find
with mv
. For example, to rename all .jpg
files in the current directory and its subdirectories by adding a suffix "_backup":
find . -type f -name "*.jpg" -exec sh -c 'mv "$0" "${0%.jpg}_backup.jpg"' {} \;
This command finds all .jpg
files and renames them by appending "_backup" to their names.