Monday, September 25, 2017
Change File Names in Bulk
Change File Names in Bulk
The post is from hartware.net soods article on Tuesday, January 20th, 2009
--------------------------------------------------------------------------------
Linux/Unix Shell command to bulk change file extensions in a directory (Linux) Posted by sood
This article will explain how to change the file extension for all files in a directory in Linux using a simple bash shell command.
1. Change from one extension to another
The command below will rename all files with the extension .php4 to .php
- for f in *.php4; do mv $f `basename $f .php4`.php; done;
The command below add the extension .txt to all files in the directory
- for f in *; do mv $f `basename $f `.txt; done;
The command below remove the extension .txt from all files in the directory
- for f in *.txt; do mv $f `basename $f .txt`; done;
The command can be entered in one line after a bash prompt. The first part, for f in *, used a for loop. f is a variable. It can be replaced by any valid variable name, for example replacedfiles. Asterisk is a wildcard, meaning that all files in current folder. f is an index, just like i or j in Cs for loop. Thus, the first part instructs the bash to handle file by file in the current folder. The semicolon declares the end of this part.
The second part used keyword, do, to inform the bash to execute some command. Here the command mv is used. mv, if issued individually in the bash prompt, is to change file name or to move a file to another location. $f signified mv to change some files, called by $f. The dollar sign signifies f is a variable, which has been declared in previous line.