Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This might have been answered already. But I wasn't able to find it.

I have:

directory
directory/subdirectory/subdirectoryofsubdirectory
directory/sub-subdirectory/subdirectoryofsub-subdirectory/subdirectoryofsubdirectoryofsub-subdirectory
directory/sub-sub-subdirectory
directory/sub-sub-sub-subdirectory

each directory and subdirectory contains files. I want to list them all, pipe the list to cat, and cat the list to a text file. Can this be done with a 'one liner'? If so, please provide it. Or does it require a script and, if so, what commands/structures will I need to write the script?

share|improve this question

2 Answers 2

up vote 11 down vote accepted

I think that your best bet is the find command. If you want just the files and not the directories in your list, something like this:

find directory/ -type f -print > textfile

The find command will recursively list the files. (If you want the directories listed too, remove the -type f). The > textfile redirects stdout to a file named textfile with no need for cat.

find is a powerful tool with many options. See man find if you find that you need to tweak the output slightly.

UPDATE (thanks gvkv):

If you want to put the contents of the files into a single text file, then one way to do it is:

find Documents/ -type f -exec cat {} + > textfile
share|improve this answer
    
If you'd like to print only the filename, not the full path, use -printf '%f\n' instead of -print. –  Adam Byrtek Sep 20 '10 at 22:53
1  
Does the OP want a list of files in the text file or the contents of the files in the text file? I interpreted the question as the latter. –  gvkv Sep 20 '10 at 23:57
    
@gvkv good point. I will update my answer. –  Steven D Sep 21 '10 at 0:13
    
use xargs instead of -exec for better process efficiency. –  jmtd Sep 24 '10 at 22:22
1  
@jmtd: According to the man page, the + rather than \; should be roughly the same as using xargs. –  Steven D Sep 25 '10 at 2:09

find is the generic way, as Steven D's answer explains. If your shell supports it, ** can be used to match any number of subdirectories (on bash it's the same as * and only matches one by default, but see Dennis' comment), so you can just do:

cat **/*
share|improve this answer
    
Dude, where do you learn this stuff? Awesome! –  gvkv Sep 20 '10 at 23:56
2  
learn this? micheal writes it down here first, then hacks into YOUR ( and everyone elses ) pc and implements the behaviour there. +1 @micheal –  Stefan Sep 21 '10 at 7:57
3  
Bash 4 will do that if you shopt -s globstar. –  Dennis Williamson Sep 21 '10 at 16:06
    
@gvkv: Probably by Reading the Friendly Manual :) –  Adam Byrtek Sep 21 '10 at 18:20
    
@Michael Mrozek I've never ventured outside of bash in my 8 years on Linux. I've heard good things about zsh, though. –  ixtmixilix Sep 27 '10 at 14:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.