-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.txt
More file actions
730 lines (579 loc) · 19.9 KB
/
commands.txt
File metadata and controls
730 lines (579 loc) · 19.9 KB
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# vim: set filetype=sh:
##########################################
### Packages Management (Debian-based) ###
##########################################
# List installed packages by size, in descending order
dpkg-query -W -f='${Installed-Size;8} ${Package}\n' | sort -nr
########################################
### Packages Management (Arch-based) ###
########################################
# List all explicitly installed packages that are not dependencies
pacman -Qet
# List installed packages by size, in descending order
pacman -Qi | awk '/^Name/{name=$3} /^Installed Size/{print $4$5, name}' | sort -hr
yay -Ps
# List installed packages by installation/last update time, the most recent
# first
expac --timefmt='%Y-%m-%d %T' '%l\t%n' | sort -r
# Remove orphaned dependencies
sudo pacman -Rs $(pacman -Qqdt)
# Display detailed information on a upgradable packages
pacman -Qii $(checkupdates+aur | cut -d' ' -f1)
# Move all the cached packages to different directory
sudo rsync -aHAXP --remove-source-files /var/cache/pacman/pkg/ dir/
paccache -vvv -k 0 -m dir/
# Remove old cached packages, keeping 3 last versions of specific package
paccache -vvv -r
paccache -vvv -r -c cache_dir
####################################
### Packages Management (Python) ###
####################################
# Upgrade pip
pip install --upgrade pip
# List all installed packages without versions
pip list --format=columns 2>/dev/null | cut -s -d ' ' -f 1 | tail -n +3
# Install packages listed in the file
pip install -r requirements.txt
###################################
### Packages Management (macOS) ###
###################################
# Upgrade packages
brew update
brew upgrade
# List outdated packages
brew outdated
# Show info about the package
brew info <package>
# Show all formulaes installed on request
brew info --json=v2 --installed | jq -r '.formulae[]|select(any(.installed[]; .installed_on_request)).full_name'
# Show all casks installed
brew list --cask -1
# Create (and overwrite if it already exists) `~/.Brewfile`, a file containing
# list of explicitly installed packages, e.g. to version control each
# installed/uninstalled package
brew bundle dump --describe --global --no-upgrade --force
#############
### C/C++ ###
#############
# Link specific library
gcc <...> -l:libboost_system.so.1.66.0
# Start without introductory and copyright messages
gdb -q
# Pass arguments to debugged executable
gdb --args <executable_name> <arguments>
# Set non-default C++ compiler (from the command line)
qmake 'QMAKE_CXX=colorgcc' ..
# Link additional libraries (from the command line)
qmake 'LIBS += -l:libboost_system.so.1.66.0 -l:libboost_program_options.so.1.66.0' ..
##################
### Networking ###
##################
# Print all IPv4 HTTP packets containing data to and from port 80 and host
# <ip_address>, i.e. omit SYN, FIN, ACK-only and similar kind of packets
tcpdump 'tcp port 80 and host <ip_address> and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
# Print each packet in ASCII (e.g. content of HTTP requests/responses)
tcpdump -A
# Don't convert addresses to names
tcpdump -n
# Show both listening and non-listening sockets
netstat -a
# Show numerical addresses
netstat -n
# Show connections using TCP protocol
netstat -t
# Show connections using UDP protocol
netstat -u
# Display the kernel routing tables
netstat -r
# Show the PID and name of the program to which each socket belongs
netstat -p
###################
### Job Control ###
###################
# List background jobs
jobs
# Run cmd in background
<cmd> &
# Move current cmd to the background
<Ctrl+Z>
# Move the most recent job to the foreground
fg
# Move job with specific id to the foreground
fg <id>
# Kill the most recent job
kill %%
# Kill job with specific id
kill <id>
# Run <cmd> in the background and do _not_ kill it when exiting terminal (it
# will save STDOUT and STDERR to the file `nohup.out` in **the current working
# directory**)
nohup <cmd> &
##################
### GNU Screen ###
##################
# Create a named session
screen -S <session_name>
# Attach to a named session
screen -x <session_name>
# or
screen -r <session_name>
# Detach from the current session
CTRL-a, d
###########
### SQL ###
###########
# Connect to chosen MSSQL server
sqlcmd -S <server_name> -d <database_name> -U <username> -P <password>
# Connect to chosen PostgreSQL database
psql -h <server_name> -d <database_name> -U <username> -p <port>
# Connect to running MySQL docker-compose container
docker-compose run --rm <service_name> mysql -h <service_name> -u root -D ${MYSQL_DATABASE} -p ${MYSQL_ROOT_PASSWORD}
# Connect to running MySQL docker-compose container (prompt for password)
docker-compose run --rm <service_name> mysql -h <service_name> -u root -D ${MYSQL_DATABASE} -p
# Restore MySQL data from database dump
docker-compose run --rm <service_name> sh -c "exec mysql -h <service_name> -u root -D ${MYSQL_DATABASE} -p ${MYSQL_ROOT_PASSWORD}" < dump.sql
# Check PostgreSQL table's definition
\d <schema_name>.<table_name>
#######################
### Storage Devices ###
#######################
# Mount device
sudo mount /dev/sdc1 /mnt
# or
udisksctl mount -b /dev/sdc1
# Unmount device
sudo umount /mnt
# or
sudo umount /dev/sdc1
# or (https://unix.stackexchange.com/a/178648)
udisksctl unmount -b /dev/sdc1
udisksctl power-off -b /dev/sdc
# Format the USB stick (https://askubuntu.com/a/223607/737231)
umount /dev/sdc1
sudo dd if=/dev/zero of=/dev/sdc bs=1M status=progress
sudo fdisk /dev/sdc
Command: n
Select: p
Partition number: 1
First sector: <return>
Last sector, +sectors or +size{K,M,G}: <return>
Command: t
Hex code: 6
Command: w
sudo mkfs.vfat /dev/sdc1 -n LABEL -v
# Check if device is a LUKS device
cryptsetup isLuks <device>
# Open the LUKS device and map it
cryptsetup open --type luks <device> <mapping_name>
# or
cryptsetup luksOpen <device> <mapping_name>
# Close the LUKS device
cryptsetup close <mapping_name>
# Create swap file (alternative to swap partition)
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap defaults 0 0' >>/etc/fstab
###########
### SSH ###
###########
# Generate an SSH key pair (linux)
ssh-keygen -C "$(whoami)@$(hostname)-$(date -Iseconds)"
# Generate an SSH key pair (macOS)
ssh-keygen -C "$(whoami)@$(hostname -s)-$(gdate -Iseconds)"
# Change the private key's passphrase
ssh-keygen -f <private_key> -p
# Change the keys' comment
ssh-keygen -f <private_key> -c -C <new_comment>
# Copy the public key to the remote server
ssh-copy-id -i <public_key> <destination>
# Connect with public key authentication explicitly disabled
ssh -o pubkeyauthentication=no <...>
# Add an SSH key to the SSH agent
eval $(ssh-agent) && ssh-add ~/.ssh/<private_key>
####################
### File-copying ###
####################
# Copy 'src' to 'dest/src'
rsync -r 'src' 'dest'
# Copy content of 'src' to 'dest'
rsync -r 'src/' 'dest'
# Archive 'src' to 'dest/src'
rsync -a 'src' 'dest'
# Preserve hard links, ACLs and extended attributes
rsync -HAX 'src' 'dest'
# Keep partially transferred file(s), and show progress
rsync -P 'src' 'dest'
# All in one
rsync -aHAXP 'src' 'dest'
###############################
### Version Control Systems ###
###############################
# Change last commit message
git commit --amend
# Undo a commit and reuse the log message and the authorship information in the
# next commit
git commit
git reset HEAD~
git add ...
git commit -c ORIG_HEAD
# Change URL(s) for the origin remote
git remote set-url origin <new_url>
svn relocate <new_url>
# version < 1.7
svn switch --relocate <old_url> <new_url>
# Get hash of the last commit
git rev-parse HEAD
# Delete a branch both locally and remotely
git push -d origin <branch_name>
git branch -d <branch_name>
# Partial revert
git revert --no-commit <SHA>
git restore --staged --worktree ... # exclude changes you do NOT want to restore
git commit
# Partial cherry-pick
git cherry-pick --no-commit <SHA>
git reset --mixed
git add -p
git commit -c ORIG_HEAD
# Completely replace one branch's code and history with another
git checkout target
git checkout -m old-target
git checkout source
git branch -m target
git push origin target -f
git branch -m source
git checkout old-target
git branch -m target
git fetch --all
git reset --hard origin/target
# Checkout a branch on non-default remote
git fetch <remote>
git checkout -b <branch_name> --track <remote>/<branch_name>
# Reuse the message of the most recent failed commit
git commit -eF .git/COMMIT_EDITMSG
# See diff between yesterday (24 hours ago) and now
git diff @{yesterday}
# Find the most recent commit on the current branch whose message contains
# `foo`
git show :/foo
# Find the most recent commits on the current branch between the one mentioning
# `introduce`, and the one mentioning `remove`
git log :/introduce..:/remove
# Print number of added and deleted lines per pathname
git diff --numstat
# Print total number of added and deleted lines
git diff --shortstat
##############
### Docker ###
##############
# Remove all:
# - stopped containers,
# - networks not used by at least one container,
# - dangling images,
# - dangling build cache,
docker system prune
# Remove all:
# - stopped containers
# - networks not used by at least one container
# - volumes not used by at least one container
# - images without at least one container associated to them
# - build cache
docker system prune --all --volumes
# Show full output of _all_ the build steps; the default `tty` hides a lot of them
docker-compose build --progress plain <service>
# Do not use cache when building the image
docker-compose build --no-cache <service>
# Build a docker image and name it
docker build --progress plain -t <name>
# Run the docker image's default entrypoint
docker run <name>
# Run interactive shell in the docker image
docker run --rm -it <name> bash
###############
### Printer ###
###############
# Print file
lpr <filename>
# Print only the odd pages of file
lpr -o page-set=odd <filename>
# Print only the even pages of file
lpr -o page-set=even <filename>
# Show printer queue status
lpq
# Cancel print jobs
lprm
###############
### Scanner ###
###############
# Scan image with the sane options (--resolution is flag specific to the
# device: Hewlett-Packard HP_LaserJet_Professional_M1132_MFP all-in-one)
scanimage --progress --format=tiff --mode Gray --resolution 300 >file.tiff
#################
### Sentry.io ###
#################
# Required sentry-cli env variables
export SENTRY_AUTH_TOKEN=... # https://sentry.io/settings/account/api/auth-tokens/
export SENTRY_ORG=organization-slug
# List releases
sentry-cli releases list
# Show autogenerated release version
sentry-cli releases propose-version
# Create new release
sentry-cli releases new -p <project-slug> <version>
# Delete existing release
sentry-cli releases delete <version>
# Associate commits with the release
sentry-cli releases set-commits --commit '<owner/repo>@<full_hash>' '<version>'
# or
sentry-cli releases set-commits --commit '<owner/repo>@<from: full_hash>..<to: full_hash>' '<version>'
# or
sentry-cli releases set-commits --auto <version>
# Finalize (release) the release
sentry-cli releases finalize <version>
############################
### Encryption & Signing ###
############################
# Encrypt file symmetrically (creates <file>.gpg and do _not_ removes the
# original: shred recommended)
gpg -c <file>
# With specified cipher and disabled passphrase caching
gpg -c --cipher-algo 'AES256' --no-symkey-cache a.txt
# Encrypt file asymmetrically
gpg -e --default-recipient-self <file>
# As ASCII and output to the STDOUT
gpg -e --default-recipient-self -a --output - <file>
# Decrypt file
gpg -d <file>
# Generate a new secret key
gpg --full-gen-key
# List secret keys in the keyring
gpg -K
# List public keys in the keyring
gpg -k
# Export the public key in ASCII format
gpg --export --armor <user-id> >public.key
# Import the public key
gpg --import public.key
# Change the expiration period of the key
gpg --edit-key <user-id>
> expire
# Sign a file
gpg -s <file>
# Verify the signature
gpg --verify <file>.gpg
###################
### Compressing ###
###################
# Show ZIP content
unzip -l example.zip
zip -sf example.zip
# Unzip to specific directory
unzip -d <dir> example.zip
# or
cd <dir> && unzip ../example.zip
# Grep all ZIPs in current directory
find . -maxdepth 1 -type f -name '*.zip' -exec zipgrep '<pattern>' {} \;
# Grep all ZIPs in current directory (consider only specific files inside ZIPs)
find . -maxdepth 1 -type f -name '*.zip' -exec zipgrep '<pattern>' {} <files_to_consider> \;
###########
### Etc ###
###########
# Print basename of everything found recursively in the current directory
find . -exec basename {} \;
# GNU-only
find . -printf '%f\n'
# Create tag file 'tags' for source code found recursively in the current
# directory
ctags -R -f tags .
# Set foreground color of the current tmux pane
tmux select-pane -P 'fg=<color>'
# List all GPUs and show their kernel drivers and modules
lspci -k | grep -EA3 'VGA|3D|Display'
# Show the topology of the system
lstopo
# Blank the display (auto-unblank on mouse movement or keyboard input)
xset dpms force off
# Manually unblank the display
xset dpms force on
# Custom ps columns (e.g. pid, full start time and elapsed seconds since start)
ps -o pid,lstart,etimes
# Without headers
ps -o pid=,lstart=,etimes=
# Modify every 2nd line
sed "0~2s/.*/prefix&sufix/"
# Color every 2nd line
sed "0~2s/.*/$(printf '\e[36m')&$(printf '\e[0m')/"
# Reduce volume to 50% of its original value
ffmpeg -i input.mp3 -filter:a 'volume=0.5' output.mp3
# Change metadata title and artist of macOS's Voice Memo
ffmpeg -i input.m4a -metadata title='title' -metadata artist='artist' output.m4a
# Change metadata of MP3 file (ID3v1 tags)
mp3info -a 'artist' -l 'album' -t 'title' -n 'track number' -y 'year' input.mp3
# Change metadata of MP3 file (ID3v1 or ID3v2 tags)
eyeD3 -a 'artist' -A 'album' -t 'title' -n 'track number' -y 'year' input.mp3
# Convert metadata of MP3 file from ID3v1 to ID3v2 tags
eyeD3 -1 --to-v2.4 input.mp3
# Change covert art metadata (ID3v2 tags) of MP3 file
eyeD3 --add-image cover.jpg:FRONT_COVER input.mp3
# Cut the audio/video file w/o changing the encoding
ffmpeg -i input.mp4 -ss HH:MM:SS -to HH:MM:SS -c copy -avoid_negative_ts make-zero output.mp4
# Convert MP4 to MP3
ffmpeg -i input.mp3 -vn -b:a 320K output.mp3
# Rename the MP3 file based on ID3 tags (e.g. '03 - Any Way You Wanna Love.mp3')
mv -vi input.mp3 "$(mp3info -p '%02n - %t.mp3' input.mp3)"
mv -vi input.mp3 "$(eyeD3 --plugin=display -p '$num(%n%, 2) - %t%.mp3' input.mp3)"
# Pretty-format CSV file
column -s',' -t result.csv | less -#5 -N -S
# Run SQL query on CSV file
sqlite3 :memory: \
-cmd '.mode csv' \
-cmd '.import file.csv table' \
-cmd '.mode <output_format>' \
'SELECT …'
# Print full list of possible output formats
sqlite3 -cmd '.help mode'
# Remove file while preventing data recovery (only HDD!)
shred -u <file>
# Print all lines starting from the <n>th
tail -n +<n>
# Show <n> random lines from file
shuf -n <n> <file>
# Simple copying between directories
cd <source>
ln -sv $(pwd) /tmp/easycp
cd <target>
cp -rv ./... /tmp/easycp/...
# Fix bluetooth on OSX
sudo pkill bluetoothd
# Open all files found by `fd` in the vim
fd <pattern> -X vim
# Insert line numbering (input from the STDIN)
seq 10 | nl
# Insert line numbering (input from the file)
nl <file>
# Insert line numbering as e.g. '1. ' instead of the default '1 '
nl -s'. '
# Insert line numbering w/o padding. GNU only; macOS nl truncates the number to 1 digit
nl -w 1
# Format number from/to human-readable string (e.g. '1659.41M' into '1.7G')
numfmt --from=auto --to=si <string>
# Format sizes in columns 2-4 of `df` output (e.g.
# ```
# Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on
# /dev/disk3s1s1 965595304 47774640 430265192 10% 502161 2151325960 0% /
# ```
# into
# ```
# Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on
# /dev/disk3s1s1 966M 48M 431M 10% 502161 2151325840 0% /
# ```
df | numfmt --header --field 2-4 --to=si
# (macOS-only) capture screenshot of the full screen of the main display
screencapture 'file.png'
# (macOS-only) capture screenshot of the full screen of each display
# (`screencapture` will ignore surplus filenames if there are less displays than
# filenames)
screencapture 'display1.png' 'display2.png' 'display3.png'
# (macOS-only) capture screenshot interactively (selected region or selected
# window; press space to switch between)
screencapture -i 'file.png'
# (macOS-only) capture screencast of the main display and show mouse clicks on
# the recording
screencapture -vk 'file.mp4'
# (macOS-only) capture screencast of each display (surplus filenames will be
# ignored)
screencapture -vk 'display1.mp4' 'display2.mp4' 'display3.mp4'
# (macOS-only) create an audiobook from the given text file
say -f input.txt -o output.aiff
# (macOS-only) run an internet speed test
networkQuality
# Open `file` in Vim at the last line
vim + 'file'
# Open `file` in Vim at the 42nd line
vim +42 'file'
# Open `file` in Vim at the `pattern`
vim +/pattern 'file'
# Record a terminal session
script -r
# Playback the recording with the recorded timing
script -p
# Playback the recording immediately
script -pd
# Wrap text (breaks words)
fold
# Wrap text (doesn't break words, doesn't break after the first word in the
# sentence, tries to preserve the text flow at the cost of not always breaking
# at the max line length)
fmt
# Alternative to `ls -1`
ls | paste -
# Alternative to `nl`
sed = <file> | paste - -
# Search for English words that begin with the prefix, including the prefix
# itself (e.g. 'car')
look <prefix>
# Print a file info in the format of:
# human-readable file permissions, octal file permissions, filename
stat -c '%A %a %n' <file>
# Show a calendar for the whole current year
cal -y
# Show a calendar for the previous, current, and next month
cal -3
# Show the date of the current year's Easter
ncal -e
# Show the current boot entries, boot order, boot timeout, etc
efibootmgr
# Temporarily override which boot entry will be the default for the next (and
# only the next) boot to 0002
efibootmgr -n 2
# Change the boot order to 0002,0001,0004
efibootmgr -o 2,1,4
###########
### AWS ###
###########
# Empty and remove all S3 buckets (versioned ones have to be emptied via AWS Console)
for bucket in $(aws s3 ls | cut -d ' ' -f3); do
aws s3 rm "s3://${bucket}" --recursive # Empty bucket
aws s3 rb "s3://${bucket}" # Remove bucket
done
#############################
### Environment Variables ###
#############################
# Substitute the STDIN w/ the value of environment variable, and output the result to STDOUT
# e.g. if `echo $FOO` is `42`, STDOUT will be `hello 42`
echo 'hello ${FOO}' | envsubst
# Export the environment variables defined in the `.env` file into the shell
source <(sed 's/^/export /' .env)
# Install Vundle, plugin manager for Vim
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
# Install tpm, plugin manager for tmux
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
##################
### Panic mode ###
##################
# Fix broken kernel image via bootable Arch Linux
# 1. mount root partition
$ mount /dev/sda6 /mnt
# 2. mount boot partition
$ mount /dev/sda2 /mnt/boot
# 3. chroot into root partition
$ arch-chroot /mnt
# 4. backup existing kernel images
$ mv -vi *linux* /root/
# 5. restore original mkinitcpio.conf from the backup
$ cp -vi /etc/mkinitcpio.conf{.bak,}
# 6. force rebuild of kernel images _and_ intel microcode
$ sudo pacman -Syyu linux intel-ucode
# 7. update boot loader
$ bootctl update
# 8. verify boot loader configuration
$ bootctl list
# and
$ bootctl status
# 9. shutdown (to test if it helped)
$ exit
$ systemctl poweroff
# Fix black screen after log in via lightdm (will exit all apps)
$ sudo systemctl restart display-manager