SSH Command in Linux with examples

Search and filter commands in Linux

Find text inside files, find files by name or size, and shape the output of other commands.

#grep

Search for text

Syntaxgrep [options] pattern [file ...]
  • grep "error" app.log

    Lines that contain error

  • grep -i "timeout" app.log

    Ignore capital letters

  • grep -rn "TODO" src/

    Search every file in a folder, show line numbers

  • grep -v "^#" config.conf

    Show lines that do NOT start with # (hide comments)

  • grep -c "404" access.log

    Just count the matching lines

  • ps aux | grep nginx

    Filter another command's output

#find

Find files by name, size or age

Syntaxfind path [tests] [action]
  • find /var/www -name "*.php"

    Every .php file below /var/www

  • find . -type d -name "backup*"

    Folders whose name starts with backup

  • sudo find /var/log -size +100M

    Files bigger than 100 MB (sudo lets it look into every folder)

  • find . -mtime -1

    Files changed in the last day

  • find /tmp -name "*.tmp" -delete

    Find and delete (test without -delete first)

  • find . -type f -exec chmod 644 {} \;

    Run a command on every file found

#locate

Find files fast using an index

Syntaxlocate name
  • locate nginx.conf

    Instant results from the file database

  • sudo updatedb

    Refresh the database so new files are found

#which

Show where a command lives

Syntaxwhich command
  • which python3

    Prints the path, such as /usr/bin/python3

#whereis

Locate a program, its manual and source

Syntaxwhereis command
  • whereis ssh

    Shows the program and manual page paths

#awk

Work with columns of text

Syntaxawk 'pattern { action }' file
  • awk '{print $1}' access.log

    Print the first column (the IP address in many web logs)

  • awk -F: '{print $1}' /etc/passwd

    Use a colon as the separator and list user names

  • df -h | awk '$5+0 > 80'

    Show only disks that are more than 80% full

#sed

Find and replace in text

Syntaxsed 's/old/new/g' file
  • sed 's/foo/bar/g' file.txt

    Print the file with every foo changed to bar

  • sed -i 's/8080/9090/' app.conf

    Change the file itself (in place)

  • sed -n '10,20p' file.txt

    Print only lines 10 to 20

TipOn macOS the in-place flag needs an empty argument: sed -i '' 's/a/b/' file.

#sort

Sort lines

Syntaxsort [options] file
  • sort names.txt

    Alphabetical

  • sort -n numbers.txt

    Numeric order

  • sort -rn -k2 data.txt

    Biggest first, by column 2

#uniq

Remove repeated lines

Syntaxsort file | uniq [-c]
  • sort ips.txt | uniq -c

    Count how many times each line appears

  • sort ips.txt | uniq -c | sort -rn | head

    The ten most common lines

#cut

Cut out columns

Syntaxcut -d delimiter -f fields file
  • cut -d, -f1,3 data.csv

    Columns 1 and 3 of a CSV

  • cut -c1-10 file.txt

    First ten characters of each line

#tr

Change or delete characters

Syntaxtr set1 set2
  • echo "hello" | tr a-z A-Z

    Prints HELLO

  • tr -d '\r' < dos.txt > unix.txt

    Remove Windows line endings

#xargs

Build commands from a list

Syntaxcommand | xargs command
  • find . -name "*.log" | xargs ls -lh

    Run ls on every file found

  • cat urls.txt | xargs -n1 curl -O

    Download every URL in a list

#diff

Compare two files

Syntaxdiff [options] file1 file2
  • diff old.conf new.conf

    Show the differences

  • diff -u old.conf new.conf

    Show them in the common unified (patch) style

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