Open In App

Rename Files and Remove Shell PID as extension Using Bash

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A shell script is a program written in the shell programming language that can be executed in a terminal. One use for a shell script is to search for files and rename them in a specific way. In this case, the script will search for files that contain the shell process ID (PID) and rename them so that the PID is no longer part of the file name.

The script will begin by defining a variable to store the current shell PID, which can be obtained using the ‘$$’ command. Next, the script will use the ‘find’ command to search for files in a specified directory that contain the PID in their names. The ‘find’ command will be used with the ‘-name’ option to search for files with a specific pattern. The script will then use the ‘-exec’ option to execute a command on the files that are found. In this article, we will be discussing a shell script that can be used to search for files in a specified directory and rename them such that they do not contain the shell process ID (PID). The script uses the command line tool “find” to search for files, and “sed” to perform the renaming operation.

Here are the instructions to use the script:

Step 1: Download the script and save it in a directory of your choice.

Step 2: Make the script executable by running the command:

chmod +x script_name.sh

Step 3: Run the script by executing the command, where “directory_path” is the path to the directory in which you want to search for files.

./script_name.sh directory_path

Script:

#!/bin/bash
dir=$1
pid=$$

#Checking if directory exists or not
if [ -d "$dir" ];
then
  echo "Started processing files in $dir"
   for file in $(find $dir -type f)
   do
    if [[ $file == *"$pid"* ]]
    then
        new_file=$(echo $file | sed "s/$pid//g")
        mv "$file" "$new_file"
        echo "File $file renamed to $new_file"
    fi
   done
   echo "Finished processing files in $dir"
else
  echo "Error: Directory $dir does not exist"
fi

Here is an example of input and output:

Input:

./script_name.sh /home/user/documents
Input

 

Output:

Output

 

Explanation:

In the above image, you can see the execution of the script, the PID is 2793 and in the documents folder there is no file that contains this integer so it does nothing but if there is a large number of files and if there exists a file which contains PID then it will rename it. each time the script is run it has a new PID so it’s difficult to show the renaming process by creating files.

In this example, the script is executed with the directory path “/home/user/documents” and the current shell PID is 2793. The script searches for all files in the directory and checks if the file name contains the PID. If it does, it performs the renaming operation by removing the PID from the file name using sed.

Conclusion

In conclusion, this script can be useful for automating the process of removing the shell PID from file names, especially in cases where multiple files need to be renamed. Keep in mind that this script will rename all files in the specified directory, regardless of file type, so it’s important to use it with caution. Also, make sure to take a backup of your files before running the script.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads