1

How do I move only certain files (not all files), from specific subdirectories (not all subdirectories), into a new directory?

The files that need to be moved have been listed in a CSV file and are about 85,000 in number. They have been mentioned with their absolute paths. All the files possess the same extension, i.e., .java. The number of specific subdirectories is about 13,000.

Is there a Python script (preferred) or a Shell script to do this?

N.B: The forums that I searched on returned solutions on how to move all files from within a single subdirectory into a new directory. They are mentioned below:

  1. https://www.daniweb.com/programming/software-development/threads/473187/copymove-all-sub-directories-from-a-folder-to-another-folder-using-python

  2. http://pythoncentral.io/how-to-copy-a-file-in-python-with-shutil/

  3. Filter directory when using shutil.copytree?

  4. https://unix.stackexchange.com/questions/207375/copy-certain-files-from-specified-subdirectories-into-a-separate-subdirectory

Community
  • 1
  • 1
sandra16
  • 63
  • 8
  • 1) Create a list of these certain directories 2) Loop over them 3) Move these certain files (or just *.java) to your destination. – OneCricketeer Jan 29 '16 at 17:21
  • 1
    Could you provide an example of what this CSV file looks like? Does it contain the absolute path of the file/directories you are moving and the absolute path to where they need to be moved? – Noctis Skytower Jan 29 '16 at 17:39
  • The CSV file contains only 1 column (without a title).The 1st row: /home/shaw/Documents//Dataset/ANT/Repository/ChangedMethodFiles/bab9e2d799ee887bc0a404b4acdec0366234eafe/930587b01d54c1800f7cde3695b86d06.java The remaining rows are similar to this one, with only the last 2 entities changing (ie the actual java file, and the folder containing it).It contains the absolute path of the file/directories I'm moving but not the absolute path to where they need to be moved. – sandra16 Jan 30 '16 at 01:10
  • Ignore the double-slashes after 'Documents', it's actually a single slash. – sandra16 Jan 30 '16 at 01:11

2 Answers2

0

Something like this might work for you:

import os
import csv

def move_file(old_file_path, new_directory):
    if not os.path.isdir(new_directory):
        os.mkdir(new_directory)
    base_name = os.path.basename(old_file_path)
    new_file_path = os.path.join(new_directory, base_name)
    os.rename(old_file_path, new_file_path)

def parse_csv_file(csv_path):
    csv_file = open(csv_path)
    csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"')
    paths = list(csv_reader)
    csv_file.close()
    return paths

if __name__ == '__main__':
    old_file_paths = parse_csv_file('your_csv_path')
    for old_file_path in old_file_paths:
        move_file(old_file_path, 'your_new_directory')

This assumes that your CSV file only contains paths, delimited by commas, and all of those files exist.

Jared Goguen
  • 8,772
  • 2
  • 18
  • 36
  • When I tried running this, the comparison between 'files mentioned in the CSV file' and 'all files mentioned in the old path' did not occur. i.e( The entire old folder was moved into the new directory; not just specific files from the old folder). Am I missing something? – sandra16 Jan 30 '16 at 10:04
  • Try just running the first line and seeing whether `len(old_file_paths)` produces a reasonable value. – Jared Goguen Jan 30 '16 at 11:24
0

Assuming that your CSV file looks like this:

George,/home/geo/mtvernon.java,Betsy
Abe,/home/honest/gettys.java,Mary

You could move the files using this shell command:

$ cut -d, -f2 < x.csv | xargs -I '{}'  mv '{}' /home/me/new-directory
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • The 1st 3 lines of my csv file (/home/shaw/Desktop/SHAW_AntUnderstand.csv) look like this: /home/shaw/Documents/Dataset/ANT/Repository/ChangedMethodFiles/bab9e2d799ee887bc0a404b4acdec036/7b01d54c1800f7cde36956d06.java – sandra16 Jan 30 '16 at 09:51
  • /home/shaw/Documents/Dataset/ANT/Repository/ChangedMethodFiles/20ab59bff57796a67e1cdfefbc29826e/f132a91640114b9d51804dd7.java – sandra16 Jan 30 '16 at 09:53
  • /home/shaw/Documents/Dataset/ANT/Repository/ChangedMethodFiles/8ce1de2178a0422105fa437c327bf3/b2933bb3d32ef7d12d5f40a.java – sandra16 Jan 30 '16 at 09:53
  • I ran the following: shaw@shaw-XPS-8700:~/Desktop$ cut -d, -f1 < /home/shaw/Desktop/SHAW_AntUnderstand.csv | xargs -I '{}' mv '{}' /home/shaw/Desktop/ANT_85K – sandra16 Jan 30 '16 at 09:56
  • (My csv file has only 1 column, hence I used -f1) – sandra16 Jan 30 '16 at 09:57
  • I got a series of errors,all of the type: mv: cannot stat ‘/home/shaw/Documents/Dataset/ANT/Repository/ChangedMethodFiles/07382b400af7ecebc64eff5c55b4f56/de0fa0428b3da0c13c6b2a3.java’: No such file or directory – sandra16 Jan 30 '16 at 09:59
  • However, when I physically check the paths; these sub-folders and files are definitely present. Where am I going wrong? – sandra16 Jan 30 '16 at 10:00
  • I don't know what is causing that. – Robᵩ Jan 30 '16 at 14:28
  • Thanks Rob...your answer is neat and concise. It worked well. The reason why it didn't work the previous time was that I had specified the wrong address for some files. – sandra16 Feb 03 '16 at 13:01