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.
2. Recursively Find Directories
This command will recursively find all folders named foo relative to the current directory.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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.