Find Files in Multiple Directories Taken from List in a File

Find files in multiple directories taken from list in a file?

This works for me

 for line in "`cat fromDirs.txt`"; do find "$line" -type f \( -name '*good*' -o -exec grep -F "(NODES_'TASK')" {} \; \) -exec cp {} /tmp/ \;; done

Find files in multiple directories

The syntax of your for-loop is incorrect.

It should be:

for f in Failed Loaded ToLoad
do
find "$f" -name 'file'
done

But you don't need a loop. It can simply be done like this:

find Failed Loaded ToLoad -name 'file'

list all files in multiple directories

You can list file with full path of a given directory using printf:

printf "$PWD%s\n" user.newskims.131017222/*

Python search files in multiple subdirectories for specific string and return file path(s) if present

great to have you here!

What you have done so far is found all the file paths, now the simplest way is to go through each of the files, read them into the memory one by one and see if the name you are looking for is there.

import glob
files = glob.glob('C:\TEMP' + '/**', recursive=True)

target_string = 'John Smit'

# itereate over files
for file in files:
try:
# open file for reading
with open(file, 'r') as f:
# read the contents
contents = f.read()
# check if contents have your target string
if target_string in conents:
print(file)
except:
pass

This will print the file path each time it found a name.

Please also note I have removed the second line from your code, because it is redundant, you initiate the list in line 3 anyway.

Hope it helps!

Listing a single file in multiple directories with python

Try following code:

import os

root = "C:\\test\\"
for path, subdirs, files in os.walk(root):
if files:
print(os.path.join(path, min(files)))

UPDATE

To exclude initial directory:

import os
import itertools

root = "C:\\test\\"
for path, subdirs, files in itertools.islice(os.walk(root), 1, None):
if files:
print(os.path.join(path, min(files)))

used min to get the first (alphabetically) filename.

Copying files from multiple directories into a single destination directory

This little BaSH script will do it both ways:

#!/bin/sh
#

# counter
i=0

# put your new directory here
# can't be similar to dir_*, otherwise bash will
# expand it too
mkdir newdir

for file in `ls dir_*/*`; do
# gets only the name of the file, without directory
fname=`basename $file`
# gets just the file name, without extension
name=${fname%.*}
# gets just the extention
ext=${fname#*.}

# get the directory name
dir=`dirname $file`
# get the directory suffix
suffix=${dir#*_}

# rename the file using counter
fname_counter="${name}_$((i=$i+1)).$ext"

# rename the file using dir suffic
fname_suffix="${name}_$suffix.$ext"

# copy files using both methods, you pick yours
cp $file "newdir/$fname_counter"
cp $file "newdir/$fname_suffix"
done

And the output:

$ ls -R
cp.sh*
dir_asdf/
dir_ljklj/
dir_qwvas/
newdir/
out

./dir_asdf:
file.txt

./dir_ljklj:
file.txt

./dir_qwvas:
file.txt

./newdir:
file_1.txt
file_2.txt
file_3.txt
file_asdf.txt
file_ljklj.txt
file_qwvas.txt

find common files between two directories - exclude file extension

Python version:

EDIT: now suports multiple extensions

#!/usr/bin/python3

import glob, os

def removeext(filename):
index = filename.find(".")
return(filename[:index])

setA = set(map(removeext,os.listdir('A')))
print("Files in directory A: " + str(setA))

setB = set(map(removeext,os.listdir('B')))
print("Files in directory B: " + str(setB))

setDiff = setA.difference(setB)
print("Files only in directory A: " + str(setDiff))

for filename in setDiff:
file_path = "A/" + filename + ".*"
for file in glob.glob(file_path):
print("file=" + file)
os.remove(file)

Does pretty much the same as my bash version above.

  • list files in A
  • list files in B
  • get the list of differences
  • delete the differences from A

Test output, done on Linux Mint, bash 4.4.20

mint:~/SO$ l
drwxr-xr-x 2 Nic3500 Nic3500 4096 May 10 10:36 A/
drwxr-xr-x 2 Nic3500 Nic3500 4096 May 10 10:36 B/

mint:~/SO$ l A
total 0
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:06 file1.fasta.profile
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:06 file2.fasta.profile
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:14 file3.fasta.profile
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:36 file4.fasta.profile
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:06 file.fasta.profile
mint:~/SO$ l B
total 0
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:05 file1.dssp
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:06 file2.dssp
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:06 file3.dssp
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:05 file.dssp

mint:~/SO$ ./so.py
Files in directory A: {'file1', 'file', 'file3', 'file2', 'file4'}
Files in directory B: {'file1', 'file', 'file3', 'file2'}
Files only in directory A: {'file4'}
file=A/file4.fasta.profile

mint:~/SO$ l A
total 0
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:06 file1.fasta.profile
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:06 file2.fasta.profile
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:14 file3.fasta.profile
-rw-r--r-- 1 Nic3500 Nic3500 0 May 10 10:06 file.fasta.profile


Related Topics



Leave a reply



Submit