Skip to content

Instantly share code, notes, and snippets.

@daysm
Last active November 2, 2020 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daysm/7608a27fe4519c7ee4c544b8d5c38a71 to your computer and use it in GitHub Desktop.
Save daysm/7608a27fe4519c7ee4c544b8d5c38a71 to your computer and use it in GitHub Desktop.
Move files to (new) directory based on file name
#!/bin/bash
START=$1
END=$2
for file in *; do dir=$(echo $file | cut -c $START-$END); mkdir -p $dir; mv "$file" "$dir"; done
@daysm
Copy link
Author

daysm commented Oct 15, 2020

cd into the directory with your files and execute

/path/to/move-files-to-new-directory-based-on-file-name.sh <start> <end>

<start> and <end> are the positions of the first and last character of the directory name as it is used in the file name. To use the character sequence from the 27th to the 43rd character in the file name as the new directory name, execute

/path/to/move-files-to-new-directory-based-on-file-name.sh 27 43

@daysm
Copy link
Author

daysm commented Nov 2, 2020

To do this for multiple directories, the command could look like this:
for dir in dir1 dir2 dir3; do cd $dir && ../scripts/move-files-to-new-directory-based-on-file-name.sh 27 43 && cd .. ; done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment