17 Useful Unix One Liner Commands

Unix one liner commands

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

Rather than keeping this list of Unix one liner commands private on my Google Drive, I decided to publish it for everyone to access. Below you will find a list of Unix tips and tricks that I decided were useful enough to write down and share with you.

1. Recursively Find Files

This command will recursively find all files named foo.txt relative to the current directory.

find . -name foo.txt

2. Recursively Find Directories

This command will recursively find all folders named foo relative to the current directory.

find . -type d -name foo

3. Recursively Remove Files up to X Levels Deep

The following command will recursively find and remove all txt files up to 4 levels deep relative to the current directory.

find . -maxdepth 4 -name *.txt | xargs rm

4. Find and Replace a String in All Files

While this command is powerful, it has potential to screw things up if not executed properly. As a result of executing this command, you can replace all instances of a string with a new string in all files.

find path -type f -exec sed -i 's/oldstr/newstr/g' {} \;

5. Find Matching String in All Files

If you would like to recursively find all instances of a string in all files, you can execute the following command.

grep -r findme .

6. Find Matching String in a Certain File Type

If you would like to recursively find all instances of a string in all files with a certain file type, you can execute the following command. While the following command will find all instances of “find me” in all txt files, you can obviously modify for your needs.

grep -r --include *.txt "find me" .

7. See What Files Are Different in Two Directories

In order to compare what files exist in two directories, you can use the diff command.

diff dirA dirB

8. Redirect Output to a File

You can redirect both stdout and stderr to a file for a given command with 2>&1. While normally stderr is printed to the terminal, you can capture it in a file called output.txt. Consequently, the command will execute silently without any output to the terminal

cat nop.txt > output.txt 2>&1

9. Determine If a Process Is Running

In order to see if a process is running on your system, you can grep the output of ps. While this example looks for a process called findme, you can modify it to you needs.

ps aux | grep findme

10. Generate a Text File of Man Page

You can generate a text file of a man page by issuing the following command. While this example generates a text file for the gcc man page, you can swap out gcc for the man page of your choice.

man gcc | col -b > gcc.txt

11. Format Files with Proper Unix Line Endings

I can’t stand when I bring a file from a Windows system and it contains those annoying DOS line ending characters. Effortlessly remove DOS line endings and replace them with Unix line endings with the dos2unix command.

dos2unix file_from_windows.txt

12. Remove All Files Except

You can remove all files in a directory except for a certain file by using the -v or –invert-match argument for the grep command. First of all, list all files in a directory. Then, pipe that list to grep where you will essentially select all files except the dontdelete.txt file. Finally, pipe the resulting list to xargs and rm. Also, the -f or force argument for rm is optional.

ls * | grep -v dontdelete.txt | xargs rm -f

13. Ping All Devices on Network

Often times, it’s useful to get a list of all devices on your network with their associated IP addresses. You can accomplish this with the arp command.

arp -a

Although this post isn’t about software development, the reason I am familiar with Unix one liner commands like these are from my six years as a software engineer. It seemed like I was working on the command line on a daily basis. Consequently, check out some of my other software tutorials here.

Also, if you have any questions, let me know below.


Meet Tony

Tony from Tony Teaches Tech headshot

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

Leave a Reply

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