디렉토리의 전체 파일 검색 [닫기]

디렉토리의 전체 파일 검색 [닫기]

파일에 특정 ID 목록이 있습니다.

file1

10012074
10182922
10193829
10213367
10302542
10332316
10492906
10592572
10606805
10627446
10681600
10697905
10758584
10882944
10919833
10921530
11021848

이 디렉토리에는 30,000개의 파일이 있습니다 dir. 부분 파일 미리보기:

16320386  23505634  31404647  40262433  49727240  59977762  72739048
16321609  23507673  31409545  40263912  49731993  59983079  72743197
16321830  23508959  31410806  40274881  49733838  59991144  72743532
16323719  23513175  31413679  40277233  49737047  60000337  72743644
16324483  23513237  31415413  40280305  49739812  60006875  72746735
16325027  23514404  31421015  40283351  49741053  60017537  72748827
16326681  23516543  31422747  40288023  49752294  60022678  72751898
16327485  23517971  31427324  40290554  49752684  60023962  72752027
16333225  23518024  31427909  40291536  49755190  60025125  72754762
16334779  23520574  31428484  40291746  49756105  60029433  72755014
16336857  23522410  31430611  40293529  49756156  60034076  72757030

이 디렉터리에서 .txt의 내용과 일치하는 동일한 이름의 파일을 제거합니다 file1.

밝히다file1: 30,000개의 파일이 포함된 디렉터리의 모든 관련 컨텍스트를 새 파일로 병합 하고 싶습니다 .다시 말해서file1, 추출된 파일에 나열된 파일을 하나의 파일로 연결하고 싶습니다 dir.

답변1

dir이 30,000개의 파일이 현재 디렉터리의 하위 디렉터리에 있는 경우 :

xargs -I XX cat dir/XX <file1 >result.txt

그러면 에 나열된 파일이 연결 file1되고 결과가 result.txt.

xargs파일 이름은 여기에서 읽혀지고 file1각 파일에 대해 실행됩니다. cat명령 내용을 파일 이름으로 -I XX바꾸도록 지시합니다.xargsXXcat dir/XX

당신은 또한 사용할 수 있습니다

cd dir
xargs cat <../file1 >../result.txt

이는 더 빠를 수 있지만 동일한 결과를 제공합니다. 차이점은 cat각 파일을 개별적으로 실행하는 대신 cat가능한 한 많은 파일 이름으로 호출된다는 것입니다.

답변2

입력 - 파일 1 출력 - 파일 2

ID가 포함된 디렉터리가 file1과 동일한 디렉터리에 있다고 가정합니다.

cat file1 | while read line ; do cat ./*/$line  >> file2.txt 2> /dev/null; done

답변3

#Sorry for my english
#Maybe you need execute this script with "sudo"
#Keep attention in the urls, you must set properly the last '/' . Example: /search_directory/
#In my case I am testing in the same directory where i am executing the scripts, that is the
#reason why DirectoryWhereToSearch is empty and BackupDirectory doesn't contain any '/'

FileListName="filelist.txt"     #File which contain de list of IDs
DirectoryWhereToSearch=""           #Url of directory where search for files
BackupDirectory="./FoundedFiles"    #Url of directory where to copy the matches files
FileResume="Resume.txt"         #Contain a resume of the results
FileContainAllFiles="AllInOne.txt"  #This file contain the content of all the founded files.

if [ -f $FileResume ]; then
    rm -r $FileResume
fi

if [ -f $FileContainAllFiles ]; then
    rm -r $FileContainAllFiles
fi

touch $FileResume
touch $FileContainAllFiles 

if [ -d $BackupDirectory ]; then
    rm -rf $BackupDirectory
fi
mkdir $BackupDirectory

if [ -f $FileListName ]; then   #If the file which contain all the IDs exist

    #This while search for the files and copy all the match files.
    while read ID
    do
        echo "Searching for file with ID=$ID"
        search=$(find $DirectoryWhereToSearch -type f -iname "*$ID*")
        if [ "$search" == "" ]; then
            echo "File not founded: $ID"
            echo "File not founded: $ID" >> $FileResume
        else 
            echo "File Founded: $search"
            echo "File Founded: $search" >> $FileResume
            cp -rf $search $BackupDirectory 2>/dev/null
            cat $search >> $FileContainAllFiles
        fi
        echo "--------------------------------"
    done < $FileListName

else
    echo "IDs file does not founded"
fi

어떤 결과를 원하시는지 잘 모르겠습니다. 만일을 대비해 모든 파일의 모든 내용을 하나의 파일에 복사했고, 생성된 모든 파일에 대한 보고서를 찾을 수 있는 파일도 만들었습니다.

도움이 되셨다면 투표 부탁드립니다 ;)

관련 정보