The below should all work in bash or zsh

for f in $( file . | grep broken | cut -d':' -f 1 ); do rm -i $f; done

Prepend to filename

for file in ; do mv $file 'Pandoras_Box_'$file ; done

Undo above (delimiter arg may need to be tweaked here)

for file in ; do mv $file `echo $file | cut -f3 -d_` ; done

Get top level of path

echo '/'`pwd | cut -d'/' -f2`

Move all *mp4 files into their own folder

for file in mp4; 
folder=`echo $file | cut -d'-' -f1 | rev | cut -b 2- | rev`;
mkdir $folder;
mv -v $file $folder;

Get multiline input

echo "Pipe in input, or paste and it ctrl-d when done"

Delete interactively any file in the folder not an mp3

find ./ ! -name "mp3" -type f -exec rm -i {} \;

Bulk rename with Vim

ls | sed 's/^/\"/g;s/$/\"/g' | vim -

:%s/./mv -i & &/g

Inside the quotes in the second arg

:s/\%V /_/g

When done

If no slashes at the end of the file name

:s/\"\ \"/\/\"\ \"/g

Save and quit

:w mvs
sh ./mvs

Inspired by

Replace all spaces with underscores in file name

for f in \ ; do mv "$f" "${f// /_}"; done

For recursive see

Create .tar.bz2

tar -cvjf output.tar.bz2 ./

Concat jpgs into pdf – requires imagemajick

convert .jpg career-guide-private-equity.pdf

Sort annoying numbering

for i in `seq 1 9`;
echo $i;
mv "career-guide-private-equity-$i-1024.jpg" "career-guide-private-equity-0$i-1024.jpg";

ffmpeg .flv to .m4a

first check the input

ffmpeg -i inputfile.flv
ffmpeg -i input.flv -vn -acodec copy output.m4a

Show top ten most used commands on the shell

history | cut -d' ' -f 1 | uniq -c | sort -r | head -n10

List contents of tar file

tar --list -f items.tar

for file in ./sdcard/repo/dotfiles/; 
    MY_FILE=`echo $file | rev | cut -f1 -d'/' | rev` ;
    echo "ln -s $MY_PATH$MY_FILE .$MY_FILE";

Find most recently modified files recursively (BSD/OSX style)

find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -10 | cut -f2- -d" " | more
while read -r url; do wget $url; done < list_or_urls
grep -o '"http://[^"]"' ./ | grep mp4

Bash colours and formatting (control sequences)


You want a random index.html from a mirror of some sort

# cd to top of directory tree
cd mirror

# generate list of index.html files
find . -name 'index.html' -type f > list_of_all

# Pick one at random     & remove leading dot
shuf -n 1 list_of_all | cut -b 2-

# Open on server
xdg-open "http://leia:8081"`shuf -n 1 list_of_all | cut -b 2-`

Audio from Youtube

# D/l from youtube
youtube-dl ''

# Extract audio
ffmpeg -i Aaliyah\ +\ Tim\ \&\ Bob\ -\ Rock\ the\ Boat\ \(Duncan\ Gerow\ Mix\)-CnDUZCtALWI.mp4 -c:a copy -vn -sn Aaliyah\ +\ Tim\ \&\ Bob\ -\ Rock\ the\ Boat\ \(Duncan\ Gerow\ Mix\)-CnDUZCtALWI.m4a


Get file exts of top 50 files by filesize in a folder (BSD)

ls -s | sort | tail -r -n 50 | cut -d'.' -f 2 | sort | uniq

Find files with interesting file exts

find . -type f -name ".MOV" -o -name ".avi" -o -name ".flv" -o -name ".m4v" -o -name ".mov" -o -name ".mp4" -o -name ".wmv" > vid_list

Combine several PDFs into one

qpdf --empty --pages .pdf -- out.pdf

List by file modified time recursively (top 20)

find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" "

Remove OSX bits

find . -name ".DS_Store" -print0 | xargs -0 rm -rf
find . -name "._" -print0 | xargs -0 rm -rf

Concat wavs, make mp3/flac

shntool join 2016-11-28_20h17m44.wav 2016-11-28_20h17m44_01h02m53.wav
lame -q0 -b256 joined.wav
flac --repla    y-gain -8 joined.wav

Webm to mp3

ffmpeg -i "${FILE}" -vn -ab 128k -ar 44100 -y "${FILE%.webm}.mp3";

Escape spaces in string

FILE_PATH=$( echo "$FILE_PATH" | sed 's/ /\\ /g' )

Markdown style output from tree

tree -F -L 2 | col -x | tr -d '│' | tr -d '├' | tr -d '└' | tr '─' '-' | cut -b 2- | sed 's/--/-/g' | sed '$d'

Rename all .markdown files in a git repository .md

for file in *markdown; do git mv $file `echo $file | cut -f1 -d'.'`.md ; done

Wrap and beautify markdown

cmark -t commonmark --width 81 >