Hidden Wonders

Useful Linux Commands


Lastmod:

Table of Contents

A list of Linux commands I like. I update regularly. It’s more for myself than for anyone else, but I’d be happy if someone else found this page useful.


Core Utils[#]


# Show total disk usage
df -h

# Shows disk usage of current directory (below two commands are equivalent)
du -h --max-depth=0
du -hs

# My usual grep incantation to find some string in a codebase:
# -R = recursively search all subdirectories
# -n = output line number of the file
# -i = ignore case
grep -Rni $string $project_directory

# Gets full path of a file, including the file itself
realpath [file]

vim[#]


# Opens vim in vertical split mode
vim -O 

# Opens vim in horizontal split mode
vim -o 

# Opens vim with tabs for each file (though it's better to use buffers)
vim -p

File Transfer[#]


Below is the script I use to update this website, using rsync:

#!/bin/sh

SERVER_DIR=/path/to/server_dir #No trailing slash here
ADDRESS="username@hiddenwonders.xyz"

hugo && rsync -uvrP --delete-after "public/" $ADDRESS:$SERVER_DIR

What is the point of this? Well, some old computers cannot boot from USB so——unless you have a floppy disk laying around——DVD is your best bet. Also, .iso files are nice for preserving old CDs.

# Mounts .iso file to a directory
mount /path/to/iso /path/to/empty/directory -o loop

# Ejects physical disk (surprised how simple it is)
eject

# Blanks a RW DVD
xorriso -outdev /dev/sr0 -blank as_needed

# Burns .iso to blank disk (CD drive is usually /dev/sr0)
xorriso -as cdrecord -v -sao dev=/dev/sr0 /path/to/iso

base64 encode/decode[#]


Using online base64 encoders/decoders is lame.

# encode
echo "base64 text" | base64

# decode
echo "YmFzZTY0IHRleHQK" | base64 -d

FFmpeg (a lot on encoding webms)[#]


# Crops an image
ffmpeg -i $input -vf crop=$height:$width:$start_height:$start_width $output

# Reduce image size (https://stackoverflow.com/a/28806881)
ffmpeg -i $input -vf scale=$width:$height $output

# Reduce image size, using whatever width value preserves the aspect ratio
ffmpeg -i $input -vf scale=-1:$height $output

# Screen records (may require X11, also no audio)
ffmpeg -r 24 -f x11grab -i :0.0 -c:v libx264rgb -preset ultrafast ~/rec-$(date +%s).mkv

# Trim video or audio file to certain start and stop interval (https://unix.stackexchange.com/a/302469)
ffmpeg -i $input -ss 00:00:20 -to 00:00:40 -c copy $output

mpv[#]


# Play a file at location copied to clipboard (could be a url)
mpv $(xclip -o)

youtube-dl/yt-dlp[#]


Useless Linux Commands[#]


# Makes music
echo "g(i,x,t,o){return((3&x&(i*((3&i>>16?\"BY}6YB6%\":\"Qj}6jQ6%\")[t%8]+51)>>o))<<4);};main(i,n,s){for(i=0;;i++)putchar(g(i,1,n=i>>14,12)+g(i,s=i>>17,n^i>>13,10)+g(i,s/3,n+((i>>11)%3),10)+g(i,s/5,8+n-((i>>10)%3),9));}"|gcc -xc -&&./a.out|aplay

# Makes random music
hexdump -v -e '/1 "%u\n"' /dev/urandom | awk '{ split("0,27,5,67,13,18,55,666,12,000",a,","); for (i = 0; i < 1; i+= 0.0001) printf("%08X\n", 70*sin(100*exp((a[$1 % 12]/24)*log(12))*i)) }' | xxd -r -p | aplay -c 1 -f S32_LE -r 66665

Home Top


Site Licensing Site last updated: 2024-10-15