3 Useful Bash Oneliners: Find IP Address, Copy Trick, Run Previous Command with Different Arguments

Mastering one-liner commands in the terminal can significantly boost your efficiency, whether you’re managing systems or just organizing your files. These compact, clever snippets can automate repetitive tasks and save valuable time. If you’re interested in enhancing your productivity even further, check out these 20 powerful find command oneliners that can be added to your toolkit.

In this second part of our Bash oneliner series, we’ll explore three practical examples: getting your IP address for a specific network interface, quickly duplicating files by adding extensions, and re-executing previous commands with string substitutions. These tricks are not only useful but also easy to understand and adapt.

1. awk Trick: Find Out a Network Interface’s IP Address

If you want to quickly identify the IP address assigned to a specific interface (like eth0), this one-liner uses a combination of ip, grep, awk, and cut to extract and display it cleanly:

ip a s eth0 | grep "inet " | head -n 1 | awk '{print $2}' | cut -f1 -d'/'

Explanation:

  • ip a s eth0 shows the address details for eth0.
  • grep "inet " filters out only IPv4 lines.
  • head -n 1 ensures we get the first matching line (if there are multiple).
  • awk '{print $2}' extracts the IP/CIDR field.
  • cut -f1 -d'/' strips the subnet mask, leaving just the IP address.

This is especially useful when scripting or troubleshooting network issues.

2. cp Trick: Quickly Copy a File with a New Extension

Need to create a backup or variant of a file with a new extension? Use brace expansion with cp to duplicate it effortlessly:

cp filename{,.copy}

Example:

cp file1{,.copy}
ls file1*
# Outputs: file1 file1.copy

You can also use this method to append specific extensions to long filenames:

touch filewithreallylongfilename
cp filewithreallylongfilename{,.doc}
ls filewithreallylongfilename*
# Outputs: filewithreallylongfilename filewithreallylongfilename.doc

This saves typing and ensures accuracy when filenames are cumbersome.

3. Re-Run Previous Command with Substitution

You can rerun the previous command and replace every occurrence of a specific string using history expansion with the !!:gs/old/new syntax:

cd /etc/ssh/
!!:gs/ssh/skel

Explanation:

  • !! references the last command.
  • :gs/ssh/skel tells Bash to substitute every occurrence of ssh with skel.

This trick is handy when working with similar paths or commands that differ slightly. It’s an efficient way to correct or adapt commands without retyping them from scratch.

You can learn more about command history manipulation in related guides like our article on smarter tab completion using inputrc and how to filter running processes with grep.

Conclusion

These three Bash oneliners are excellent examples of how a little shell knowledge can go a long way. Whether you’re scripting, troubleshooting, or just navigating your system more efficiently, having these in your toolbox can save time and reduce manual errors.

Explore and experiment with them in your workflow, and be sure to combine them with other Bash tools and techniques for even more power. If you found these helpful, don’t miss out on our full collection of Bash tricks and tips in future posts.

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *