Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Rename Files on macOS Using Terminal

Renaming files is a common task that can be easily accomplished through the graphical user interface (GUI) on macOS. However, there are scenarios where renaming files via the Terminal is more efficient, especially when dealing with multiple files or when automating tasks. This article will guide you on how to rename files using Terminal commands on macOS, providing practical examples and scripts to streamline the process.

Examples:

  1. Renaming a Single File To rename a single file, you can use the mv (move) command in Terminal. The mv command is used to move files or directories from one place to another, and it can also be used to rename them.

    mv oldfilename.txt newfilename.txt

    In this example, oldfilename.txt is renamed to newfilename.txt.

  2. Renaming Multiple Files with a Similar Pattern If you need to rename multiple files that follow a specific pattern, you can use a loop in the Terminal. For instance, if you want to rename all .txt files by adding a prefix "new_", you can use the following script:

    for file in *.txt; do
       mv "$file" "new_$file"
    done

    This script loops through all .txt files in the current directory and renames each one by adding the prefix "new_".

  3. Using rename Command The rename command is not available by default on macOS, but you can install it using Homebrew. First, install Homebrew if you haven't already:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

    Then, install the rename command:

    brew install rename

    Now you can use the rename command to rename files. For example, to change all .txt file extensions to .md, you can use:

    rename 's/\.txt$/.md/' *.txt

    This command uses a regular expression to substitute .txt with .md for all .txt files in the directory.

  4. Automating File Renaming with a Shell Script You can create a shell script to automate the renaming process. Here's an example script that renames all .jpg files by appending the current date:

    #!/bin/bash
    for file in *.jpg; do
       mv "$file" "$(date +%Y%m%d)_$file"
    done

    Save this script to a file, for example, rename_jpg.sh, and make it executable:

    chmod +x rename_jpg.sh

    Run the script:

    ./rename_jpg.sh

    This script will rename all .jpg files by appending the current date to the beginning of each filename.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.