Skip to content

Instantly share code, notes, and snippets.

@101t
Last active September 19, 2023 03:56
Show Gist options
  • Save 101t/5032abaa66521000a2b6 to your computer and use it in GitHub Desktop.
Save 101t/5032abaa66521000a2b6 to your computer and use it in GitHub Desktop.
Frequently useful commands in Linux

Cleaning *.pyc files in terminal

find . -name "*.pyc" -exec rm -rf {} \;

or use this:

find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf

source: link

Search a file/folder if exist in directory:

find ~/ -type d -name "directoryname"
find ~/ -type f -name "filename"

Install multi-line apt-get from file and escape comments by #

apt-get install $(grep -vE "^\s*#" filename  | tr "\n" " ")

replace filename with your filedirectory.

grep -rnw '/home/to/specific/path/' -e 'pattern'

find specific "Word" under the path director in files

grep -Ril "MyWord" /
  • i stands for ignore case (optional in your case).
  • R stands for recursive.
  • l stands for "show the file name, not the result itself".
  • / stands for starting at the root of your machine.

source : (here)[https://stackoverflow.com/a/16956844/1756032]

Find number of lines of your code you have wrote in project

find . -name '*.php' | xargs wc -l
find . -name '*.py'  | xargs wc -l
find . -name '*.js'  | xargs wc -l
find . -name '*.html'| xargs wc -l
find . -name '*.css' | xargs wc -l

compress and un-compress as *tar.gz format

  1. compression
tar -czvf name-of-archive.tar.gz /path/to/directory-or-file --exclude=.git/ --exclude=.github/ --exclude=*.mp4
  1. compression without .git version (This excludes all version control system directories) --exclude-vcs
tar -czvf archive.tar.gz --exclude-vcs --exclude='*.zip' /home/username/mycode
  1. decompression
tar -xzvf archive.tar.gz -C /tmp

Run command on Terminal with output redirect to /dev/null

yourcommand > /dev/null 2>&1

to run in the background add an &

yourcommand > /dev/null 2>&1 &

>/dev/null 2>&1 means redirect stdout to /dev/null AND stderr to the place where stdout points at that time

If you want stderr to occur on console and only stdout going to /dev/null you can use:

yourcommand 2>&1 > /dev/null

In this case stderr is redirected to stdout (e.g. your console) and afterwards the original stdout is redirected to /dev/null

If the program should not terminate you can use:

nohup yourcommand &

Without any parameter all output lands in nohup.out

Running source .bashrc after ssh login so painfull

.bashrc is not sourced when you log in using SSH. You need to source it in your .bash_profile (for debian .profile) like this:

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

source: here

Inline SFTP

echo "put /tmp/test.txt /tmp/" | sftp user@example.com

source: here

Scan your local network

nmap -sP 192.168.1.0/24

to get ssh version

timeout 1 cat </dev/tcp/192.168.1.10/22

Kill a process on port in linux

sudo kill -9 `sudo lsof -t -i:8000`

you can use use $() for command interpolation

sudo kill -9 $(sudo lsof -t -i:8000)

source: here

Make your directory readonly (cannot be writable)

# find everything in /var/www/html and set to read-only #
find /var/www/html -iname "*" -print0 | xargs -I {} -0 chattr +i {}

to allow again read/write on directory

find /var/www/html -iname "*" -print0 | xargs -I {} -0 chattr -i {}

Print Environment Variables

using printenv command

printenv

source: here

to print quiet pretty sorted

env -0 | sort -z | tr '\0' '\n'
# or
env | sort -f

source: here

Change swap size in Linux

In a nutshell swap is a piece of storage used from harddisk which can be used as additional RAM, to change the swap size:

  1. Turn off all running swap processes: swapoff -a
  2. Resize swap fallocate -l 1G /swapfile to change to 1 GB size
  3. CHMOD swap chmod 600 /swapfile
  4. Make file useable as mkswap /swapfile
  5. Active the swap file swapon /swapfile

Some commands may take some time to be executed, just wait patiently for the commands to finish.

to verify swap size run the following command free -m

source: here

Redis CLI

to get all keys

redis-cli> KEYS *

to delete redis related patterns

redis-cli --scan --pattern 'Product:*:*' | xargs redis-cli DEL

source: here

IPTABLES

to disable all rules on iptables

iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

source: here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment