Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, all: I've got two folders, folder A contains some image files (say, 100 files) in .jpg format; folder B contains all description files (say, 500 files) in .txt format. ...
  1. #1
    Just Joined!
    Join Date
    Dec 2006
    Location
    Surrey, BC, Canada
    Posts
    3

    copy all files with the same filenames as those in another folder

    Hi, all:

    I've got two folders, folder A contains some image files (say, 100 files) in .jpg format;
    folder B contains all description files (say, 500 files) in .txt format.

    All image files in folder A are able to find their corresponding description files in folder B.
    That is to say, regardless of file extension, 100 .txt files in folder B share the same name as those 100 .jpg files, for example 00011020.jpg in folder A is able to find its corresponding 00011020.txt in folder B.

    Now, my question is:
    How to just copy out the 100 description .txt files that share the same name as those 100 image files, out from folder B into folder C( or create folder C first, and then carry out the copy) ?

    Looking forward to your help.

    Best Regards
    JIA Pei

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    School work?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Dec 2006
    Location
    Surrey, BC, Canada
    Posts
    3
    Haha...
    not at all.
    My task.
    Sorry, I'm not a specialist in script.

  4. #4
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    Hmm,
    in dubio pro reo

    This oneliner is hack-ish as it lives on certain assumptions and has no errorchecking.
    Also, the multiple cp can be optimized.
    So dont use that as a cronjob and just for one time shots

    Assumptions
    - directories A, B and C exist
    - and are on the same level
    - the script is called from the directory containing A,B and C

    Code:
    for FILE in `find A/ -type f -iname "*.jpg" |sed s#A/##| sed s/\.jpg$// | sort |uniq`; do cp B/$FILE.txt C/;done
    You must always face the curtain with a bow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...