
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add File Extension to Files from Bash
In an operating system, file extensions usually help identify file types and ensure proper handling by applications. When working with files in a Unix-like operating system including Linux, you may encounter situations where files sometimes lack extensions, making it difficult to identify their types or open them with the appropriate applications.
Fortunately, Bash, the default shell on many Linux distributions, provides powerful tools to simplify the process of adding file extensions. This tutorial will guide you through various options that you can use to add file extensions to files using Bash.
Adding File Extension using the mv Command
The simplest way to add an extension to a single file is using the mv (move) command ?
$ mv filename filename.extension
For example,
$ mv document document.txt
Using a for Loop
Using a for loop, you can add extensions to multiple files in a directory ?
for file in *; do mv "$file" "$file.extension" done
For example, to add .txt extension to all files in a directory, you can use ?
for file in *; do mv "$file" "$file.txt" done
Using the find Command
The find command in Linux offers exec flag to execute any commands on items found in the search. To add .txt extension to all files in subdirectories under current directory which do not have an extension, using find command with exec, you can try ?
$ find . -type f ! -name "*.*" -exec mv {} {}.txt \;
Adding Extensions to Specific Files
If you want to add extensions to files that match a particular pattern, you can modify the for loop earlier with search pattern as ?
for file in prefix*; do mv "$file" "$file.extension" done
Handling Spaces in Filenames
Some files can have spaces in their names and if you want to remove all the spaces in the filename post appending the extension, you need to modify the mv command argument accordingly. So, if your filename is "some file name", it will be renamed to "some_file_name.txt"
for file in *; do mv "$file" "${file// /_}.extension" done
Checking for Existing Extensions
You can add file extensions only to files which are missing them. As such, to avoid adding extensions to files that already have them, use mv command with for loop as given below ?
for file in *; do if [[ $file != *.* ]]; then mv "$file" "$file.extension" fi done
Adding Extensions Based on File Type
One common possible use case where you may need to add the file extensions is to add the respective extension based on its actual file type, assuming the original extensions are somehow lost or not already available.
We can determine the file type using the type command and then we can use the case statement to instruct renaming of file types according to file's content.
for file in *; do type=$(file -b "$file") case "$type" in "JPEG image"*) mv "$file" "$file.jpg" ;; "PNG image"*) mv "$file" "$file.png" ;; "ASCII text"*) mv "$file" "$file.txt" ;; esac done
Best Practices
Always use quotes ? You should wrap filenames in quotes to handle spaces and special characters.
Test first ? When working with multiple files, you can use echo command to preview changes before executing them as actual mv command. Here's an example ?
for file in *; do echo "mv $file ${file}.txt" done
Make backups ? You should create backups before batch renaming multiple files. To create backup of a directory using cp command, try something like ?
$ cp -r directory directory_backup
Conclusion
Adding file extensions in Linux using Bash can be accomplished through various methods, from simple one-file operations to complex batch processing. You can choose the appropriate method based on your specific needs and always remember to back up your files before performing batch operations.
Remember that these commands are powerful and can potentially rename many files at once, so use them carefully and test with a small subset of files first. This guide covers the most common scenarios, but Bash's flexibility allows for many other approaches depending on your specific requirements.