Skip to content

Instantly share code, notes, and snippets.

@randria
Last active September 25, 2016 18:05
Show Gist options
  • Save randria/ae36af166ed89dedf8167a0705d3675a to your computer and use it in GitHub Desktop.
Save randria/ae36af166ed89dedf8167a0705d3675a to your computer and use it in GitHub Desktop.
tips on bash
# ps show cpu elapsed time
ps -eo pcpu,etime,pid,user,args --sort=-%cpu --no-header| head -10
# generate a password abcd-efeg-ffdg-dfdf-…
COMB=4; for i in `seq 1 $COMB`; do A=`perl -le 'print map { (a..z)[rand 26] } 1..4;'`; if [ $i -eq $COMB ]; then echo "$A"; else echo -n "$A-"; fi ; done
# remove 1st line
awk ‘NR>1’
# find files not executable
find /some/path -type f ! -perm -111 -ls
# shuffle a number
- seq 5 | perl -MList::Util=shuffle -e 'print shuffle <>;'
- seq 5 | shuf
# replace an ext to another
FILE=file.ext
${FILE[@]/%ext1/ext2}
becomes file.ext2
# replace filename without extension
if FILE="a.out" then
${FILE##*.} = "a"
endif
# xargs
- <output lines> | xargs -I{} command {} -> will execute <command> for each line with the pattern {}
- <output lines> | xargs -J% command % -> will execute <command> with all parameters in a single line
- find .. -print0 | xargs -0 .. -> replaces spaces and EOL by “\0” (NUL)
# send an attachment
uuencode file_to_send file_name_attached | mailx -s “..” destination
# for sequentiel
for ((i=1; i<=10; ++i)); do echo $i; done
# a sequence number
- seq 1 10 -> 1 to 10, one number per line
- for ((i=1; i<=10; i+=2)); do echo $i; done
- echo {1..10} -> 1 to 10 on a single line with space as separator
- seq -w 1 10 -> 01..10 as a string with 2 digits
- for ((i=5; i<=10; ++i)); do printf '%02d\n' $i; done
# sort and remove duplicate lines from 2 or mores files
uniq -u <(sort file1 file2 .. filen)
# remove a line and save file afterward
- vi +<number>d +wq file
- sed -i <number>d file
# show dd status every so often
watch --interval 5 killall -USR1 dd
# a simple function to conveniently extract columns from an input stream
col() { awk '{print $'$(echo $* | sed -e 's/ /,$/g')'}'; }
# iTunes music
find Music/ -type f \( -iname *mp3 -o -iname *wav -o -iname *m4a -o -iname *m4p \) -exec shasum -a 256 {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment