These Linux tools have significantly enhanced my terminal skills.
fuser
If you're a developer, you've likely encountered issues like "port already in use" or "bind address already in use," where you have no idea which program (or process) is occupying that port. fuser is a handy tool that identifies processes using files or sockets. For example, let's find process (in this case, a tcp server) running at port 5000, and kill that process.
1snwzd@kepler ~ > fuser 5000/tcp
25000/tcp: 163293
3snwzd@kepler ~ > kill 163293
objdump
I use objdump to list the shared libraries required by a binary. This is particularly useful when building minimal Docker images for Go applications which compile to binaries. For example:
1snwzd@kepler ~ > objdump -p bin/csp | grep NEEDED
2NEEDED libresolv.so.2
3NEEDED libc.so.6
find
find is used for searching files and directories based on a variety of conditions like name, size, modification date, and more. For example:
1find . -type f -name "*.txt" -size +1M -maxdepth 3 -mindepth 2
Here, . represents the current directory. -type f filters only regular files, -name "*.txt" selects files ending in .txt, -size +1M filters by size (>1MB), -maxdepth 3 and -mindepth 2 restricts search depth.
grep
grep is used to list files that match regex patterns.
1# display lines containing the words "app", "apple", or "apples", i.e. display filename of files whose text match pattern
2grep -l "^app(le|les)?$" *.txt
3
4# display lines not containing the word "banana", i.e. display text inverse of match pattern (case insensitive)
5grep -vi "^banana$" example.txt
6
7# print lines that match with regex
8grep -E "^[a-zA-Z0-9._]+@[a-zA-Z0-9]+.[a-z]{2,3}(.[a-z]{2,3})?$" email_list.txt
sed
Stream editor which is commonly used to perform find and replace.
1sed 's/hello/hi/n' example.txt # Replace the nth occurrence
2sed 's/hello/hi/g' example.txt # Replace all occurrences
3sed 's/hello/hi/ng' example.txt # Replace every nth occurrence
4sed -i 's/hello/hi/g' example.txt # In-place editing, modifies the file
5sed '2d' example.txt # Delete the second line
6sed '/hello/d' example.txt # Delete lines containing the pattern
xargs
Used to build and execute commands from standard input.
1# using end of line as delimiters
2echo -e "item1\nitem2\nitem3" | xargs -d '\n' -n 1 echo
3
4# don't run if empty
5echo "" | xargs -r echo {}
6
7# replace string
8ls *.md | xargs -I {} head -n 1 {} # prints first line of each .md file in current directory
Parallel execution:
1ls *.md | xargs -P 4 -n 1 grep "example"
Here, -P 4 specifies 4 process can be run parallely and -n 1 makes sure one filename is passed to one grep command. I am using grep in example as grep is single threaded.
tee
Used to redirect the output of a command to both the stdout and one or more files simultaneously. It is handy for creating verbose logs while executing scripts.
1ls -l | tee file_list.txt # redirect output to file
2ls -l | tee -a file_list.txt # append output to file
3ls -l | tee file_list.txt backup_file_list.txt # redirecting output to multiple files
tar
Used for archiving files and directories.
1# creating archive, c: create v: verbose :z compression :f archive file
2tar -cvzf archive.tar file1.txt file2.txt directory/
3
4# viewing archive
5tar -tvzf archive.tar
6
7# appending files to archive
8tar -rvzf archive.tar newfile.txt
9
10# extracting archive
11tar -xvzf archive.tar
Thankyou for reading my blog.