Skip to content

Instantly share code, notes, and snippets.

@DaRaFF
Last active September 28, 2015 12:18
Show Gist options
  • Save DaRaFF/1437173 to your computer and use it in GitHub Desktop.
Save DaRaFF/1437173 to your computer and use it in GitHub Desktop.
Bash Command Line Magic (shell)

remote connection

# check if a hostname/port is open
nc -zv <host> <port>

# connect to host/port with telnet
telnet <host> <port>

general commands

# make a regex on a list of files - put the filenames in a .txt file
grep -i -r -l "\<Role FormalName=\"Article\"" /media/data/importCache/newsml.nzz.ch/root-nodes/_1/ > /media/data/grep-output.txt

# make a regex on a list of files - if somethings is found, take the filename and put the content of the file in a .txt file
grep -i -r -l "\<Role FormalName=\"DepartmentTeaser\"" /media/data2/importCache/newsml.nzz.ch/root-nodes/_1/ | xargs -n 1 cat > /media/data1/department-teaser-list-out2.txt

# find xml's in a directory / do a regex on the result list, take the matching part, sort it and kill duplicats and pipe it to a file
find ./src/Nzz/ApiBundle/Tests/Functional/Fixtures/newsml/www.mann.nzz.ch/root-nodes/_1/ -name "*.xml" | grep -o "_1\.[0-9]\{1,10\}\.[0-9]\{1,12\}" | sort -u > diff-file.txt

bash programming

zipping / unzipping

# x= extract / c= compress / v = verbose / j = bz2 compression / z = gzip compression / f = filename
tar -cvjf filename.tar.bz2 <src-directory>                                 #pack tar.bz2 file
tar -cvzf filename.tar.bz2 <src-directory>                                 #pack tar.gz file
7z a -t7z filename.7z <src-directory>                                      #pack 7z

tar xvjf filename.tar.bz2                                                  #unpack tar.bz2 file
tar xvzf filename.tar.gz                                                   #unpack tar.gz file
7z x filename.7z                                                           #unpack 7z file

some helper commands to find where something is or where something is executed

which
find
whereis
apropos
locate

rpm

rpm -qa | grep redis                                                         #get package name and filter
sudo rpm -e phpredisadmin-1-0.x86_64                                         #remove rpm package
sudo rpm -ivh /home/nzz/rpmbuild/RPMS/x86_64/phpredisadmin-1-0.x86_64.rpm    #install rpm package
rpmbuild -bb phpredisadmin.spec                                              #build rpm based on spec file

rpm to deb

sudo alien -k phpredisadmin.rpm                                              #convert rpm to deb
sudo dpkg -i phpredisadmin.deb                                               #install deb

install deb

sudo dpkg -i package.deb

ssh tunneling

show folders where a package is installed

dpkg -L php5-xdebug

copy data from one computer to another via ssh recursively

scp -r miguel@10.1.2.2:/home/miguel/ miguel@10.1.2.3:/home/miguel/

copy data from one computer to another via ssh recursively without following symlinks

rsync -avz -e ssh /src/dir user@remote.host:dst/dir

show partitions of harddisks

df -h

show total filesize of directory

du -hs ./build

List of useful Ubuntu Shortucts

List of useful Unix Commands

List of Commands for Systeminfos

show size of folders in a nice overview

ncdu

shows which ports are in status LISTEN

netstat -an | grep "LISTEN "

shows port and running program on this port

netstat -atlpvn

shows default gateway

netstat -rn 

dig - DNS lookup utility

dig buecher.nzz.ch

ps - Show memory consumption of some processes

ps -eo size,pid,user,command | grep consumer | awk '{ hr=$1/1024 ; printf("%13.6f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | sort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment