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.

Watch: How to Find Any File in Linux: find, locate & grep (Fast)

Read the video transcript

Sooner or later, you have to find a file on a Linux server. In this video, you'll learn the commands that do it fast: find, locate, which, and grep. All live, on a real Ubuntu server, with SSH Command. We're connected, with the Shell view open. I've made a practice folder called find-demo, with a mix of files to search. find lists everything inside a folder, all the way down. The dot means the current folder. Add -name to search by file name. Put the pattern in quotes, so the shell doesn't expand it. Here are all the .txt files. -type d finds only folders, and -type f finds only files. -iname ignores capital letters. This finds every file with report in its name, however it's written. -size finds files by size. +100M means bigger than one hundred megabytes. This is how you track down what is filling a disk. -mtime filters by age. -mtime +7 means changed more than seven days ago. -mtime -1 means changed within the last day. Once you've found files, you can act on them. -exec runs a command on each result. Here, ls -lh shows their sizes. Always test without -delete first, because deleting has no undo. locate is much faster, because it searches a database of file names instead of walking the disk. Try it on sshd_config. -n 5 limits the output to five results. The catch: the database is refreshed only once a day, so a brand new file won't be there. sudo updatedb refreshes it, and now the file turns up. which tells you where a command lives. whereis also finds its manual page. To find a file by what is inside it, use grep with -r, which searches every file in a folder. -n adds line numbers. Let's find every TODO. And in SSH Command, the search bar lists these commands with their options. Type find, and pick one to copy. That's how to find files on Linux: find for the details, locate for speed, which for commands, and grep for contents. The full guide is at sshcommand.com, under search commands. SSH Command is free for personal use. The link is in the description. Thanks for watching.

#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