Top 10 Handy Linux Command Tricks You Should Know
Linux is an incredibly powerful operating system, full of hidden gems and shortcuts that can significantly speed up your workflow. While seasoned Linux users may already be familiar with some of these, there are plenty of tricks that can make your life much easier. Here are 10 Linux command tricks that you should know to become more efficient in your day-to-day operations.
1. Cursor Movement Shortcuts for Command Line Navigation
When typing long commands in the terminal, you often need to modify specific parts. Instead of using the arrow keys repeatedly, here are some useful shortcuts to speed things up:
• Ctrl + a: Move the cursor to the beginning of the line.
• Ctrl + e: Move the cursor to the end of the line.
• Ctrl + w: Delete the word before the cursor.
• Esc + b: Move the cursor one word to the left.
• Esc + f: Move the cursor one word to the right.
These shortcuts can save you a lot of time when editing long commands!
2. Vim Quick Operations
Vim is a popular text editor used to modify configuration files. Here are some quick operations to enhance your efficiency in Vim:
• :set nu: Display line numbers in the file.
• :20: Move the cursor to line 20.
• :%s/old/new/g: Replace all occurrences of “old” with “new” in the file.
• ddp: Swap the current line with the one below it.
• ci": Delete everything between the quotes, useful for editing parameters in configuration files.
Mastering these Vim commands will significantly speed up your text editing tasks.
3. Quickly Jump to the Previous Directory
When working in multiple directories, you often need to switch back and forth between them. Instead of typing out the full directory path, use this simple command:
cd -
This will take you back to the last directory you were in, which is very useful when frequently switching between two directories.
4. Copying Files Between Servers Without Password Using nc or python
Copying files between servers is a common task. When scp is not feasible (e.g., you don’t have access to the target server’s password), here are two workarounds:
1. Using nc (Netcat):
On the source machine: nc -l 10017 < file.sh
On the target machine: nc source_ip 10017 > file.sh
2. Using Python’s HTTP server:
On the source machine: python -m SimpleHTTPServer 10010
On the target machine: wget http://source_ip:10010/file.sh
These methods allow you to transfer files quickly without needing authentication.
5. Simplifying Commands
You can use Linux commands to either empty files or create new ones, and here are some shortcuts:
> file.log: Clears the contents of file.log or creates the file if it doesn’t exist.
Ctrl + c: Interrupts a command and saves its output to a file.
Press Esc + .: Quickly reinsert the last segment of the previous command.
For example, if you just created a directory using mkdir -p /path/to/dir, you can quickly navigate into it by typing cd, then pressing Esc + ..
6. Check Internal IP Address
If you need to find your machine’s internal IP address, use the following command:
hostname -i
Please note, this method works only if DNS is configured on your machine.
7. Quick Redis Connection Without Redis CLI
If you don’t have the Redis client installed, you can still connect to Redis using telnet:
telnet 127.0.0.1 6379
While this is a quick and easy way to connect to Redis in emergencies, it’s not ideal for production environments, as telnet doesn’t support all Redis commands.
8. Send a Running Task to the Background
Sometimes, you need to pause a task (e.g., a script running in Vim) and jump back to the command line. Here’s a quick way to do that:
Ctrl + z: Pause the current task and send it to the background.
fg: Resume the paused task.
This is useful when you’re in the middle of editing a file and need to run some commands in the terminal.
9. Find the Directory of a Running Process
If you need to locate where a running process is based, use the following command:
pwdx <pid>
This command will tell you the current working directory of the process with the specified PID. This is useful if you need to identify where an application is running, especially for resource-intensive processes like Python scripts.
10. Save Command Output to a File and Display It Simultaneously
When running a script or command that produces a lot of output, you might want to save the output while also displaying it on the terminal:
command | tee output.log
For example, instead of running python script.py > output.log and checking the log in a separate terminal, you can use python script.py | tee output.log to display the output in real-time while saving it.
Conclusion
These Linux command tricks can significantly boost your productivity, whether you’re a sysadmin, developer, or just a power user. Incorporate these tips into your daily workflow, and you’ll notice how much time and effort they save. Linux is all about efficiency, and mastering these small yet powerful tricks can make a big difference.
Which of these have you used before, and which ones are new to you? Feel free to share your favorite Linux command-line tricks!
Subscribe to my newsletter
Read articles from Nan Song directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by