bash make directories using specific text from filenames

# Basic syntax:
for file in /input/directory/search_pattern; do
	substring=${file:start#:length}
	mkdir "/output/directory/prepended_text_${substring}_appended_text"
done

# Where:
#	- The search_pattern should return the set of files you want to
#	use for making the new directories
#	- The substring function takes the start character number and the 
#	length of the substring to use

# Example usage:
# Say you have a directory with the following files and you want to use
# the middle part of the consistent filenames to make new directories:
filename_ABC_123.txt
filename_DEF_123.txt
filename_GHI_123.txt
some_other_file_in_the_directory.txt

for file in ./*123.txt; do # Process files in current directory that end in 123.txt
	substring=${file:11:3} # Use 
	mkdir "/output/directory/prepended_text_${substring}_appended_text"
done

# The /output/directory/ would now have subdirectories named:
prepended_text_ABC_appended_text
prepended_text_DEF_appended_text
prepended_text_GHI_appended_text

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source