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:
- grep
- sed
- awk
- find
- rm
- lsof -i
- kill
- ps
- 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
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
Some Points
-
Strongly recommend using
git grepinstead ofgrepif you are in a git repository.-
git grepis a command that searches for a pattern in the git repository that the current directory is in.
-
- Use
-ioption to ignore case. E.g.:git grep -i "pattern" - Use
-noption 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
- Use
-voption 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
sed
sed is a command that filters and transforms text.
sed "command" file
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
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
seddoes not edit the file in place.- If you want to edit the file in place, use
-iand-eoption. E.g.:sed -i -e "s/walnut07/foobar07/g" ./README.md
- If you want to edit the file in place, use
- Use
/numberto 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
Example
This command prints the 2nd column of the README.md file under my current directory.
awk '{print $2}' ./README.md
Result:
Kurumi's
tech
Built
Next.js
TypeScript
CI/CD
Some Points
- Use
BEGINandENDto 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
find
find is a command that finds files in a directory hierarchy.
find directory -name "pattern"
Example
This command finds all files that have the string "README.md" in their name under the current directory.
find . -name "*README.md*"
Result:
./blog/README.md
./README.md
./README.md-e
Some Points
- Use
-typeoption to specify the type of the file. E.g.:find . -type f-
fis used to specify a regular file. -
dis 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
Example
This command removes the README.md file under the current directory.
rm ./README.md
Some Points
- Use
-roption if the file is a directory. E.g.:rm -r ./blog
lsof -i
lsof -i is a command that lists open files.
lsof -i
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)
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
-iTCPor-iUDPoption to specify the protocol. E.g.:lsof -iTCP - You can also run
-iTCP:PORT_NUMBERor-iUDP:PORT_NUMBERoption 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
Example
This command sends a signal to terminate the process whose process id is 12345.
kill -9 12345
Some Points
- Use
-9option to send a signal to the process. E.g.:kill -9 12345-
-9is used to send a signal to the process to terminate it.
-
- Remember that we can get a process id by using
lsof -icommand? Go get a process id and kill it by usingkill -9command!
cut
cut is a command that removes sections from each line of files.
cut -d "delimiter" -f "field" file
Example
This command prints the 2nd column of the README.md file under my current directory.
cut -d " " -f 2 ./README.md
ps
ps is a command that reports a snapshot of the current processes.
ps
Example
ps
Result:
PID TTY TIME CMD
12345 ttys000 0:00.00 -bash
12346 ttys000 0:00.00 ps
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 -9command with the process id. - Also, you can use
lsof -icommand to get the process id of a process that is using a specific port. - Then, you can kill the process by using
kill -9command with the process id.
- Use
-aoption to show processes for all users. E.g.:ps -a - Use
-uoption to show processes for a specific user. E.g.:ps -u user
history
history is a command that lists the history of commands.
history
Example
This command lists the history of commands in the terminal you are working with.
history
Result:
This is a history of my terminal commands.
1024 lsof -i:3000
1025 kill -9 70894
1026 lsof -i:3000
Top comments (0)