Node.JS Lab

G3N3R4T1NG.C0D3.4.FUN!


Quick reference of useful Linux commands. The kind of thing you need handy when emergencies arise.

Linux permissions

Linux permissions

List contents of .tgz file

tar tf /home/user/my_file.tgz

Compress folder into a .tgz file

tar cvzf /home/user/my_folder.tgz my_folder

Compress folder into a .tgz file with date

tar cvzf /home/user/my_folder_$(date +%d-%m-%Y_%H-%M-%S).tgz my_folder

Uncompress .tgz file into a folder

tar xzvf my_folder.tgz -C /home/user/my_folder

SSH Copy file (local > remote) (Upload)

scp my_file.tgz user@host:/home/user/my_file.tgz

SSH Copy file (remote > local) (Download)

scp user@host:/home/user/my_file.tgz my_file.tgz

SSH Copy directory (local > remote) (Upload)

scp -r /home/source user@host:/home/user/destination

SSH Copy directory (remote > local) (Download)

scp -r user@host:/home/user/source /home/destination

Check Linux distro and version

lsb_release -a

Follow journal and grep something

journalctl -f | grep something

Follow journal and grep several things

journalctl -f | grep -e orange -e peach -e raspberry

Follow journal and grep regex

journalctl -f | egrep '(orange|peach|raspberry)'

Clear console, restart a service, follow journal, and grep regex

clear && clear && systemctl restart my_service && journalctl -f | egrep '(something)'

Unzip into a specific folder

unzip my_file.zip -d my_folder

Find string in files

grep -r "string to find" /home/dir/*.ext

Find a file

find /home/user/ -name file_name.ext

MongoDB - Connection string for replica set

mongodb://db1.server.com:port,db2.server.com:port,arbiter.server.com:port/?replicaSet=MyReplicaSetName&authSource=MyDBName

MongoDB - updateOne() example

db.collection_name.updateOne(
    {
        _id: ObjectId("UUIDOFTHEOBJECT")
    } , {
        $set: {
            field1: {
                field2: "new value",
                field3: 3,
                field4: {
                    field5: false
                }
            }
        }
    }
);

Node.JS - Replace all ocurrences of a string

"hello.user.test".split(".").join(" ");

Certbot - Generate SSL/TLS certificates for NGINX

certbot --nginx -d example.com -d example.net

Certbot - Renew SSL/TLS certificates for NGINX

(TO DO: info here: https://certbot.eff.org/docs/using.html)
certbot renew
certbot --force-renew

Node.JS - List object keys

console.log(Object.keys(myJsonObj));

See file sizes inside a given folder

du /home/* -sh

See size of current folder

du -sch

See size of file system

df -h

Docker - Follow container logs

docker logs -f --since 1m containerNameOrID

Docker - Go inside the container

docker exec -it containerNameOrID /bin/bash

Docker - See images in local PC

docker images

Docker - See runnning containers

docker ps

Docker - Stop a container

docker stop containerNameOrID

Docker - Build an image

docker build . -t myImageName

Docker - Delete an image by force

docker image rm --force myImageNameOrID

Docker - Inspect the layers of an image

docker history --human --format "{{.CreatedBy}}: {{.Size}}" myImageNameOrID

Docker - Run an image in a container in detached mode

docker run -p LocalPort:InternalPort -d myImageNameOrID

Modify user password

passwd userName