[Solved] How to concatenate files in subdirectories?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] How to concatenate files in subdirectories?
# 8  
Old 02-07-2011
@raj284
Please open a new thread describing your environment and your particular problem giving lots and lots of detail.
# 9  
Old 02-07-2011
@methyl
How to open a new thread?
# 10  
Old 02-08-2011
@cfajohnson
Please reply to my message. I badly need the solution!
# 11  
Old 02-11-2011
@cfajohnson
I saved your code in test.sh file and then executed from commnad prompt using ./test.sh. But it didn't concatenate files, instead it deleted files from all subdirectories. Please let me know what is wrong. I am concerned
# 12  
Old 02-11-2011
Quote:
Originally Posted by raj284
@cfajohnson
I saved your code in test.sh file and then executed from commnad prompt using ./test.sh. But it didn't concatenate files, instead it deleted files from all subdirectories. Please let me know what is wrong. I am concerned

Did you save it exactly as I posted it?

Did you cut and paste or retype it?

If you changed anything, what did you change?

When you ran it, did you get any error messags?
# 13  
Old 02-11-2011
@raj284
It is exceedingly dangerous to take code from the Internet that you think might match your problem and execute it on your computer unless you 100% understand what it is going to do in the context of your computer. If you are ever in this position again please feel free to describe your environment and your problem and post your question and what you are planning to run.

The expert poster cfajohnson has posted code to match the original poster's requirement and received thanks a month ago.
If you have deleted files inadvertantly, ask your Systems Adminstrator to restore them from backup.

Sorry to be blunt but may I recommend that you use your computer to research training courses.

Last edited by methyl; 02-11-2011 at 04:53 PM..
# 14  
Old 02-11-2011
@cfajohnsn

Did you save it exactly as I posted it?
Ans: No

Did you cut and paste or retype it?
Ans: I cut, pasted and edited it as shown below.

If you changed anything, what did you change?
Ans: The only thing I changed is the value of logdir as shown below.

When you ran it, did you get any error messags?
Ans: No.
I saved the script as script.sh in $HOME/testing1 where I have several subdirectories and .txt files into them. I ran script.sh from the command prompt of $HOME/testing1 as ./script.sh

The only messages I got:
cat: file11.txt: input file is output file
cat: file22.txt: input file is output file
......
......
Here is the script I have used:

logdir=$HOME/testing1 ## adjust to taste
for dir in "$logdir"/*/
do
(
cd "$dir"
files=( *.txt )
cat "${files[@]}" > "${PWD##*/}.txt"
rm "${files[@]}"
)
done

Moderator's Comments:
Mod Comment As the original question was answered, and you were asked to start a new thread, I am closing this one.

Last edited by Scott; 02-12-2011 at 09:42 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

2. Shell Programming and Scripting

Unzip all the files with subdirectories present and append a part of string from the main .zip files

Hi frnds, My requirement is I have a zip file with name say eg: test_ABC_UH_ccde2a_awdeaea_20150422.zip within that there are subdirectories on each directory we again have .zip files and in that we have files like mama20150422.gz and so on. Iam in need of a bash script so that it unzips... (0 Replies)
Discussion started by: Ravi Kishore
0 Replies

3. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

4. Shell Programming and Scripting

[Solved] Script to concatenate 2 files with the same number of lines

Hi everyone, I have two files, namely: file1: file1Col1Row1;file1Col2Row1;file1Col3Row1 file1Col1Row2;file1Col2Row2;file1Col3Row2 file1Col1Row3;file1Col2Row3;file1Col3Row3file2: file2Col1Row1;file2Col2Row1;file2Col3Row1 file2Col1Row2;file2Col2Row2;file2Col3Row2... (0 Replies)
Discussion started by: gacanepa
0 Replies

5. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies

6. Shell Programming and Scripting

[solved] merging two files and writing to another file- solved

i have two files as file1: 1 2 3 file2: a b c and the output should be: file3: 1~a 2~b 3~c (1 Reply)
Discussion started by: mlpathir
1 Replies

7. UNIX for Advanced & Expert Users

Recursively concatenate files in subdirectories with the same folder name

I'm trying to concatenate files in subdirectories with the same folder name. Say concatenate all the files in the 'current' subdirectories in 'Literature' parent directory. Literature/USA/current/ Literature/Europe/current/ Can anyone help with it? Thanks a lot! (2 Replies)
Discussion started by: joyce007
2 Replies

8. UNIX for Dummies Questions & Answers

recursively concatenate files in subdirectories with same folder name

I'm trying to concatenate files in subdirectories with the same folder name. Say concatenate all the files in the 'current' subdirectories in 'Literature' parent directory. Literature/USA/current/ Literature/Europe/current/ Can anyone help with it? Thanks a lot! (2 Replies)
Discussion started by: joyce007
2 Replies

9. UNIX for Dummies Questions & Answers

list the files but exclude the files in subdirectories

If I execute the command "ls -l /export/home/abcde/dev/proj/code/* | awk -F' ' '{print $9}' | cut -d'/' -f6-8" it will list all the files in /export/home/abcde/dev/proj/code/ directory as well as the files in subdirectories also proj/code/test.sh proj/code/test1.c proj/code/unix... (8 Replies)
Discussion started by: shyjuezy
8 Replies

10. UNIX for Dummies Questions & Answers

read files from subdirectories

hello there the problem i got: i need to list .rrd files in each sub-directory from the parent directory, then create .xml files for each rrd files, the xml file should be in the same subdirectoryas rrd file. i have tried ls |awk '{print... (3 Replies)
Discussion started by: binbintriangel
3 Replies
Login or Register to Ask a Question