What I want to do is search for all the files whose name meet a certain requirement (starts with 's', followed by either a '1' or a '2' and end with 'sh') and then copy the contents of all those files into a new file, (name ending with .txt).

So far, what I think it should look like is this:

cat / "s[1-2]*sh" >> /home/admin/Desktop/myFile.txt

But it does not work, reporting

cat: /: Is a directory

I'm completely out of ideas. I'm running ubuntu 18.04.1

  • All files everywhere or all files in a certain directory? – Jesse_b Apr 13 at 17:58
  • All files everywhere – Eleuis Apr 13 at 18:00

To concatenate the files in your current directory:

cat s[12]*sh > /home/admin/Desktop/myFile.txt

To find and concatenate the files in your current directory and subdirectories:

find . -name "s[12]*sh" -exec cat '{}' > /home/admin/Desktop/myFile.txt \;

To find and concatenate the files everywhere:

find / -name "s[12]*sh" -exec cat '{}' > /home/admin/Desktop/myFile.txt \;
  • 3
    note that find -exec cat {} > out.txt \; is exactly equivalent to find -exec cat {} \; > out.txt as the shell processes the redirection before find runs, and it applies to the whole find process. So you might as well put the \; before the redirection for clarity. Also, you can use -exec cat {} + to have find pass more than one file name to each cat invocation. The effect is the same, but it can be faster for large numbers of files. – ilkkachu Apr 13 at 18:07
  • 1
    To accept an answer click the ✓ – ctrl-alt-delor Apr 13 at 18:31
  • 2
    -exec cat {} + would be more efficient as it would call cat as few times as possible. – Kusalananda Apr 13 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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