Building Custom Workflows with Modern Command Line Programs
Are you tired of manually repeating the same tasks day after day? Do you dream of automating your workflow but are unsure of where to start? Look no further than modern command line programs!
In this article, we’ll explore how using modern command line programs in combination with shell scripting can automate your workflow and save you valuable time. We’ll start by covering the basics of shell scripting and then dive into the usage of three modern command line programs: fzf, sed, and xarg.
What is Shell Scripting?
Before we dive into the specifics of modern command line programs, let’s first touch upon shell scripting.
Shell scripting is a powerful tool that allows you to automate repetitive command line processes. Shell scripts are essentially programs that are run in a Unix-like shell. By writing commands that the shell interpreter will execute, you can carry out repetitive tasks with ease.
Shell scripts can be run manually or scheduled to run automatically via cron jobs. Combining shell scripting with automation programs like fzf, sed, and xarg make it easy to create custom workflows and automate your everyday tasks.
fzf: The Fuzzy Finder
fzf is a command line program that brings fuzzy search to your terminal. It lets you quickly search through a list of files or directories and select the one you want. This program is particularly useful when working with files that are difficult to remember or have a large number of files that you can’t easily navigate through.
Here’s an example of how to use fzf to find and open files on your directory:
vim $(fzf)
When you run this command, fzf will display a list of files in your current directory. Simply select the file you want and hit enter. fzf will then output the path to that file and pass it on to the vim command.
If you need to search through a larger number of files or folders, you can even pipe find into fzf so that you can quickly narrow down your search:
find . -type f -not -path "*/node_modules/*" -not -path "*/__pycache__/*" -not -path "*/build/*" -print0 | fzf --read0 --multi --reverse --preview 'bat --color=always --style=header,grid --line-range :500 {}'
In this example, we have passed the output of find
command into fzf, which makes it easy to search through a list of files and select one or more to work with. Here are some of the command options that are used in the above example:
--read0
: input items are separated by a null character instead of a newline--multi
: allows you to select multiple items--reverse
: reverses the direction of the search--preview
: provides a preview command to display more information about the selected item (in this case, it uses bat to pretty-print files with syntax highlighting).
fzf can also be used to search through multiple files and directories at once. Here’s an example of how to use it to search through all files with a particular extension:
ls **/*.py | fzf-tmux
This command will list all files with the .py
extension recursively and display the list in the tmux pane. You can then use fzf to select the file you want to work on.
sed: The Stream Editor
sed is a command line program that allows you to edit text in a Unix-like shell. It is often used to find and replace strings of text in a file, but it can also be used to insert, delete, or modify lines of text.
Here’s an example of how to use sed to find and replace a string of text in a file:
sed -i 's/original/new/g' file.txt
In this example, we are searching for the string ‘original’ in the file.txt and replacing it with the string ‘new’. We are using the -i
option to edit the file in place.
sed can also perform regular expression searches and replacements. Here’s an example of how to use sed to replace all instances of a particular pattern:
sed 's/foo/bar/g' file.txt
This command replaces all instances of ‘foo’ with ‘bar’ in file.txt. The ‘g’ flag at the end indicates that the replacement is to be done globally, rather than just the first instance.
sed can even be used to modify lines of text. Here’s an example of how to use sed to insert a line of text into a file:
sed '2iThis is a new line of text.' file.txt
This command inserts the line ‘This is a new line of text.’ into line 2 of the file.txt.
The possibilities with sed are endless, and it is an essential tool for any shell scripting enthusiast.
xargs: The Argument List Processor
xargs is a command line program that takes a list of arguments and passes them on to another command. It is often used when you have a list of files or directories that you need to iterate over and run a command on each one.
Here’s an example of how to use xargs to copy files from one directory to another:
find /path/to/source -name '*.txt' | xargs -I {} cp {} /path/to/destination
In this example, we are using the find
command to generate a list of all files with a .txt
extension in /path/to/source
. We then pass this list of files to xargs, which runs the cp
command on each file and copies it to /path/to/destination
. The -I {}
option tells xargs to replace {}
with each filename in turn.
xargs can also be used to run multiple instances of a command in parallel. Here’s an example of how to use xargs to use multiple CPU cores to insert data into a database:
find /path/to/files -name '*.csv' | xargs -P 4 -I {} sh -c 'cat {} | psql dbname -c "COPY mytable FROM STDIN WITH CSV HEADER;"'
This command reads in all CSV files in /path/to/files
and then inserts them into a database table MyTable.
The -P
option specifies how many parallel instances of the command to run. In the above example, -P 4
tells xargs to run 4 instances of the command in parallel.
Conclusion
Modern command line programs like fzf, sed, and xargs can help you automate your workflow and save valuable time. By combining these programs with shell scripting, you can create custom workflows to automate repetitive tasks.
We hope that this article has given you a taste of what is possible with modern command line programs. There is an endless number of ways you can use these tools to speed up your workflow and make your life easier. We encourage you to continue exploring and experimenting with these powerful tools.
Happy scripting!
Editor Recommended Sites
AI and Tech NewsBest Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
NFT Sale: Crypt NFT sales
Learn Beam: Learn data streaming with apache beam and dataflow on GCP and AWS cloud
LLM Finetuning: Language model fine LLM tuning, llama / alpaca fine tuning, enterprise fine tuning for health care LLMs
Jupyter Consulting: Jupyter consulting in DFW, Southlake, Westlake
Infrastructure As Code: Learn cloud IAC for GCP and AWS