Shorts
Git – Remove untracked files matching a pattern
Solution
$ git clean [-n] -f <path>...
Example
Run:
$ git clean -n -f *.json
to remove all untracked json files.
Important note
Always use -n or --dry-run before running the command.
Remove first line from a very large file
$ tail -n +2 very_large_export_file.csv > very_large_export_file_NEW.csv
Ubuntu – list all local/system users
$ cut -d: -f1 /etc/passwd
Connect to remote DB via ssh tunneling
The DB you want to connect to is on a remote server and cannot be accessed directly from the outside, but you do have ssh access to the remote server.
Solution
First, open a terminal window and run the following command (keep it open while you want to maintain the connection to the DB):
ssh -L localhost:<local_port>:<remote_db_host>:<remote_db_port> <remote_ssh_user>@<remote_ssh_host> [-i <permission_file>.pem]
Then, you connect to the DB at localhost:<local_port> and the remote DB credentials.
Download streamable files with Axios/NestJS
Solution
Use responseType: arraybuffer. Example in NestJS (The httpService of NestJS uses Axios):
const response = this.httpService.get(downloadUrl, {
responseType: 'arraybuffer', // <= THIS
});
return new Promise((resolve, reject) => {
response.subscribe({
next(response) {
response.status === 200 ?
resolve(response.data) :
reject(response.data);
},
error(err) {
reject(err);
},
}
});
});
Search and replace new lines in Vim
Solution
When using the search and replace feature in Vim (:%s/search/replace/), you must use \n to search for new line characters and \r to replace them with new line characters.
Example of replacing new line characters with commas:
:%s/\n/,/g
Example of replacing commas with new line characters:
:%s/,/\r/g
Prevent Postgres from automatically lower casing constraint names
This doesn’t work:
ALTER TABLE company DROP CONSTRAINT UQ_924dc2ee53aa15f1b16b4af12be;
Postgres says that this constraint doesn’t exist.
Solution
The problem is that Postgres will lowercase the constraint name. Use double quotes around the constraint name, like this:
ALTER TABLE company DROP CONSTRAINT "UQ_924dc2ee53aa15f1b16b4af12be";
Reference groups in search and replace in Vim
Solution
Use \x where x is the number of the group and backspace group parentheses.
Let’s say you want to add dashes around digits in a text file, from “today is 20 May 2022” to “today is -2–0- May -2–0–2–2-”. You can do it like this:
:%s/\(\d\)/-\1-/g