Skip to Content

By mastering these shell techniques, you can also become a senior engineer

 

Ever found yourself drowning in a sea of terminal commands, struggling to remember the exact syntax or wondering how to chain multiple commands efficiently?

I’ve been there too. I remember my first deep dive into shell scripting, where I felt overwhelmed by the sheer number of tools and techniques available.

It was only after countless hours of experimenting and learning that I began to grasp the power of advanced shell techniques.

This guide is the result of that journey. It’s designed to help you navigate the complexities of shell scripting with confidence.

From combining commands to managing jobs and downloading files, these techniques will streamline your workflow and make your command-line experience more efficient.

Let’s dive in and discover how you can leverage these powerful tools to become a shell scripting pro.

1. Advanced Examples Combining Multiple Shell Commands

Combining shell commands allows you to perform complex operations and data manipulations in a streamlined manner. Here are some advanced examples:

Listing and Filtering Files:

ls -lh | grep 'Aug' | sort -k5 -h

This command lists files with detailed info, filters by the month of August, and sorts them by size in human-readable format.

Count and Display Unique Items:

cat file.txt | grep 'term' | sort | uniq -c

This command counts the number of unique lines containing ‘term’.

Find and Archive Files:

find /path/to/dir -type f -mtime -7 | tar -czvf recent_files.tar.gz -T -

This uses find to locate files, then tar to archive them into a compressed file.

2. Pipes and Redirection

Pipes and redirection are fundamental for effective command-line usage:

Combining Commands:

ps aux | grep 'nginx'

This command finds processes related to ‘nginx’.

Redirection:

echo "Error log" > error.log

This creates or overwrites error.log with “Error log”. To append:

echo "Another error" >> error.log

Error Redirection:

command > output.log 2> error.log

This redirects standard output to output.log and errors to error.log.

3. The tee Command

The tee command is useful for real-time logging and debugging:

Basic Usage:

dmesg | tee system.log

This captures kernel messages in system.log and displays them in the terminal.

Appending Output:

tail -f /var/log/syslog | tee -a syslog_watch.log

This command monitors and appends system logs in real-time.

Use in Pipelines:

find / -name "*.log" | tee log_files.txt | xargs cat

This lists .log files, saves the list to log_files.txt, and concatenates the files.

4. Shell Expansion

Shell expansions simplify complex command constructions:

Pathname Expansion:

ls /home/user/*.txt

Lists all .txt files in /home/user/.

Tilde Expansion:

cd ~/projects

Changes directory to projects in the user’s home directory.

Parameter Expansion:

echo ${HOME}

Prints the home directory path.

Command Substitution:

latest_file=$(ls -t | head -n 1)

Stores the most recently modified file in the latest_file variable.

Arithmetic Expansion:

echo $((10 * 2 + 5))

Evaluates to 25.

Brace Expansion:

mkdir project/{src,bin,doc}

Creates src, bin, and doc directories under project.

Quote Removal:

echo "$(date)"

Evaluates and prints the current date.

5. Job Control in Shell Programming

Managing multiple processes is crucial for multitasking in the shell:

Foreground and Background:

long_running_command &

Runs long_running_command in the background. Use fg to bring it back to the foreground.

Viewing Jobs:

jobs

Shows the status of jobs.

Terminating Jobs:

kill %1

Sends a termination signal to job number 1. Use kill -9 %1 for forceful termination.

Process Status:

ps aux

Lists all active processes with detailed information.

6. Downloading Files with curl and wget

Both curl and wget are powerful tools for downloading content:

Using curl:

curl -O http://example.com/file.txt

Downloads file.txt from the specified URL.

Handling POST Requests:

curl -X POST -d "param1=value1&param2=value2" http://example.com/api

Sends a POST request with form data.

Using wget:

wget -r -np -k http://example.com/

Recursively downloads the website, avoiding parent directories, and converts links for offline viewing.

7. Working with Shell Commands

Efficiently using shell commands involves knowing the right tools:

Manual Pages:

man ls

Shows the manual page for the ls command.

Help Command:

ls --help

Provides usage details and options for ls.

Which and Type:

which python

Finds the path to the python executable. type ls indicates if ls is a built-in command, alias, or executable.

Alias:

alias ll='ls -la'

Defines ll as a shortcut for ls -la.

8. Keyboard Tricks

Mastering keyboard shortcuts can greatly improve command-line efficiency:

Navigation Shortcuts:

  • Ctrl + A: Move to the beginning of the line.
  • Ctrl + E: Move to the end of the line.
  • Alt + B: Move backward by a word.
  • Alt + F: Move forward by a word.

Text Editing Shortcuts:

  • Ctrl + K: Delete from cursor to the end of the line.
  • Ctrl + U: Delete from cursor to the beginning of the line.
  • Ctrl + W: Delete the word before the cursor.

Command History Shortcuts:

  • Ctrl + R: Search command history interactively.
  • Ctrl + P / Ctrl + N: Move up and down through command history.

Miscellaneous Shortcuts:

  • Tab: Autocomplete filenames and commands.
  • Ctrl + C: Interrupt a running command.
  • Ctrl + D: Exit the shell or end input.

 

Conclusion

Mastering advanced shell techniques empowers you to perform complex tasks more efficiently and automate repetitive processes.

By leveraging command combinations, pipes, redirection, job control, and tools like curl and wget, you can significantly enhance your productivity and streamline your workflow. Experiment with these techniques to discover their full potential and transform your command-line experience.

If you have any specific questions or need further assistance, don’t hesitate to reach out!