Files with same names in different folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Files with same names in different folders
# 1  
Old 06-24-2013
Files with same names in different folders

Hello,

I am looking for a command line that can do some operations on two files that have the same names but in different folders.

for example if

folder A contains files 1.txt, 2.txt, 3.txt,..
folder B contains files 1.txt, 2.txt, 3.txt,..

If I would like to concatenate the two files /A/1.txt and /B/1.txt and A/2.txt and B/2.txt, what is a shell command that I had to do to do that:

If file name in A is equal the file name in B; cat /A/1.txt /B/1.txt; done

I would like to do that for all files in folders A and B, if only names are matched.

Thank you in advance,
Regards,
Mohamed
# 2  
Old 06-24-2013
The location of the concatenated file directory is $DEST. You choose that.
The name of the newly concatenated file will be [oldfilename].cat

Code:
ls /A/*.txt >/tmp/A.lis
ls /B/*.txt >/tmp/B.lis
awk 'FILENAME=="/tmp/A.lis" {arr[$0]++}
       FILENAME=="/tmp/B.lis"  { if ($0 in arr){ print $0} ' /tmp/A.lis /tmp/B.lis > /tmp/list
while read fname 
do
    cat /A/${fname} /B/${fname} > $DEST/${fname}.cat
done < /tmp/list

There are other shorter pieces of code that can do this but you can see what this one is doing easily. So you can edit it to make minor changes.
# 3  
Old 06-25-2013
try without creating list files ...
Code:
cd /A
for file in *.txt
do
    [ -f ../B/$file ] && (cat $file ../B/$file > /dir/${file}.cat)
done

# 4  
Old 06-25-2013
The latter with && safety and " " safety and without ( )
Code:
cd /A &&
for file in *.txt
do
  [ -f ../B/"$file" ] &&
   cat "$file" ../B/"$file" > /dir/"$file".cat
done

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 5  
Old 06-25-2013
i can see that "[ -f ../B/"$file" ] &&" may be useful ...

i am unclear, however, on the usefulness of "cd /A &&" since the o/p owns the directory and knows it exists as well as is making it the reference directory ... adding the "&&" after the cd seems to be just an unnecessary check ...
This User Gave Thanks to Just Ice For This Post:
# 6  
Old 06-25-2013
Just a good habit. In
Code:
cd dir &&
rm *

with the extra && you can stay cool seeing "not found", "not a directory", "permission denied".
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 06-25-2013
i am all for good habits and i am also for efficient coding which by the way are not mutually exclusive ... however, putting in unnecessary checks in code that is not standard coding practice -- as well-meaning as it is -- does not help readability and increases the possibility of confusion ...

otherwise coding will be ...
Code:
#! /bin/ksh

cd /dir &&
pwd &&
ls -l &&
cd /otherdir &&
pwd &&
ls -l &&
date +%Y%m%d &&
hostname &&

exit 0

This User Gave Thanks to Just Ice For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy files/folders and show the files/folders?

Hi, So i know we use cp -r as a basic to copy folders/files. I would like this BUT i would like to show the output of the files being copied. With the amazing knowledge i have i have gone as far as this: 1) find source/* -exec cp -r {} target/ \; 2) for ObjectToBeCopied in `find... (6 Replies)
Discussion started by: Imre
6 Replies

2. UNIX for Dummies Questions & Answers

Allocating names to folders based on a file

Hi everyone, I have a problem and I would be gratful if you can help. I have set of folders with files in them. e.g. data1, data2, data3 and I have a json file with info ... looking like this I want to rename my files to replace the data with their gender to some processing and back to... (8 Replies)
Discussion started by: A-V
8 Replies

3. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

4. OS X (Apple)

Remove leading spaces from file names and folders

Hi All, I have a vexing issue with leading spaces in file names. Basically, we're moving tons of data from our ancient afp file share to Box.com and Box forbids leading spaces in files or folders. The HFS file system seems to be perfectly fine with this, but almost all other Unix file systems... (1 Reply)
Discussion started by: prometheon123
1 Replies

5. Shell Programming and Scripting

Ordering Folders having Date as Names

Hi All, I have directories under /development/arun/weekly/ 20120421 20120414 . . . . I need to arrange these directories in descending order. folder name with recent date will be on top and then others. (1 Reply)
Discussion started by: Arun Mishra
1 Replies

6. Shell Programming and Scripting

List all the files in the present path and Folders and subfolders files also

Hi, I need a script/command to list out all the files in current path and also the files in folder and subfolders. Ex: My files are like below $ ls -lrt total 8 -rw-r--r-- 1 abc users 419 May 25 10:27 abcd.xml drwxr-xr-x 3 abc users 4096 May 25 10:28 TEST $ Under TEST, there are... (2 Replies)
Discussion started by: divya bandipotu
2 Replies

7. UNIX for Dummies Questions & Answers

Searching for folders/parent folders not files.

Hello again, A little while back I got help with creating a command to search all directories and sub directories for files from daystart of day x. I'm wondering if there is a command that I've overlooked that may be able to search for / write folder names to an output file which ideally... (2 Replies)
Discussion started by: Aussiemick
2 Replies

8. Shell Programming and Scripting

Archive different folders based on their names

This is my first post so ... be gentle:) Hello I have several folders that are backed up daily in following format: /back_YY.MM.DD/backup1/* ........................./backup2/* I looking a script to archive and rename all backup folders bazed on root folder... (8 Replies)
Discussion started by: vilibit
8 Replies

9. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

Hi, all: I've got two folders, say, "folder1" and "folder2". Under each, there are thousands of files. It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command. However, if I change the above question a... (1 Reply)
Discussion started by: jiapei100
1 Replies

10. UNIX for Dummies Questions & Answers

Copying multiple folders to local machine (don't know folder names)

Hi. I'm trying to copy multiple folders from the remote machine to the local machine. I wrote a batch file to run an ftp window. The problem I am having is that the only command to copy files is mget *, and this copies only files, not folders. For example, ftp ts555 cd ts555/test ' test... (5 Replies)
Discussion started by: leenyburger
5 Replies
Login or Register to Ask a Question