1 min read

Linux Commands Reference

Reference of Linux commands to perform common operations.
Linux Commands Reference

A reference resource for Linux commands to perform common operations.

How do I find an replace text in a file using the Linux command line?

sed -i 's/old-text/new-text/g' input.txt

Use the stream editor command, better known as sed.  Pass a string to the -i argument the specifies what you want to do and follow that with the file you want to operate on. s/ invokes the substitute operation and /g specifies that the operation should be performed globally. You may be familiar with this feature as replace all instead of the more technical variant.

How do I change ownership of a directory to the current user in Linux?

sudo chown -R $(id -u):$(id -g) directory_name/

How to see the domains in an acme.json file using jq

sudo jq '.provider | .Certificates[] | .domain' acme.json

How to generate a password hash using htpasswd

htpasswd -nb username password

This will output the username:password on standard output (that is, the command line).

If you want to save it to a file:

htpasswd -c output_file username

You will be prompted for a password interactively. This will create an output file in the current directory.

How to find all Python scripts under a specific directory

This can be applied for any file extension but here's a specific case for finding Python files. Navigate to the location you want to search on command line and run the following:

python3 -c "import pathlib,pprint; pprint.pprint([i for i in pathlib.Path.cwd().glob('**/*.py') if 'site-packages' not in str(i)])"