That's a rather pointless construct. It's inefficient (requires fork-exec to create a subshell and run ls), prone to breakage if any of the file names contain an IFS character (by default, space, tab, and newline), and is bound by the system's exec()'s limit (ARG_MAX).
A simpler, safer, more efficient alternative: for txtfile in "$system"/*.txt
Thank you all for ur efforts, unfortunately none of the suggested solutions has worked for me.
I managed to get the file name, the question is how to inser the file name before the concatenation happens.
Thank you all for ur efforts, unfortunately none of the suggested solutions has worked for me.
I managed to get the file name, the question is how to inser the file name before the concatenation happens.
Unless I missed it, you never made it clear if the output filename varies with directory and if so how to choose its name. To collect the desired concatenation of all .txt files in a directory, with each file's contents preceded by its filename, the solution that follows will create a file named ALL-TEXT-FILES.txt in each directory
You mention that you have 145 directories, but neglected to explain how the code is expected to visit them. Do you have a list to feed the script, either via a pipe or command line arguments? Or are they all in a hierarchy which can be simply traveresed with find from a single root location? I will assume the later and the following script can take a single argument, the location of this starting directory. If absent, the current working directory is assumed.
I tested it and it works as I intend.
However, there is a bug in this code (which is also present in some of the other suggestions). It's unlikely to be triggered, but it's lurking ... sleeping ... hoping.
In case anyone would prefer to find it themselves...
***** CAUTION: SPOILERS AHEAD *****
If a directory happens to contain a file whose name is identical to the output file, cat will enter an infinite loop of reading-writing to itself until the machine explodes. The non-lazy solution would be to use a unique tempfile (or at least a filename that is guaranteed to be outside the traversal).
I am trying to compare/confirm the output of an script using the perl below, which does execute. However I can not seem to print $1 and $2 in each line of the input separated by a tab in each line of the output. The input file is quite large so I have only included a portion, but the format is the... (0 Replies)
I have two files for comparison which are extracts from set of xml files.
file1 has:
Comparing File: BRCSH1to320140224CC3.xml
:: TZZZ:BR
:: TAZZ:OUT
UIZZ:0 :: ERAZ:1.000000
UIZZ:0 :: CTZZ:B
UIZZ:0 :: CCAZ:MYR
Comparing File: BRMY20140224CC18REG013SPFNSY13.xml
:: TZZZ:BR
:: TAZZ:INB... (1 Reply)
What would be the best way to store the name of an extracted file from a tar to a text file?
I want to extract one file from a tar and store the name of the extracted file to a temp file.
tar -xvf tar_file.tar file_to_be_extracted (1 Reply)
Hi,
Please excuse for posting new thread on control characters,
I am facing some difficulties in removing the control character from a file extracted from top command,
i am able to see control characters using more command and in vi mode, through cat control characters are not visible ... (8 Replies)
Iam trying to execute a file that include many files but it seems my main copy.c can't read anyone of them
-----------------------------------------------------------------------------------------
Copy.c
#include <sys/stat.h>
#include <fcntl.h>
#include "tlpi_hdr.h"
#ifndef BUF_SIZE /*... (2 Replies)
If I have a script that is using
unrar e file.part1.rar
How does the script get the name of the extracted file if I don't know the extension of the file?
In my example the name would be file.***, but I wouldn't know the extension.
---------- Post updated at 05:13 PM ---------- Previous... (0 Replies)
Hi All,
I have a big file which looks like this:
0 4.5 6.7
0 3.4 6
1 5 6
1 6 4
2 9 4.44
Above is just a miniature version of the file. In fact, considering the first column of the file that is 0 0 1 1 2, the numbers in actual go until 10442.
This means my actual file looks like... (2 Replies)
Hi All,
In make file i want to include header files from my local directory and if it did not find in local directory i want to include from network directory. can any help me how i can do this?.
here is the code
INCLUDE=${include}/
this is point to network dir how i can add option that it... (1 Reply)
I am trying to run a script called install.sh as follows:
I get a jar file, and extract it using the command:
unzip filename.jar -D path/to/files
then I navigate to that directory where I extracted the files, and run the command:
sh install.sh
(where install.sh is one of the files... (12 Replies)
Hi,
I have been trying to shoot an email with the email body to be obtained from a file.
Can someone please help me with it..
I have been trying to use the MAILX commad for the same.
mailx -s "test email" -r sender@test.com < file.txt
but it sends the file as an attachment,while i... (3 Replies)