How to Search, Sort, Read, Count

Searching for Strings (grep)

To find a string within a file, just run:

grep "search string" filename

Often, you will want to search multiple files, you can do that with the recursive flag:

grep -r "search string" ./

This will print the entire line containing the string. If you just want to print filenames, use the -l flag:

grep -rl "search string" ./

Also, I'll leave this here as its bloody useful. Want to open all the files you've searched for with vim (or another text editor)?

vim $(grep -rl "search string" ./)

Searching for Filenames (find)

To find a filename, use find ./ -name filename.

You can use wildcard characters as well. So for example to find all go files:

find . -name *go

Reading Some of a File (less)

If you want to take a look inside a large file, less is your friend. less won't open the entire file - so it doesn't use as much memory - however you can still navigate and search as you would with vim or a similar tool.

Printing the Entire File (cat)

To print a file to output (stdout), use cat filename.

Reading and Watching the Tail of a File (tail)

To watch the end of a file (useful for watching logs):

tail -f file.log

To just print the last few lines of a file:

tail file.log

Editing a File (vim)

For more info click here

Sorting a File (sort)

To sort something, pipe it into sort.

For example, cat a file then pipe it:

cat filename | sort 

Finding Unique Lines (uniq)

Let's say you've run the above sort command, and wish to find unique lines:

cat filename | sort | uniq

You may wish to count how many times each line is duplicated:

cat filename | sort | uniq -c 

Counting Number of Lines in a File (wc -l)

Now lets say you wish to count the number of unique lines in a file. You could run:

cat filename | sort | uniq | wc -l

To calculate the number of lines in a file:

cat filename | wc -l

Viewing Filesize (du -sh ./*)

To view the size of a file, use du -sh filename.

Combine this with * to view the size of all files in the current directory.

Viewing Disk Usage (df -h)

The usage of various disks and partitions can be seend with df -h

Watching Memory and CPU Usage (htop, top)

htop is a great tool for monitoring cpu and ram usage.

Checkout our Web Scraping Service