What's the right command to copy the content from file X and File Y to create a new File XY with all letters being lowercase?

  • Please modify the tags, as you don't actually want to copy or cp anything. – Dani_l Dec 2 '15 at 11:32
  • Do you need to lowercase non-ascii characters? – Daenyth Dec 2 '15 at 20:01

Use a combination of cat and dd:

cat LIST_OF_FILES | dd of=OUTPUT_FILE conv=lcase

An example:

$ cat file1.txt 
I am File 1.

$ cat file2.txt 
Here is File 2!

$ cat file1.txt file2.txt | dd of=file12.txt conv=lcase
0+1 records in
0+1 records out
29 bytes (29 B) copied, 0,000301417 s, 96,2 kB/s

$ cat file12.txt 
i am file 1.
here is file 2!
  • 1
    Why use cat at all? dd if=LIST_OF_FILES of=OUTPUT_FILE conv=lcase – muru Dec 2 '15 at 10:53
  • 5
    Note that dd only works with ASCII letters. tr '[:upper:]' '[:lower:]' would work with any letter, except that the GNU implementation doesn't work with multi-byte characters. GNU awk's tolower() function will work or s/.*/\L&/g in GNU sed. – Stéphane Chazelas Dec 2 '15 at 10:53
  • 3
    @muru, dd supports only one if=file argument. With zsh, you could do dd < file1 < file2, but that's zsh-specific. – Stéphane Chazelas Dec 2 '15 at 10:57
  • 1
    @StéphaneChazelas dd <(cat file1 file2) should work in bash. Oh, so many ways... – a CVn Dec 2 '15 at 14:47
  • 1
    @MichaelKjörling, that should be dd < <(cat file1 file2) or dd if=<(cat file1 file2) (which btw doesn't work in ksh even though ksh is the one that introduced <(...)), but that still invokes cat. – Stéphane Chazelas Dec 2 '15 at 15:02

Use a combination of cat and tr:

cat FILEX FILEY | tr '[:upper:]' '[:lower:]' > FILEXY

  • Why use cat at all? tr '[:upper:]' '[:lower:]' < LIST_OF_FILES – muru Dec 2 '15 at 10:53
  • @muru because of KISS. cat's raison d'etre is to concatenate files. – Dani_l Dec 2 '15 at 10:55
  • 2
    @muru In my opinion < LIST_OF_FILES won't work with multiple files (only in special shells). Can you give an example what you mean? – chaos Dec 2 '15 at 10:58
  • 2
    @cas Not at all. Some uses of cat are useless. In particular, cat | foo and cat file | foo are useless and should be replaced by foo and < file foo, respectively. But not all of them are. If you want to concatenate files, use cat. – wchargin Dec 2 '15 at 19:03
  • 1
    that was my point. muru was railing against an appropriate use of cat partly because his belief that UUOC exists blinds him to the fact that cat has its uses. IMO even if a particular instance of using cat does happen to be useless, there's little point in complaining about it - cat is also useful as a placeholder for any command or pipeline of commands in examples. And if using cat makes it easier for someone to understand then it's certainly not useless. The point here is learning, not pedantry. there are plenty of things worth being pedantic about, the occasional UUOC isn't one of them – cas Dec 2 '15 at 23:05

<old_file tr 'A-Z' 'a-z' > new_file

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.