Skip to content

Instantly share code, notes, and snippets.

@quicksnap
Last active December 12, 2015 05:08
Show Gist options
  • Save quicksnap/4719144 to your computer and use it in GitHub Desktop.
Save quicksnap/4719144 to your computer and use it in GitHub Desktop.
`cat` and `grep`
Hey I'm some file.
Bob
Bob Joe Barny
Hey Unix
hey Linux
La dee dah.

cat and grep are essential Linux keywords. They're up there with ls and cd--you just have to know what they do.

cat basically prints out the contents of a file. So, with the file in this Gist, "some_file", if you were to type the command, cat some_file, you would get the output as the following:

$ cat some_file
Hey I'm some file. 

Bob

Bob Joe Barny

Unix

Linux

La dee dah.

grep searches a file. So if you run grep PATTERN some_file:

$ grep "Joe" some_file
Bob Joe Barny

grep is case-sensitive:

$ grep "hey" some_file
hey Linux
$ grep -i "hey" some_file
Hey I'm some file.
Hey Unix
hey Linux

Add an option for line numbers:

$ grep -in "hey" some_file
1:Hey I'm some file.
7:Hey Unix
9:hey Linux

When you learn about pipes, you can filter output from one program and search for certain items, like one type of file:

$ ls /bin | grep cat
bzcat
cat
netcat
zcat

$ ls /etc/ |grep pass
mustchangepassword
passwd
passwd-
passwd.homedirs
passwd.homedirs-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment