Skip to content

Instantly share code, notes, and snippets.

@jdblischak
Created September 2, 2014 18:36
Show Gist options
  • Save jdblischak/c9b77b4838c9d5b7b3da to your computer and use it in GitHub Desktop.
Save jdblischak/c9b77b4838c9d5b7b3da to your computer and use it in GitHub Desktop.
Quick example of renaming files in nested directory structure
# Renames all files using name of directory.
# e.g. sample_name_1/s_1_sequence.txt.gz -> sample_name_1_s_1_sequence.txt.gz
import glob
import shutil
filepaths = glob.glob("sample_name_*/s_*_sequence.txt.gz")
new_names = [x.replace("/", "_") for x in filepaths]
[shutil.copyfile(x[0], x[1]) for x in zip(filepaths, new_names)]
#!/bin/bash
# Set up a directory with files in the following format:
#
# - s_1_sequence.txt.gz
# - sample_name_1 --
# - s_2_sequence.txt.gz
#
# --
#
# - s_1_sequence.txt.gz
# - sample_name_2 --
# - s_2_sequence.txt.gz
#
mkdir sample_name_{1..9}
for dir in sample_name_*
do
touch $dir/s_{1..3}_sequence.txt.gz
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment