SSH Command in Linux with examples

Reading and editing text commands in Linux

Look inside files, follow growing log files and edit text from the terminal.

Watch: Read & Search Text Files in Linux: cat, head, tail & grep (Beginner Guide)

Read the video transcript

Most of what you do on a Linux server is reading text: logs, configs, notes. In this video, you'll learn the commands that make that fast: cat, head, tail, grep, wc, and the pipe that joins them. We're already connected with SSH Command, and the Shell view is open. Everything you type here runs on the server. cat prints a whole file to the screen. Try it on notes dot txt. It's perfect for short files. For longer files, head shows just the beginning. By default you get ten lines, and dash n lets you choose how many. Here are the first three lines of the application log. tail does the opposite. It shows the end of a file, which is where the newest log entries are. Here are the last three lines. grep searches for text. Give it a word and a file, and it prints only the lines that contain it. Let's find every error in the log. Add dash i to ignore upper and lower case, and dash n to show line numbers. And dash c simply counts the matching lines. wc counts things. With dash l, it counts lines. This log has thirty. Now the clever part. The pipe character sends the output of one command into the next. Here, grep finds the errors, and wc counts them. Five errors, in a single line. Pipes work with any text command. Let's see which requests the web server gets most often. sort puts the lines in order, uniq dash c counts the repeats, sort dash r n ranks them, and head keeps the top three. That's cat, head, tail, grep, wc, and the pipe. Each one has a full guide, with more examples, at ssh command dot com, under text commands. SSH Command is free for personal use. The link is in the description. See you in the next one.

#cat

Print a file

Syntaxcat file [file2 ...]
  • cat notes.txt

    Print the whole file

  • cat -n script.sh

    Print with line numbers

  • cat a.txt b.txt > all.txt

    Join two files into a new file

#less

Read a long file page by page

Syntaxless file
  • less /var/log/syslog

    Open a log; use arrows or Space to move, / to search, q to quit

  • less +F app.log

    Open and follow new lines as they arrive (Ctrl+C to stop following)

Show the first lines

Syntaxhead [-n N] file
  • head -n 5 data.csv

    First five lines

  • head -c 100 file.bin

    First 100 bytes

#tail

Show the last lines

Syntaxtail [-n N] file
  • sudo tail -n 50 /var/log/nginx/error.log

    Last 50 lines (most files in /var/log need sudo)

  • sudo tail -f /var/log/syslog

    Keep watching new lines as they are added (Ctrl+C to stop)

  • tail -F app.log

    Same, but keeps going if the log is rotated

#nano

Simple text editor

Syntaxnano file
  • nano /etc/hosts

    Open a file (use sudo for system files)

TipCtrl+O saves, Ctrl+X exits, Ctrl+W searches. The shortcuts are listed at the bottom of the screen.

#vim

Powerful text editor

Syntaxvim file
  • vim config.yml

    Open a file. Press i to type, Esc to stop typing

  • vim +42 app.py

    Open at line 42

TipTo leave Vim: press Esc, then type :wq and Enter to save and quit, or :q! to quit without saving.

#tee

Show output and save it too

Syntaxcommand | tee file
  • ls -l | tee list.txt

    Print the list and also write it to list.txt

  • echo "127.0.0.1 test" | sudo tee -a /etc/hosts

    Append a line to a root-owned file (plain > would fail)

#wc

Count lines, words and bytes

Syntaxwc [-l] [-w] [-c] file
  • wc -l access.log

    Count lines

  • ls | wc -l

    Count how many files are in the folder

#nl

Number lines

Syntaxnl file
  • nl script.sh

    Print the file with numbered lines

Run these commands with SSH Command

SSH Command is a desktop app for Mac, Windows and Linux that puts a real terminal, SFTP file panes, a sudo switch and a searchable Linux command finder in one window. Type what you want to do, and send the command to your server with one click.

Download free Windows commands with examples SFTP & FTP client