DEV Community

Kurumi
Kurumi

Posted on • Originally published at walnut07.com

Terminal Commands that Every Developers Should Know

Today, I'm going to introduce some useful commands that every developer should know.
I will update this article as I learn more commands, so please bookmark it and check it back later!

Table of Contents:

  1. grep
  2. sed
  3. awk
  4. find
  5. rm
  6. lsof -i
  7. kill
  8. ps
  9. history

grep

grep is a command that searches for a pattern in a file and prints the lines that contain that pattern.

grep "pattern" file
Enter fullscreen mode Exit fullscreen mode

Example

This command searches for the string "walnut07", which is my username,
in the README.md file under my current directory, and prints the lines that contain the string "walnut07".

grep "walnut07" ./README.md

> # Kurumi's Blog walnut07.com
Enter fullscreen mode Exit fullscreen mode

Some Points

  • Strongly recommend using git grep instead of grep if you are in a git repository.
    • git grep is a command that searches for a pattern in the git repository that the current directory is in.
  • Use -i option to ignore case. E.g.: git grep -i "pattern"
  • Use -n option to print the line number. E.g.: git grep -n "pattern"
  git grep -n "walnut07" ./README.md
  > README.md:1:# Kurumi's Blog walnut07.com
Enter fullscreen mode Exit fullscreen mode
  • Use -v option to print the lines that do not contain the pattern. git grep -v "pattern"
  git grep -v "walnut07" ./README.md
  > README.md:# Kurumi's Blog walnut07.com
Enter fullscreen mode Exit fullscreen mode

sed

sed is a command that filters and transforms text.

sed "command" file
Enter fullscreen mode Exit fullscreen mode

Example

This command replaces the string "walnut07" with "foobar07" in the README.md file under my current directory, and outputs the result.

sed "s/walnut07/foobar07/g" ./README.md
> # Kurumi's Blog foobar07.com
Enter fullscreen mode Exit fullscreen mode

The s command is used to replace the string. The g option is used to replace all occurrences of the string.

Some Points

  • Note that sed does not edit the file in place.
    • If you want to edit the file in place, use -i and -e option. E.g.: sed -i -e "s/walnut07/foobar07/g" ./README.md
  • Use /number to specify the Nth occurrence of the string. E.g.: sed -i -e "s/walnut07/foobar07/2" ./README.md
    • This command replaces the 2nd occurrence of the string "walnut07" with "foobar07" in the README.md file under my current directory, and edit the file.
  • Use & to refer to the string that is being replaced. E.g.: sed -i -e "s/walnut07/&07/g" ./README.md
    • This command replaces the string "walnut07" with "walnut0707" in the README.md file under my current directory, and edit the file.

awk

awk is a command that filters and transforms text.

awk '{action}' file
Enter fullscreen mode Exit fullscreen mode

Example

This command prints the 2nd column of the README.md file under my current directory.

awk '{print $2}' ./README.md
Enter fullscreen mode Exit fullscreen mode

Result:

Kurumi's

tech


Built
Next.js
TypeScript
CI/CD
Enter fullscreen mode Exit fullscreen mode

Some Points

  • Use BEGIN and END to specify the action that is executed at the beginning and the end of the file.
  awk 'BEGIN {print "Start"} {print $2} END {print "End"}' ./README.md
  > Start
  > Kurumi's
  > tech
  > Built
  > Next.js
  > TypeScript
  > CI/CD
  > End
Enter fullscreen mode Exit fullscreen mode

find

find is a command that finds files in a directory hierarchy.

find directory -name "pattern"
Enter fullscreen mode Exit fullscreen mode

Example

This command finds all files that have the string "README.md" in their name under the current directory.

find . -name "*README.md*"
Enter fullscreen mode Exit fullscreen mode

Result:

./blog/README.md
./README.md
./README.md-e
Enter fullscreen mode Exit fullscreen mode

Some Points

  • Use -type option to specify the type of the file. E.g.: find . -type f
    • f is used to specify a regular file.
    • d is used to specify a directory.
    • find . -type f -name "*README.md*" finds all files that have the string "README.md" in their name under the current directory.
    • find . -type d -name "*blog*" finds all directories that have the string "blog" in their name under the current directory.

rm

rm is a command that removes files.

rm file
Enter fullscreen mode Exit fullscreen mode

Example

This command removes the README.md file under the current directory.

rm ./README.md
Enter fullscreen mode Exit fullscreen mode

Some Points

  • Use -r option if the file is a directory. E.g.: rm -r ./blog

lsof -i

lsof -i is a command that lists open files.

lsof -i
Enter fullscreen mode Exit fullscreen mode

Result:

COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    12345 user    3u  IPv4 0x1234567890abcdef      0t0  TCP *:3000 (LISTEN)
loginwindow  123 user   11u  IPv4 0x1234567890abcdef      0t0  TCP *:60000 (LISTEN)
Enter fullscreen mode Exit fullscreen mode

Some Points

  • Add a port number to the command to specify the port. E.g.: lsof -i :3000

    • This command lists open files that are using port 3000.
    • Result:
      COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
      node    12345 user    3u  IPv4 0x1234567890abcdef      0t0  TCP *:3000 (LISTEN)
    
    • Use -iTCP or -iUDP option to specify the protocol. E.g.: lsof -iTCP
    • You can also run -iTCP:PORT_NUMBER or -iUDP:PORT_NUMBER option to specify the protocol and the port. E.g.: lsof -iTCP:3000

kill

kill is a command that sends a signal to a process.

kill -signal PID
Enter fullscreen mode Exit fullscreen mode

Example

This command sends a signal to terminate the process whose process id is 12345.

kill -9 12345
Enter fullscreen mode Exit fullscreen mode

Some Points

  • Use -9 option to send a signal to the process. E.g.: kill -9 12345
    • -9 is used to send a signal to the process to terminate it.
  • Remember that we can get a process id by using lsof -i command? Go get a process id and kill it by using kill -9 command!

cut

cut is a command that removes sections from each line of files.

cut -d "delimiter" -f "field" file
Enter fullscreen mode Exit fullscreen mode

Example

This command prints the 2nd column of the README.md file under my current directory.

cut -d " " -f 2 ./README.md
Enter fullscreen mode Exit fullscreen mode

ps

ps is a command that reports a snapshot of the current processes.

ps
Enter fullscreen mode Exit fullscreen mode

Example

ps
Enter fullscreen mode Exit fullscreen mode

Result:

  PID TTY           TIME CMD
12345 ttys000    0:00.00 -bash
12346 ttys000    0:00.00 ps
Enter fullscreen mode Exit fullscreen mode

Some Points

  • Why is this command useful?
    • This command is useful when you want to know the process id of a process.
    • For example, if you want to kill a process, you can use kill -9 command with the process id.
    • Also, you can use lsof -i command to get the process id of a process that is using a specific port.
    • Then, you can kill the process by using kill -9 command with the process id.
  • Use -a option to show processes for all users. E.g.: ps -a
  • Use -u option to show processes for a specific user. E.g.: ps -u user

history

history is a command that lists the history of commands.

history
Enter fullscreen mode Exit fullscreen mode

Example

This command lists the history of commands in the terminal you are working with.

history
Enter fullscreen mode Exit fullscreen mode

Result:
This is a history of my terminal commands.

 1024  lsof -i:3000
 1025  kill -9 70894
 1026  lsof -i:3000
Enter fullscreen mode Exit fullscreen mode

Top comments (0)