197

Assuming i have a line that i want to add to a file without opening an editor.

How could i append this line

alias list='ls -cl --group-directories-first'

to this file

config.fish
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
NES
  • 33,195

3 Answers3

310

You can append a line of text to a file by using the >> operator:

echo "hello world" >> my_file.txt

or in your case

echo "alias list='ls -cl --group-directories-first'" >> config.fish

Please take note of the different types of quotes.

  • 33
    I use echo myself, but be careful, if you only specify one > then the file will truncate, not append. for a safer command you can use sed: sed -i '$a hello world' filename – invert Jan 25 '11 at 08:05
  • 16
    explanation: -i will update the file (otherwise it will just print the result to stdout), $ is regex that will match the end of the file, and a appends the following text to filename. – invert Jan 25 '11 at 08:08
  • 2
    echo "hello world" >> my_file.txt does not create a new last line with HW, but add it to the string of the last line. – Timo Nov 03 '17 at 07:28
  • What is HW @timo ? – 7wp Jul 03 '18 at 03:50
  • 1
    Maybe "Hello World" @7wp :) It's echo that adds the line break (making it a line as opposed to just a bunch of characters). You can switch off the line break at the end with -n. – Stefano Palazzo Jul 03 '18 at 14:59
  • what if i want to take the output from a command, in my case ´pwd´ and append the output? – Andrew Nov 26 '19 at 20:43
  • for example, i can do pwd >> login.conf but can't seem to make it work with sed – Andrew Nov 26 '19 at 20:52
  • Worth noting that the command @invert suggested (i.e., sed -i '$a hello world' filename) doesn't work if the file is completely empty (e.g., if it was generated with the command : > filename). – Patrick Dark May 21 '20 at 07:06
9

There's plenty of methods of appending to file without opening text editors, particularly via multiple available text processing utilities in Ubuntu. In general, anything that allows us to perform open() syscall with O_APPEND flag added, can be used to append to a file.

  • GNU version of dd utility can append data to file with conv=notrunc oflag=append

    printf "\nalias list='ls -cl --group-directories-first'\n" | dd conv=notrunc oflag=append bs=1 of=config.fish
    

    Portably we could use something like this on the right side of pipeline:

    dd conv=notrunc seek=$(wc -c < testFile.txt) bs=1 of=testFile.txt
    

    Note the use of bs=1 , which is to prevent short reads from pipeline

  • The tee command can be used when you need to append to file and send it to stdout or to next command in pipeline

    tee -a config.fish <<< "alias list='ls -cl --group-directories-first'"
    
  • awk has append operator >> which is also portable and defined by POSIX specifications

    awk 'BEGIN{ printf "alias list=\x27ls -cl --group-directories-first\x27\n" >> "config.fish"  }'
    
  • We can combine sed's flag $ to match the last line with a for appending and -i for in-place editing.

    sed -i '$a alias list='"'"'ls -cl --group-directories-first'"'" config.fish
    
  • We could even implement something like dd in Python 3:

    #!/usr/bin/env python3
    # read bytes from stdin, append to specified file
    import sys

    with open(sys.argv[1],'ab') as f:
         f.write(sys.stdin.buffer.read())

See also:

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
8

Adding to Stefano's answer, you can also use cat:

  • Using a heredoc:

    $ cat >> config.fish <<'EOF'
    > alias list='ls -cl --group-directories-first'
    > EOF
    

    <<'EOF' means "take the following as input, until you reach a line that is just EOF". The quotes mean to take the input literally.

  • Or inputting the line on stdin:

    $ cat >> config.fish
    

    Then paste or type in the line, press Enter to go to a new line, then press Ctrl+D to mark the end.

wjandrea
  • 14,236
  • 4
  • 48
  • 98
  • I often use this method but recently got caught when I pasted in a text that included some (escape) codes. It didn't complain but when I checked the file there were chunks of pasted text missing. So use it with care! –  Sep 14 '21 at 19:53
  • @elmclose Sorry, which method? They work differently with respect to metacharacters. I think the second one doesn't do anything with them, though there might be a few exceptions. – wjandrea Sep 14 '21 at 19:59
  • I meant EOF method. Very convenient and useful when you type or paste in readable text. But codes in my text confused the process. The file was a bash script that kept failing. Took me a while before I discovered what had happened. –  Sep 14 '21 at 21:31