cat command error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cat command error
# 1  
Old 11-30-2011
Question cat command error

I use the "cat" command to cancaterate files into a new file.

Sometimes the cat command failed because there were too many files in the directory for cancateration.

I also tried to use the "ls" command to put the list of files into a text file and then cancaterate each file one by one by reading the text file. However when there are too many files, it failed the "ls" command as well.

What other method can I use?

Regards
Lena
# 2  
Old 11-30-2011
The way to avoid too many arguments is not cramming the too many arguments into ls in backticks, or brackets, or anything else. Too many arguments is too many arguments no matter how you do it. It's an operating system limit.

The way to avoid too many arguments is to not use too many arguments. Often this means putting them through a pipe instead of putting them directly on the commandline. You had the right idea using ls, it is able to list them all, but just putting the output of ls into arguments gets you right back where you started. It's too many arguments no matter where they come from.

The xargs command is also useful, as it will convert arguments from a pipe into arguments on a commandline, running the command you ask it to with arguments limited to the maximum number the OS allows. If there are too many, it will run the program several times.

Code:
ls | xargs -I{} cat > /path/to/destination

If for example, the maximum number of arguments was 4 and it had 10 files, it'd run "cat file1 file2 file3 file4", then "cat file5 file6 file7 file8", then "cat file9 file10". All the output of those would go into the same destination file, as if you'd run "cat file1 file2 file3 file4 file5 file6 file7 file8 file9 file10".

The real limit is much higher than 10 but hopefully that gets the idea across.
# 3  
Old 11-30-2011
You can also try something like this:

Code:
find . -type f -name "*" -exec cat >> newfile {} \;

# 4  
Old 11-30-2011
-name "*" is redundant. It matches everything, which find does by default anyway.

You should also put >> newfile at the end of the line, after {} \;

On Linux, you can do '+' instead of \; which will cause find to act like xargs, putting as many arguments into one cat as possible.
# 5  
Old 12-05-2011
Bug Cat error

Thanks for all the help. I shall certainly try them and see if it works.
# 6  
Old 12-05-2011
The only really safe way is to run cat once for every file. Also by avoiding "ls" we can deal with filenames containing space characters.


Code:
# Initialise concatonated file (which is not in this directory)
concatonated="/tmp/concatonated_file"
> "${concatonated}"
# Append each file one-by-one
find . -type f -print | while read filename
do
         cat "${filename}" >> "${concatonated}"
done

This User Gave Thanks to methyl For This Post:
# 7  
Old 12-05-2011
How is this better than the the solution with 'find', which also runs cat for each individual file?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting Error in with cat command

Hi All , Below is the script which I have written - export LOGDIR=/home/rmsbatch/testdata # TEMPDIR : Path where all non zero files need to be moved after search. export TEMPDIR=/home/rmsbatch/testdata/TEMP # RESULTFILE : Path of the Final File export... (3 Replies)
Discussion started by: LoneRanger
3 Replies

2. UNIX for Advanced & Expert Users

Help need on cat command

I have two files as below 1.txt AA 123 CC 145 DD 567 2.txt AA 111 YY 128 CC 144 FF 222 DD 777 ZZ 875 basically 1.txt is updated file, if i do cat 1.txt 2.txt output should be as below o/p (4 Replies)
Discussion started by: Tecnical_help12
4 Replies

3. Shell Programming and Scripting

Help need on cat command

I have two files as below 1.txt AA 123 CC 145 DD 567 2.txt AA 111 YY 128 CC 144 FF 222 DD 777 ZZ 875 basically 1.txt is updated file, if i do cat 1.txt 2.txt output should be as below o/p (2 Replies)
Discussion started by: Tecnical_help12
2 Replies

4. AIX

cat command

Hi. How can i write a command on AIX like the one i did at linux that find string in a file and show me that string, and return 3 lines before and 4 lines after that string. my linux command is: /bin/cat <filename> | tail -150 | grep -B2 -A8 "<string to look for>" Example: /bin/cat ... (10 Replies)
Discussion started by: yechi_r
10 Replies

5. Homework & Coursework Questions

Need some help on using cat command

I have a file "sample.txt" with the content as below: Hi This is a Sample Text. I need a single command using cat which serve the following purpose. 1.display the contents of sample.txt 2.append some text to it 3. and then exit But, all should be served by a sinle... (1 Reply)
Discussion started by: ashok.g
1 Replies

6. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

7. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

8. UNIX for Dummies Questions & Answers

CAT command

All - how do i save the file after i used CAT command line to modify? Thanks :confused: (2 Replies)
Discussion started by: March_2007
2 Replies

9. Shell Programming and Scripting

cat command

What does the below command means $cat <<% >abc.txt (3 Replies)
Discussion started by: surjyap
3 Replies

10. Shell Programming and Scripting

the CAT command

hi everybody, how do i open a txt file writen in unix on to a web page so when i want to view the txt file that was generated from a shell program, that file is open on a web page do i use the cat > filename.html command to do this, or is there another way many thanks :D (2 Replies)
Discussion started by: alexd
2 Replies
Login or Register to Ask a Question