one line BASH commands that you can't remember anyways 😉
Life is too short to fumble with bash commands
Share with your pals 🤝🔗: oneliners.sh
Merge in your own slick lines 📋✍️: oneliners GitHub repo
Oneliners for disks and files
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
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"
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"
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
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
Like .zip files, but for Linux
Copy current folder into a gzip
-compressed tar
archive
tar zcvf test.tgz .
Extract the contents of a tar into current directory
tar -xzf test.tgz
For all the times you can’t just use tar
requires the zip
package
zip sampleZipFile.zip ExampleFile.txt ExampleFile1.txt
requires the unzip
package
unzip sampleZipFile.zip
Linux create a filesystem link
creates another inode to the data on hard disk,
completely transparent to underlying software
ln SOURCE TARGET
creates a symbolic link to point to another file
some software doesn’t play nice
ln -s SOURCE TARGET
df
is a coreutil
and is almost certainly on your system
df -h
parted
is a useful tool for disks
May not be on your system
Has great GUI’s available like GParted
parted -l
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
requres ncdu
package
Look through a filesystem and see where space is being used:
sudo ncdu /
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
smartctl
is part of the smartmontools
package
useful for maintaining spinning-disk Hard drives
can detect some fatal errors before data loss
smartctl -t short /dev/sda
smartctl -t long /dev/sda
smartctl -H /dev/sda
smartctl -a /dev/sda
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
Oneliners for network troubleshooting, downloading, and remote backup
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. myuser@remotemachine.com:/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
Oneliners for network tunneling over SSH.
Quick, easy and secure method to access remote services
ssh -L local_port:remote_address:remote_port username@server.com
Make a local resource available on a remote machine
ssh -R remote_port:local_address:local_port username@server.com
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
check if a website is legit, and/or how long it has been registered:
whois example.com
Figure out what your IP looks like to other computers on internet:
curl ifconfig.me
check if system DNS is working
nslookup pets.com
requires speedtest-cli
package
test your connection speed to the internet:
speedtest-cli
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
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)
Oneliners for downloading things
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
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
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'
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
Oneliners for FFMPEG
, the powerful video manipulation tool
All commands require ffmpeg
package
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
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 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!
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
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
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:
...xargs -0 -n 1 -P 1 -I {}...
...xargs -0 -n 1 -P 64 -I {}...
...-resize 4096x4096...
(preserves aspect ratio, scales by maximum of h and w)...-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
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)
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