cat multiple files questions

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers cat multiple files questions
# 1  
Old 10-15-2009
cat multiple files questions


I'm trying to figure out how to use cat more wisely. I have the following command, which works, but I'd like to understand how to get it to work more clearly and efficiently.

cat 'my file.001' 'my file.002' 'my file.003' 'my file.004' 'my file.005' 'my file.006' 'my file.007' 'my file.008' 'my file.009' 'my file.010' > 'my file'

But I think you're supposed to be able to do

cat file.* > file

Unfortunately, the quotes are preventing the wildcard from working. So my first question is, is there a way to get the wildcard thing to work when you need to use quotes?

The second thing is after running the command, nothing happens. I get no feedback as to what's happening. So the second question is, is there a way to make cat more verbose?

# 2  
Old 10-15-2009
You can use double-quotes to tell the shell not to split arguments on whitespaces, but still substitute globs and variables, eg:
cat "my file.*" > 'my file'

As for verbosity, cat isn't really that telling. You could put another command at the end to tell you that it's done, or you'll have to use some coding:
[ -f 'my file' ] && rm 'my file'
for file in "my file.*"
    echo "Appending ${file}"
    cat "$file" >> 'my file'

This User Gave Thanks to pludi For This Post:
# 3  
Old 10-15-2009
Actually, I found yet another way to do this. Just place the wildcard outside the quotes:

cat 'my file.'* > 'my file'

But I still like the double-quotes better Smilie

As far as the verbose command goes, sorry for my n00bness, but do I just use that whole thing in terminal, or do I need to make a script of some kind?
# 4  
Old 10-15-2009
If it's a one-shot thing, just copy & paste it into a terminal. If you need it more than once, put it into a file, write a shebang

into the very first line, save. Note that it will always look for 'my file.*' files in the current directory.
# 5  
Old 10-15-2009
Cool Smilie

Thanks very much.

---------- Post updated at 03:09 PM ---------- Previous update was at 01:20 PM ----------

Well, I checked out the mini script, and it isn't actually working.

Here's what I found. First, it doesn't look like double-quotes matter. You still get file not found errors. Only by placing the * outside the quotes, or double-quotes in this case, are the files recognized.

As well, it seems that instead of appending one file to the next, somehow, at least the way I tried it, the output file was being rm before the next append, so each file was simply appending to a now non-existing file.

As I understand it, wherever you say "my file" that means the filename of the output file, correct? So then the only place we're referring to the input files are the line:

for file in 'my file.'*

Is that right?

Last edited by rlinsurf; 10-15-2009 at 07:14 PM..
# 6  
Old 10-16-2009
You're right, the asterisk should be outside the quotes. Sorry, should have caught that, correct version below. But I can' reproduce your other error (disappearing output file).

Yes, when I say 'my file' I mean the output file, and 'my file.*' refers to the input files, as per your original post. and the for-loop is the only line that references the input files by name. Inside the loop, the name of the current file in the variable 'file'.

Correct code

[ -f 'my file' ] && rm 'my file'
for file in 'my file.'*
    echo "Appending ${file}"
    cat "$file" >> 'my file'

# 7  
Old 10-16-2009
np Smilie

I think I had forgotten the # command at the top. That works perfectly, of course.

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge multiple columns into one using cat

I will like to merge several files using 'cat', but I observe the output is not consistent. the merge begins at the last line of the first file. file1.txt: 1234 1234 1234 file2.txt: aaaa bbbb cccc dddd cat file1.txt file2.txt > file3.txt file3.txt: 1234 1234 1234aaaa bbbb cccc... (13 Replies)
Discussion started by: geomarine
13 Replies

2. UNIX for Dummies Questions & Answers

Is there any way to cat multiple files and show filenames?

Hi, Is there any way to do a cat * where it shows the name of each file in the process? Similar to what more does below? $ more ?.sql :::::::::::::: 1.sql :::::::::::::: set linesize 200 select db_unique_name, cast( from_tz( cast(... (5 Replies)
Discussion started by: newbie_01
5 Replies

3. Shell Programming and Scripting

Select answers from multiple questions using shell script

I have a text file in this format Some lines.... Question no: 1 The question? A. Answer 1 B. Answer 2 C. Answer 3 D. Answer 4 Answer:B Some lines.... Question no: 2 The question? (choose 2) (10 Replies)
Discussion started by: zorrox
10 Replies

4. UNIX for Dummies Questions & Answers

CAT multiple files according to file name

I have a folder that contains a number of files with file names as follows: XX.YYYY..ZZZ.2014.001.000000 XX.YYYY..ZZZ.2014.001.000400 XX.YYYY..ZZZ.2014.001.000800 XX.YYYY..ZZZ.2014.001.001200 XX.YYYY..ZZZ.2014.001.001600 ..... XX.YYYY..ZZZ.2014.002.000000 XX.YYYY..ZZZ.2014.002.000400... (8 Replies)
Discussion started by: quakesrus
8 Replies

5. UNIX Desktop Questions & Answers

trying to cat multiple pairs of files

I have a number of files in a directory named like this: fooP1, fooN1, fooP2, fooN2 ... fooP(i), fooN(i). I'd like to know how to combine each P and N pair into a single file, foo(i) TIA John Balwit (1 Reply)
Discussion started by: balwit
1 Replies

6. Shell Programming and Scripting

bash: cat multiple files together except first line?

Hopefully the title summarized what I need help with. I have multiple files that I would like to concatenate in bash. ie: cat file1 file2 file3 > bigfile except I do not want to include the first line from each file (). Any help? Thanks. (6 Replies)
Discussion started by: sanimfj
6 Replies

7. Shell Programming and Scripting

cat certain files in directories to files named after the dir?

Hi all, I have a directory with many subdirectories each named like so: KOG0001, KOG0002, ...KOG9999. Each of these subdirectories contain a variable number two kinds of files (nuc and prot) named like so: Capitella_sp_nuc_hits.fasta (nuc) and Capitella_sp_prot_hits.fasta (prot). The... (2 Replies)
Discussion started by: kmkocot
2 Replies

8. 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

9. UNIX for Dummies Questions & Answers

cat files

Hi, I was a typical Windows guy. Like to do things just by clicking my mouse:cool:. I got a new job now...where they are big on unix. I am trying to wet my fingures now with unix. Haven't taken the dive yet. I am trying to find a solution for this problem. Please help me with some... (4 Replies)
Discussion started by: sandeep78
4 Replies

10. Filesystems, Disks and Memory

*BSD slices - multiple questions

Can you have a second primary slice on a second hdd? I know that primary slices are defined in the mbr, but where are all the sub partitions defined at? From the OBSD installation FAQ: What exactly is in the PBR? (0 Replies)
Discussion started by: Derrek
0 Replies
Login or Register to Ask a Question