-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.txt
489 lines (440 loc) · 35.4 KB
/
commands.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
*********************************
*** Useful Linux Commands ***
*********************************
$ cat <FILENAME> | less (muestra por pagina y permite búsqueda como vim)
$ tar cvf <FILE.TAR> dirname/ (create new .tar archive)
$ tar xvf <FILE.TAR> (extract a .tar archive)
$ tar tvf <FILE.TAR> (view a .tar archive)
$ grep -I “the” <file> (search string in file)
$ grep -r “string” * (search string recursively)
$ grep -r -i "some string" /home/yourusername (search string in all files within directory)
$ grep -rnw '/poner_ruta_aca' -e 'drop database' (this is a best option to search files within directories)
$ find -iname “file.c” (find files using filenames)
$ find -iname “my file.c” -exec md5sum {} \; (find and execute files)
$ split -l 500 <filename> (split file into smaller files with 200 lines each)
$ split -b 200MB <big_filename> (split a large file into 200MB files)
$ cat * > merged-file (merge all files into one single file)
$ gzip [file] (create a .gzip file)
$ gzip -d [file] (decompress a .gzip file)
$ zip -r <file.zip> </folder> (compress a folder)
$ unzip <file.zip> (decompress a zipped folder)
$ locate FILENAME.EXT (locate files in hard drive)
$ grep -r "something" . (search for string "something" in hard drive)
$ grep -Ril "something" /path/to (search for string "something" in a specific path - best option)
$ find /pathdir -type d -exec chmod 755 {} + (change permissions in all directories except files)
$ find /pathdir -type f -exec chmod 644 {} + (change permissions in all files except directories)
$ find <Path_To_Old_Files> -mtime +30 -delete; (delete files older than 30 days)
$ echo "d *" | mail --user=www-data -N (delete all emails in /var/mail)
$ ls -1 . | wc -l (count how many files in the current directory)
$ wc -l /file (count number of lines of a file)
$ ec -w /file (count number of words of a file)
$ head -n 20 /file (display first 20 lines of a file)
$ tail -n 20 /file (display last 20 lines of a file)
$ shuf -n N input > output (use shuf with the -n option as shown below, to get N random lines)
$ sort -R input | head -n 100 > output (sort the file randomly and pick first 100 lines)
$ cat -n /file | more (display pagination and number of lines)
$ ls -ad .* (display only hidden files and folders)
$ cloc /dir (count number of lines of code within directory)
$ du -h /folder (display folder size)
$ du -a / | sort -n -r | head -n 10 (display 10 most large files and folders)
$ du -hsx * | sort -rh | head -10 (display 10 most large files and folders (another approach))
$ du -a --exclude="/proc" --time / | sort -n -r | head -n 20 (display 10 most large files and folders with time)
$ apache2ctl -M (list all installed modules in apache web server)
$ sudo a2enmod <nom_module> (enable apache web server module)
$ sudo a2dismod <nom_module> (disable apache web server module)
$ openssl enc -aes-256-cbc -salt -in file.txt -out file.txt.enc (Encrypt a single file with OpenSSL)
$ openssl enc -aes-256-cbc -d -in file.txt.enc -out file.txt (Decrypt a single file with OpenSSL)
$ ssh -l user remote.host (create an ssh connection)
$ ssh -v -l user remote.host (debug ssh client)
$ shutdown -Fr now (force to check filesystem)
$ ps -ef | more (display current running processes)
$ kill -9 [PID] (kill a process)
$ kill -9 `lsof -t -u someuser` (kill all processes for specific user)
$ tail -f [FILE] (display real time changes in a file)
$ less [FILE] (display file content)
$ whatis <command> (get help of a specific command)
$ cat /etc/issue (information about the os)
$ cat /etc/*-release (more detailes information about the os)
$ ifconfig eth0 10.10.10.10
$ ifconfig eth0 up | down
$ ifconfig eth0 arp | -arp (enable or disable arp table)
$ arp -a (arp table)
$ route -n
$ service [daemon] start|stop (start or stop a daemon)
$ chkconfig (enable o disable system services)
$ htop (display memory and processes information)
$ free -h / watch free -h (display memory information - real time)
$ sudo sh -c “sync; echo 3 > /proc/sys/vm/drop_caches” (clear the cache on linux)
$ watch free -h o H (display changes in memory - real time)
$ expr 1 + 2 \* 8 / 3 (perform aritmetic calculations)
$ calc 15 / 4 (with decimals)
# netstat -a -p -n | grep <port-number> (display services using a specific port)
$ scp [file] user@host:/etc/some (secure file copy)
$ scp -i file.pem /source user@host:~/destination/ (secure copy from local to remote using a key)
$ scp -i file.pem user@host:~/source /local-dest (secure copy from remote to local using a key)
$ curl -Is http://www.domain.com (check if site is alive)
$ service --status-all (display service status, "(+)Service running, (-)Not Running, (?)Unknown)
$ df -h (display disk space usage)
$ du -h (display disk space usage)
$ find / -xdev -type f -size +10M -exec ls -lah {} \; | sort -nk 5 (display files that are bigger than 10megs.)
$ source .bashrc (apply changes to .bashrc without closing the terminal)
$ exec bash (apply changes to .bashrc without closing the terminal)
$ echo $SHELL (show current shell)
$ pushd /etc (jump to the /etc folder)
$ popd (return to the /etc folder)
$ <command or process>& (send command or process to the background)
$ CTRL + Z (pause a process then type "bg" to send it to the background>
$ jobs (display running processes in background)
$ fg <1> (bring a process from the background)
$ ssh-add <key.pem> (add key to localhost)
$ netstat -nt | grep :443 | wc -l (check apache concurrences on port 443)
$ netstat -ant | grep :443 (display connected ip addresses)
$ ab -c 1000 -n 50000 -k https://www.domain.com/ (run apache benchmarks to stress server)
$ autocannon -s 200 -d 20 http://www.domain.com/ (run benchmark on domain using "autocannon")
$ npm i autocannon -g (install "autocannon" globally using npm)
# lsof (list open files)
# lsof -i tcp:80 (display running services in port 80)
# lsof -i :8080
# lsof -i tcp|udp
# lsof /var/log/some.log (display info of a file and the user using it)
# lsof -p PID (display all used files for a specific process)
# lsof -p PID | grep bin (display binary directory for a specific process)
# lsof -p PID | grep lib (same for libs)
# lsof -p PID | grep log (same for logs)
# lsof -u USERNAME (display all open files for this user)
# apt-cache search linux-image (search for kernels)
# sudo apt install linux-image-<flavour> (install kernel)
# dpkg --list | grep linux-image (list installed kernels)
$ sudo apt remove --purge <linux-image-x.x.x> (remove installed kernel)
$ sudo update-grub2 (updated GRUB loader with installed kernels)
# dpkg-reconfigure tzdata (reconfigure timezone settings in Debian OS)
# vcgencmd measure_volts core (measure voltage RaspberryPi)
# vcgencmd measure_temp (measure temperature RaspberryPi)
$ timedatectl (display current timezone)
$ timedatectl list-timezones (list available timezones)
$ sudo timedatectl set-timezone your_time_zone (set timezone)
$ certbot renew --force-renewal (force to renew all letsencrypt ssl certs)
$ ffmpeg -i file.m4a -acodec libmp3lame -ab 256k output.mp3 (convert "m4a" audio file to "mp3")
$ nohup ./my_script.sh & (run script in background and leave running even when disconnected)
******************
*** Python ***
******************
$ python3 -m venv .venv (to create virtual environment)
$ source .venv/bin/activate (to activate virtual environment)
********************
*** Mongo DB ***
********************
$ mongodump -d <database_name> -o <directory_backup> (dump a database into folder)
$ mongorestore -d <database_name> <directory_backup> (restore a mongo dump file)
*********************
*** Using VIM ***
*********************
:wr (write file)
:q (quit vim)
:q! (quit without saving)
:se nu (show line numbers)
:/[string] (search forward) // “N” to jump to next result
:?[string] (search backward) // “N” to jump to next result
“u” (undo (without quotes)
"ctrl+r" (redo (without quotes)
“yy” (yank a line / copy a line to clipboard)
“66yy” (yank 66 lines of code)
“p” (paste content on clipboard)
“dd” (delete a line)
“58G” (jump to line 58)
“!” (execute a shell command)
"V" (selects entire lines)
"v" (selects range of text)
"ctrl-v" (selects columns)
"gv" (reselect block)
:split [file] (open a new file if exists or create one (split visual view))
vim -o [file1] [file2] (open two or more files)
“ctrl+w” (move between opened files)
“$” (move to the end of the line)
“0” (move to the beginning of the line)
:%s/[someting]/[replacement]/g (Search and replace)
:%s/^[a]/!/ (replace 1st char(a) with !)
500+dd (delete 500 lines)
$ vim +143 [file] (go to line 143)
$ vim +/search-term [file] (go to the first match)
:e filename (edit another file)
ctrl-w up arrow (move cursor up a window)
ctrl-w ctrl-w (move cursor to another window (cycle))
ctrl-w_ (maximize current window)
ctrl-w= (make all equal size)
10 ctrl-w+ (increase window size by 10 lines)
:vsplit file (vertical split)
:sview file (same as split, but readonly)
:hide (close current window)
:only (keep only this window open)
:ls (show current buffers)
:b 2 (open buffer #2 in this window)
:set tabstop=8 (tabs are at proper location)
:set expandtab (don't use actual tab character (ctrl-v)
:set shiftwidth=4 (indenting is 4 spaces)
:set autoindent (turns it on)
:set smartindent (does the right thing (mostly) in programs)
:set cindent (stricter rules for C programs)
---Browsing the file (navigation)
e = end of a word
w = beginning of a word
$ = end of a line
0 = beginning of a line
G = end of a file
gg = start of a file
---Visual mode = SHIFT+v
*********************
*** Using APT ***
*********************
# apt-get update
# apt-get upgrade
# apt-cache search <package>
# apt-cache show <package>
# apt-get dist-upgrade
# apt-get install <package>
# apt-get remove <package>
# apt-get remove --purge <package>
# apt-get clean
# apt-get autoclean
# apt list --installed
# apt autoremove
*********************
*** Using SED ***
*********************
$ sed 's/value1/value2/' file (replace 'value1' for 'value2' once, don't write the file)
$ sed 's/value1/value2/g' file (replace 'value1' for 'value2' globally, don't write the file)
$ sed 's/value1/value2/g' file1 > file2 (does the same thing and write output into 'file2')
$ sed -i 's/value1/value2/g' file1 (replace and write file)
$ sed -i 's/ˆvalue1/value2/g' file1 (replace 'value1' at the beginning of the line by char: ˆ)
$ sed -i 's/value1$/value2/g' file1 (replace 'value2' at the end of the line by char: $)
$ sed 's/[0-9]/value2/g' file1 (replace any number with 'value2')
$ sed 's/[A-Z]/value2/g' file1 (replace any uppercase alphanumeric with 'value2')
$ sed 's/[A-z]/value2/g' file1 (does the same with uppercase and lowercase)
$ sed 's/[A-z0-9]/value2/g' file1 (replace numbers and letters with 'value2')
$ sed 's/[A-Z]i/value2/g' file1 (replace uppercase followed by 'i' with 'value2')
$ sed 's/[0-9]/(&)/g' file1 (mark between () the matching of first value)
$ sed 's/[0-9][0-9]/(&)/g' file1 (mark between () two contiguos numbers in 'value1')
$ sed 's/\//value2/g' file1 (replace with escape character)
$ sed 's/[^0-9]/value2/g' file1 (replace non-numbers with 'value2', ^ inside [])
$ sed 's/[^A-Z]/value2/g' file1 (replace non uppercase alphanumeric with 'value2')
$ sed 's/[0-z ]/value2/g' file1 (replace all alphanumeric and space with 'value2')
$ sed 's/some1/some2/g;s/some3/some4/g' file1 (replace two values with the same command)
$ sed 's/some1/some2/Ig' file1 (replace 'some1' with 'some2' and ignore casing)
$ sed -i -f myrules file1 (execute 'myrules' with all the replacements, this is done over 'file1')
$ sed 's/\bNEW\b/some2/Ig' file1 (find the word 'NEW' and replace, doesn't matter the case)
$ sed '/\bNEW\b/Id' file1 (delete all lines containing the word 'new', doen't matter the case)
$ sed '1!{/^#/d;}' file1 (take out all comments and commented lines except the fist line)
*********************
*** Using AWK ***
*********************
$ awk '{ print }' file1 (print all lines of a file)
$ awk '{ print $1 }' file1 (print the first column)
$ awk '{ print $1,$2 }' file1 (print the first and second column)
$ awk '{ print $1.$2 }' file1 (print the first and second column merged)
$ awk '/[a-z]/ { print }' file1 (print alphanumeric only)
$ awk '/[0-9]/ { print }' file1 (print numeric only)
$ awk '/^[0-9]/ { print }' file1 (print all that begins with numbers)
$ awk '/[0-9]$/ { print }' file1 (print all that ends with numbers)
$ awk '{ if($1 ~ /123/) print }' file1 (if column 1 is equal to 123, then print)
$ awk '{ if($2 ~ /[0-9]/) print }' file1 (if column 2 is equal to numbers, then print)
************************
*** GIT Commands ***
************************
$ apt-get install git (install git on a Debian based system)
$ apt-get install git-core (install git server)
$ git init (initialize the current located folder as a git repo)
$ git config --list (display current configurations)
$ git config --global user.name "User Name" (set the user name)
$ git config --global user.email "[email protected]" (set the user email)
$ git status (display the status of the repo, locally)
$ git show <file> (display changes on file)
$ git add *.algo o <file> (send a file or bunch of files to the 'staging area')
$ git commit -m "message" (make a commit with comments)
$ git --version (display git version)
$ git help <command> (display git help or man page)
$ git diff (display changes in files)
$ git checkout <file> (bring the last commit of the specific file)
$ git blame <file> (provides information about changes and who made those changes)
$ git log (list commits in this branch)
$ git log -P (display the history of the repo with details)
$ git log /folder (list logs in this folder)
$ git log <my-branch-name> (list logs in a specific branch)
$ git log --since=8am (list logs since 8am)
$ git log --author=my-name (list logs for specific user)
$ git log --grep=“something” (list and search string in logs)
$ git log --decorate --graph --oneline --all (display detailed logs)
$ git log --shortlog (display short logs)
$ git branch (see local branches)
$ git branch -r (see remote branches)
$ git branch -a (see local and remote branches)
$ git checkout -b <my-branch-name> (create a new branch)
$ git checkout <my-branch-name> (switch to branch locally)
$ git merge <my-branch-name> (merge to master, before running this command switch to master)
$ git push origin --delete <my-branch-name> (delete remote branch)
$ git branch -D <my-branch-name> (delete local branch)
$ git mergetool (tool to solve conflicts)
$ git config --global merge.tool vimdiff (select "vimdiff" as the default tool to solve conflicts)
$ git config --global alias.SOMETHING "command" (create an alias of the specified command with the name 'SOMETHING')
$ git clone https://repo.git (clone a remote repo locally)
$ git fetch --all (bring all remote branches)
$ git pull, push
$ git stash
$ git stash list
$ git stash show
$ git stash apply
$ git tag
************************
*** PM2 Commands ***
************************
$ npm install pm2@latest -g (install PM2 globally)
$ pm2 start <app.js> (start app.js)
$ pm2 start <app.js> -i max (start with cluster mode using all proccessors)
$ pm2 start app.js --name APP_NAME --watch -i max --merge-logs --time --output logs/app.log --error logs/app.log
(start with cluster, logs, time on logs and watch mode)
$ pm2 list (list all running proccesses)
$ pm2 restart <app_name> (restart process using app name)
$ pm2 reload <app_name> (reload process using app name)
$ pm2 stop <app_name> (stop process using app name)
$ pm2 delete <app_name> (delete registered process using app name)
$ pm2 logs (show logs)
$ pm2 logs --lines 200 (show 200 lines of logs)
$ pm2 monit (start monitor)
$ pm2 save (save a snapshot of your processes)
$ pm2 startup (start saved proccesses when linux reboots (run pm2 save first))
$ pm2 resurrect (To manually bring back previously saved processes (via pm2 save))
$ pm2 unstartup (To disable and remove the current startup configuration)
************************
*** Using Screen ***
************************
$ apt-get install screen (install 'screen' multiplexer)
$ screen (start 'screen' with a bash shell)
$ CTRL+A y "?" (display commands and shortcuts)
$ CTRL+A y "d" (detach console with a process running)
$ CTRL+A y "n" (move to the next console)
$ CTRL+A y "p" (move to the previous console)
$ CTRL+A y "c" (create a new console)
$ CTRL+A y "x" (block 'screen' with password)
$ screen -ls (list all running consoles)
$ screen -r (list session id if there is one)
$ screen -r <NUMBER> (show specific session)
**********************
*** Using TMUX ***
**********************
$ tmux (start tmux)
$ tmux ls (list all sessions)
$ CTRL + b c (create a new window)
$ CTRL + b , (rename current window)
$ CTRL + b & (kill current window)
$ CTRL + b p (move to the previous window)
$ CTRL + b n (move to the next window)
$ CTRL + b % (split pane vertically)
$ CTRL + b " (split pane horizontally)
$ CTRL + b (arrow up) (switch to pane to the direction)
$ CTRL + b (arrow down) (switch to pane to the direction)
$ CTRL + b (arrow left) (switch to pane to the direction)
$ CTRL + b (arrow right) (switch to pane to the direction)
$ exit (exit tmux)
**************************************
*** Node Version Manager (nvm) ***
**************************************
$ nvm ls-remote (list all Node versions)
$ nvm install 17.4.0 (install specific Node version)
$ nvm use 17.4.0 (use specific Node version as default)
******************
*** Docker ***
******************
$ docker version (see the version of client and server)
$ docker build -t hello-docker . (Build an image following “Dockerfile”)
$ docker images (list all images on computer)
$ docker image ls (list all images on computer)
$ docker pull <image-name> (pull an image from Docker Hub)
$ docker ps (list running containers)
$ docker ps -a (list all containers, including stopped)
$ docker run <image-name> (run or install docker image from any location)
$ docker run <image:version> (run or install docker image with specific version)
$ docker run -it <image-name (ej: ubuntu)> (run in interactive mode)
$ docker run -d <image-name> (run a container in a detached mode)
$ docker run -p<localhost-port>:<image-port> <image-name> (port binding)
$ docker run —name <myapp> <image-name> (create a custom name for container)
$ docker stop <container_ID> (stop a specific container by ID)
$ docker start <coniatner_ID> (start a specific container by ID)
$ docker logs <container_ID or name> (see the logs)
$ docker network ls (list all generated networks)
$ docker network create <network-name> (create a new network)
$ docker-compose -f <file.yaml> up (deploy all container in yaml file)
$ docker-compose -f <file.yaml> down (destroy all deployed containers via Docker Compose)
$ docker rmi <image_name or container ID> (delete an image or container)
$ docker exec -it <container_ID> /bin/sh (enter a container that’s running)
$ docker system prune -a (removes cache, images, stopped images, etc -- use with caution --)
*******************
*** AWS CLI ***
*******************
Command Reference: https://docs.aws.amazon.com/cli/latest/index.html
$ aws configure (configure AWS CLI)
$ aws iam list-users (list all IAM users on this account)
$ aws s3 ls (list all S3 buckets)
$ aws s3 ls s3://<bucket_name> --recursive (list all files and folders within <bucket_name>
********************
*** Anaconda ***
********************
$ conda env list (list all created environments in this computer)
$ conda create --name <my-env-name> (create a new environment with current default Python version)
$ conda create -n <my-env-name> python=3.7 (create a new environment with Python 3.7)
$ conda activate <my-env-name> (activate or switch to my environment)
$ conda deactivate <my-env-name> (deactivate the current environment)
$ conda list (list all installed packages in the current environment)
$ conda install <package> (install a package within the current environment)
$ conda env export > environment.yml (export my current environment to be shared with others)
---
# Functions to encrypt a single files with custom password
function encrypt(){
which openssl &> /dev/null
[ $? -ne 0 ] && echo "OPENSSL is not installed." && return 1
# openssl enc -aes-256-cbc -salt -in $1 -out $2
openssl enc -aes-256-cbc -salt -pbkdf2 -in $1 -out $2
}
# Functions to decrypt a single files with custom password
function decrypt(){
which openssl &> /dev/null
[ $? -ne 0 ] && echo "OPENSSL is not installed." && return 1
# openssl enc -aes-256-cbc -d -in $1 -out $2
openssl enc -aes-256-cbc -d -pbkdf2 -in $1 -out $2
}
# Function to encrypt files in a folder
encrypt_folder() {
# Check if openssl is installed
which openssl &> /dev/null
[ $? -ne 0 ] && echo "OPENSSL is not installed." && return 1
# Specify the input folder and output folder
input_folder="$1"
output_folder="$2"
# Create the output folder if it doesn't exist
mkdir -p "$output_folder"
# Encrypt each .txt file in the input folder
for file in "$input_folder"/*.txt; do
filename=$(basename "$file")
encrypted_file="$output_folder/${filename%.*}_encrypted.txt"
openssl enc -aes-256-cbc -salt -pbkdf2 -in "$file" -out "$encrypted_file"
done
echo "Encryption completed."
}
# Function to decrypt files in a folder
decrypt_folder() {
# Check if openssl is installed
which openssl &> /dev/null
[ $? -ne 0 ] && echo "OPENSSL is not installed." && return 1
# Specify the input folder and output folder
input_folder="$1"
output_folder="$2"
# Create the output folder if it doesn't exist
mkdir -p "$output_folder"
# Decrypt each encrypted file in the input folder
for file in "$input_folder"/*.txt; do
filename=$(basename "$file")
decrypted_file="$output_folder/${filename%.*}_decrypted.txt"
openssl enc -aes-256-cbc -d -pbkdf2 -in "$file" -out "$decrypted_file"
done
echo "Decryption completed."
}
# Fetch this file from console using aliases
alias <alias_name>='curl https://raw.githubusercontent.com/martingaido/BASHScripts/master/commands.txt'