Skip to content

Latest commit

 

History

History
628 lines (463 loc) · 15.5 KB

index.md

File metadata and controls

628 lines (463 loc) · 15.5 KB

oneliners.sh

Life is too short to fumble with bash commands

Share with your pals 🤝🔗: oneliners.sh

Merge in your own slick lines 📋✍️: oneliners GitHub repo


Disk/IO

Oneliners for disks and files

find

start search from current directory (.)

Find where a file of a certain name (case insensitive) is located:

find . -type f -iname init.el

Find where a directory of a certain name (case insensitive) is located:

find . -type d -iname ".emacs.d"

Find files of multiple extentions:

find . -type f -iname "*.cpp" -o -iname "*.txt"

where -o acts as a logical OR operator

grep

requires the grep package

a powerful tool for searching plain-text

Find occurences of a phrase in all files in current directory

grep -iIn "kind: ConfigMap" *

where * is a wildcard

Find occurences of a phrase in all files in current directory and its subdirectories:

grep -iIrn "kind: ReplicaSet"

ag

requires the_silver_searcher (silversearcher-ag) package

much faster than grep, useful for searching through large directory

Search occurences of a phrase in all files in current directory and its subdirectories:

ag "kind: ReplicaSet"

accounts and groups

create an account for maryq and add it to group "sambashare":

sudo useradd -s /bin/bash -m -c "Mary Quinn" -Gsambashare maryq

change password for an account, e.g. maryq:

sudo passwd maryq

create a new group:

sudo groupadd coolpeople

add existing user to a group:

sudo usermod -a -G coolpeople maryq

list groups a user is a member of:

groups maryq

file permissions

change ownership of a file file1.txt to user root:

sudo chown root file1.txt

change group ownership of a file file1.txt to group sudo:

sudo chown :sudo file1.txt

do both of the above at once:

sudo chown root:sudo file1.txt

add execute permissions to a file:

sudo chmod ugo+x gettools.sh

remove execute permissions from a file:

sudo chmod ugo-x gettols.sh

remove group and others ability to write a file:

sudo chmod go-w file1.txt

allow group and others to read a file:

sudo chmod go+r file1.txt

tar

Like .zip files, but for Linux

make a tar

Copy current folder into a gzip-compressed tar archive

tar zcvf test.tgz .

extract a tar

Extract the contents of a tar into current directory

tar -xzf test.tgz

zip

For all the times you can't just use tar

make a zipped folder

requires the zip package

zip sampleZipFile.zip ExampleFile.txt ExampleFile1.txt

extract a zipped folder

requires the unzip package

unzip sampleZipFile.zip

ln

Linux create a filesystem link

hard link

creates another inode to the data on hard disk,

completely transparent to underlying software

ln SOURCE TARGET

soft link

creates a symbolic link to point to another file

some software doesn't play nice

ln -s SOURCE TARGET

List Disks:

df

df is a coreutil and is almost certainly on your system

df -h

parted

parted is a useful tool for disks

May not be on your system

Has great GUI's available like GParted

parted -l

mount

From a given disk device, mount it to filesystem:

mount [OPTION/S] DEVICE_NAME DIRECTORY_NAME

mount -t ext4 /dev/sdb1 /mnt/media

You can un-mount with umount:

sudo umount /dev/sdc1

ncdu

requres ncdu package

Look through a filesystem and see where space is being used:

sudo ncdu /

DD:

Use DD to make a backup image of a physical disk device:

sudo dd bs=4M if=/dev/sdc of=/home/urname/pi_BKP.img status=progress

Use DD to restore a backup image of a drive onto a disk

sudo dd bs=4M if=/home/urname/pi_BKP.img of=/dev/sdc status=progress

Use DD to duplicate one drive onto another:

dd if=/dev/sda of=/dev/sdb bs=4M conv=notrunc,noerror status=progress

S.M.A.R.T. Hard Drive health

smartctl is part of the smartmontools package

useful for maintaining spinning-disk Hard drives

can detect some fatal errors before data loss

Run a short test on a disk

smartctl -t short /dev/sda

Run a long test on a disk

smartctl -t long /dev/sda

Human-readable health of a disk

smartctl -H /dev/sda

All info of a disk, including results of all tests

smartctl -a /dev/sda

mkdosfs

Format a whole thumbdrive as FAT32, without a partition table

part of the dosfstools package

useful for many legacy hardware devices:

sudo umount /dev/sdc1 && sudo mkdosfs -F 32 -I /dev/sdc1

can be used for formatting FAT16 or FAT12 as well:

sudo umount /dev/sdc1 && sudo mkdosfs -F 16 -I /dev/sdc1

Network and remote backup

Oneliners for network troubleshooting, downloading, and remote backup

rsync

requires rsync package

Remotely SYNC two directories

smarter than scp and easier than creating a tar:

rsync -rtvP --bwlimit=512 [source] [destination]
# [source] [destination] e.g. [email protected]:/mixtapes /home/localuser/mixtapebkp
# Where
#
# -r is recursive
# -t retains the files' modification time
# -v to show what is going on
# -P shows the progress
# --bwlimit=512 limits the upload to 512Kb/sec, ~= 4.1 mbps
#
# If interrupted, just run the command again and it will pickup where it left off

SSH Tunneling

Oneliners for network tunneling over SSH.

Quick, easy and secure method to access remote services

ssh -L local_port:remote_address:remote_port [email protected]

Make a local resource available on a remote machine

ssh -R remote_port:local_address:local_port [email protected]

ping

check if a server is reachable, AKA an echo request:

ping google.com

simple way to check if if internet connection is good, even if DNS isn't:

ping 1.1.1.1

whois

check if a website is legit, and/or how long it has been registered:

whois example.com

what's my IP

Figure out what your IP looks like to other computers on internet:

curl ifconfig.me

nslookup

check if system DNS is working

nslookup pets.com

speedtest

requires speedtest-cli package

test your connection speed to the internet:

speedtest-cli

nmap

requires nmap package

Check if a specific port or range of ports (TCP and or UDP) is/are open using nmap:

nmap -p U:53,111,137,T:21-25,80,139,8080 1.1.1.1 example.com

telnet

requires telnetd package

simple way to test if a TCP session can be initiated to a host at a port:

telnet 1.1.1.1 80

press ctrl+] then ctrl+d to exit (US keyboard)


Downloading

Oneliners for downloading things

wget

requires wget package

Download a file from a website over http:

wget example.com/funnymeme.jpg

Download all URL's listed in a text file:

wget -i example.com/fire_mixtape.m3u

aria2c

requires aria2 package

Download a file faster using multiple TCP connections:

aria2c -x 16 -s 16 example.com/funnyvideo.mp4
#          |    |
#          |    |
#          |    |
#          ---------> the number of connections here

yt-dlp

requires yt-dlp package

works on many different video and audio sites, including YouTube!

may not work right unless you update it

Download a YouTube video:

yt-dlp 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"

Download a YouTube video and convert to mp3:

yt-dlp --restrict-filenames --ignore-errors -x --audio-format mp3 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'

Download an album from bandcamp as mp3 files:

yt-dlp 'https://example.bandcamp.com/album/an-album' -x --audio-format best

Download a video from any other website via m3u8 manifest

(Chrome) Right Click -> Inspect -> Network -> search "m3u8" -> refresh page -> right click -> Copy -> link address

download via m3u8:

yt-dlp 'https://ga.video.cdn.pbs.org/videos/frontline/279e5586-ffca-4870-a56f-f6452b06aafd/2000244604/hd-16x9-mezzanine-1080p/00004005-hls-16x9-1080p.m3u8'

eyeD3

requires eyeD3 package

label all .mp3 files in current directory with Artist and Album name:

eyeD3 -a "Example Artist" -A "Example Album" *.mp3

set track number for an .mp3 file

eyeD3 -n 2 song.mp3

set title for an .mp3 file

eyeD3 -t "In The End" intheend.mp3

FFMPEG

Oneliners for FFMPEG, the powerful video manipulation tool

All commands require ffmpeg package

convert a video (any format) for Twitter

max length 2min 20sec, any video after will be cut off

convert a video for posting on Twitter:

ffmpeg -i in.mov -strict -2 -max_muxing_queue_size 9999 -vcodec libx264 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -pix_fmt yuv420p -strict experimental -r 30 -t 2:20 -acodec aac -vb 1024k -minrate 1024k -maxrate 1024k -bufsize 1024k -ar 44100 -ac 2 out.mp4

create video of music over a static image

e.g. for uploading a song to YouTube

use:

ffmpeg -loop 1 -r 1 -i pic.jpg -i audio.mp3 -c:a copy -shortest -c:v libx264 output.mp4

where audio.mp3 is music, pic.jpg is album cover, output.mp4 is output filename

trim a video

trim a video without re-encoding

from stackoverflow.com/a/42827058

use:

ffmpeg -ss 00:01:00 -to 00:02:00  -i input.mp4 -c copy output.mp4

-i: This specifies the input file. In that case, it is (input.mp4).
-ss: Used with -i, this seeks in the input file (input.mp4) to position.
00:01:00: This is the time your trimmed video will start with.
-to: This specifies duration from start (00:01:40) to end (00:02:12).
00:02:00: This is the time your trimmed video will end with.
-c copy: This is an option to trim via stream copy. (NB: Very fast)

Note: -to specifies the length of the footage clip, not the end timecode!

heavily compress a video

ideal for spotty networks, like w/ cell phones

set -threads to number of physical CPU cores on computer

compress a 16:9 video to very low resolution (768x432):

ffmpeg -i input.mp4 -strict -2 -max_muxing_queue_size 9999 -threads 4 -vf scale=768x432:flags=lanczos -c:v libx264 -preset slow -crf 21 output_compress_480p.mp4

upscale a video

ideal for uploading low resolution video to youtube while perserving bitrate (quality)

upscale a video:

ffmpeg -i input.mp4 -strict -2 -max_muxing_queue_size 9999 -threads 4 -vf scale=1920x1080:flags=lanczos -c:v libx264 -preset slow -crf 21 output_compress_1080p.mp4

ImageMagick

Oneliners for ImageMagick, the powerful photo manipulation tool

credit Drew Lustro and Lukasz Tkacz

All commands require ImageMagick package and associated pre-requisites

Display info on an image:

magick identify portrait.jpeg

Fix image orientation

convert INPUT_FILE -auto-orient OUTPUT_FILE

Remove transparency

convert INPUT_FILE -alpha remove OUTPUT_FILE

set -P to number of physical CPU cores on computer

Convert raw images, commonly used in photography, to web-safe JPEG:

find . -type f \( -iname \*.CR2 -o -iname \*.ARW -o -iname \*.3fr -o -iname \*.ari -o -iname \*.arw -o -iname \*.bay -o -iname \*.braw -o -iname \*.crw -o -iname \*.cr2 -o -iname \*.cr3 -o -iname \*.cap -o -iname \*.data -o -iname \*.dcs -o -iname \*.dcr -o -iname \*.dng -o -iname \*.drf -o -iname \*.eip -o -iname \*.erf -o -iname \*.fff -o -iname \*.gpr -o -iname \*.iiq -o -iname \*.k25 -o -iname \*.kdc -o -iname \*.mdc -o -iname \*.mef -o -iname \*.mos -o -iname \*.mrw -o -iname \*.nef -o -iname \*.nrw -o -iname \*.obm -o -iname \*.orf -o -iname \*.pef -o -iname \*.ptx -o -iname \*.pxn -o -iname \*.r3d -o -iname \*.raf -o -iname \*.raw -o -iname \*.rwl -o -iname \*.rw2 -o -iname \*.rwz -o -iname \*.sr2 -o -iname \*.srf -o -iname \*.srw -o -iname \*.tif -o -iname \*.x3f \) -print0 | xargs -0 -n 1 -P 4 -I {} convert -verbose -units PixelsPerInch {} -colorspace sRGB -resize 2560x2650 -set filename:new '%t-%wx%h' -density 72 -format JPG -quality 80 '%[filename:new].jpg'

modify for:

  • potato CPU: ...xargs -0 -n 1 -P 1 -I {}...
  • NASA CPU: ...xargs -0 -n 1 -P 64 -I {}...
  • Max Twitter resolution: ...-resize 4096x4096... (preserves aspect ratio, scales by maximum of h and w)
  • higher quality JPEG: ...-format JPG -quality 97...

set -quality to desired level of compression, from 1-100

Compress a JPEG to smaller file size:

convert image.jpg -quality 75 output_file.jpg

Scale an image, preserve ratio:

convert INPUT_FILE -resize 75% OUTPUT_FILE

Resize, within bounding limits, still preserving ratio:

convert INPUT_FILE -resize 640x480 OUTPUT_FILE

Convert an image:

convert vaccine_card.jpg vaccine_card.pdf

Rotate an image 90 degrees:

convert INPUT_FILE -rotate "+90" OUTPUT_FILE

Rotate only if width is greater than height:

convert INPUT_FILE -rotate "+90>" OUTPUT_FILE

Rotate only if height is greater than width:

convert INPUT_FILE -rotate "+90<" OUTPUT_FILE

Convert video to GIF animated image:

convert -quiet -delay 1 a.avi a.gif

Ghostscript (PDF tool)

Merge PDF files:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=vaccine_combined.pdf vaccine_card.pdf booster.pdf

Replace page in a PDF document:

extract page from new document:

gs -sDEVICE=pdfwrite -sOutputFile=part1.pdf -dBATCH -dNOPAUSE -sPageList=1 newdoc.pdf

extract all other pages from old document:

gs -sDEVICE=pdfwrite -sOutputFile=part2.pdf -dBATCH -dNOPAUSE -sPageList=2- olddoc

combine two parts together (see above)


Get Tools

Install Matthew's favorite tools

Uses Matthew's gettools.sh BASH script

works on MacOS, Debian (e.g. Ubuntu), Fedora (e.g. RHEL), and Arch (e.g. Manjaro) based systems

careful, downloads lots of stuff

Don't do this for scripts you don't trust! (Also, why do you trust me?):

sudo wget -O - https://raw.github.com/mkrupczak3/gettools/master/gettools.sh | sudo bash