
Linux terminal tricks
The Linux terminal is an incredibly powerful tool, capable of handling everything from simple file operations to complex system management tasks. Whether you're a developer, system administrator, or a Linux enthusiast, using Linux commands with arguments and additional tools can save time, streamline workflows, and boost your efficiency 🚀.
I've compiled some essential Linux commands and tools, that I personally use in my day-to-day tasks. You'll find practical tips, real-world use cases, and tools every Linux user should know.
Bash commands
find command
Use this command if you want to find a file, but you don’t know where it is.
1find ~ -name "*.json" -maxdepth 3 2>/dev/null
You can use find . if you want to search in the current directory. By default, it searches with max depth. Use -maxdepth N if you want to see recursive nested files with depth N.
💡 You can suppress possible permission denied errors by redirecting errors to /dev/null. But in that case, you have a chance to miss your file. 😅
Here I will include additional useful arguments that I use:
- maxdepth - Set search depth
- name - Name of the file or regex. It’s not necessary to use regex. You can use *.json.
Redirect stdin and stderr
Let’s quickly talk about typical redirection cases. Basically, during my work, I had to deal with 3 cases:
Redirect stdout to a file
1command > output.txt
ℹ️ Use >> instead of > if you want to append and not overwrite the file.
In that case, only stdout (expected/normal output without error) will be written into output.txt. In order to write stderr into the same file or another file, you can use:
1command > stdout.log 2> stderr.log
Suppress stderr
To ignore errors, just redirect them to /dev/null (data written into /dev/null is immediately discarded):
1python main.py 2>/dev/null
Redirect stdout to stderr
1bash ./some-bash-script.sh 2>&1 i-want-to-see-output-with-errors.log
nohup & command
nohup is a must-have command to run a process as a daemon (in the background). Basically, it’s an alternative to using tmux (terminal multiplexer).
1nohup python main.py > ./my-output-file.log &
By default, output will be written to nohup.out. To overwrite output, you can redirect the command as described above (e.g. <your-command> > <output-path> &). In case you wrote something wrong and the command is executed, you can also check via top/htop and kill the process (e.g. kill -9 <pid>).
cat command
There are not many secret hacks that I found with the cat command, except concatenating files into one. If you are using ssh and want to concatenate files, use:
1cat file-1.txt file-2.txt > file-3.txt
tail -n / tail -f commands
tail shows the end of files (by default, the last 10 lines). Change the -n argument to change the number of lines to show.
1# Show last 50 lines 2tail -n 50 some-log-file.log
The second command is useful in combination with nohup. To follow the nohup command, use, for example:
1nohup python main.py > i-wanna-follow-output.log & \ 2tail -f i-wanna-follow-output.log # ℹ️ P.S. you can use `-n` as well
less +G -N command
I always use the +G argument to navigate a large file and start at the end. It’s really useful if you want to check a large log file and you are interested in the recent logs. Also, to make it pretty, I add -N to see the number of lines as well.
1less +G -N /var/log/syslog
- +G - start at the end
- -N - show the number of lines
watch -n command
Repeat some command every -n seconds (default: 2). I use it when I want to check a process by periodically running a command. For example, if I want to check some output of a spark command in a cluster that wasn’t run or is scheduled:
1# Check the directory in HDFS every second. 2watch -n 1 "hdfs dfs -ls /user/bondaika/i-need-to-see-output-of-this.log"
Useful tools
top / htop commands
top
top is included in Linux by default. There will be some cases when you can’t use htop (can’t install it / no sense) on a cluster node or on a freshly loaded Linux image, or you can’t install htop for some reason. Let’s talk about the top command first:
In macOS, since it’s not usually a server and, in most cases, a work/personal laptop, I don’t see a reason to use top/htop. We can use Activity Monitor. If you are a fan of the terminal, you most likely know all these basics 😎. Now let’s focus on the top command in Linux.
Filtering processes
In Linux, to filter, I usually use the following combination:
- top
- Press o / O (Shift + o)
- Type COMMAND=watchdog (capitalized) P.S. COMMAND=watch will include watchdog as well.
Optional way to filter process by username:
- Type u
- Write the full username (e.g. just root)
Sorting processes by Memory/CPU
Another useful use case is to sort processes that are using too much CPU/Memory:
- Shift + P - orders the list of processes using the CPU the most (descending order).
- Shift + M - processes using the most memory (Resident Set Size, a.k.a. sort by RES).
htop
htop is an improved top command. Basically, you can navigate with the mouse/shortcuts and do the same things:
Search:
- Press F3 (in macOS fn + F3)
- Type a search word, e.g. python. It will find all fields (users/commands that contain python word).
Filter:
- Press F4 (in macOS fn + F4)
- Type a search word, e.g. python 3. Navigate with ⬆️/⬇️
Sort resources most spending CPU/Memory:
- Press F6 (in macOS fn+ F6)
- Select PERCENT_CPU or PERCENT_MEM
ranger tool
Really useful to traverse directories. You just need to navigate via arrows ⬅️ ➡️ ⬆️ ⬇️. Another advantage is you don’t need to call vim/cat to view a file. Just press ➡️ and you are already in INSERT mode.
tldr tool
Simplified Man Pages. man/ --help is usefil if you are familiar with tool and want to know specific argument or so on. However, if you don't remember even to basic usage of command - use tldr. Just compare:
- man tar:
- tldr tar:
Do you see the difference?
Last words
I hope you like the article and will use all these tricks and tools!
Last Updated: 05 Jan 2025