Useful Shell Commands

Find largest files in the current directory

1find . -printf '%s %p\n'| sort -nr | head -10

Find largest directories or files in the current directory

1du -a . | sort -n -r | head -n 10

Find files matching the name pattern and apply a command

1# applying a command to search result
2find . -name '*.iml' -exec ~/bin/fromdos.sh {} \;
3# copy build jobs configurations, keeping folders structure
4find . -name config.xml | grep -v "/modules/" | while read i; do echo "$i" && cp --parents "$i" /tmp/build-jobs/.; done

An option for recursive grep

1find . -maxdepth 2 -name config.xml | xargs grep -s deploy
2find . -name '*.conf' -exec grep -H refapp-t {} \;

Grepping multiple files and highlighting the results

1egrep -wi --color 'warning|error|critical' /var/log/messages

Dealing with ls results

1for i in $(ls *.jpg); do mv $i a_$i; done

Listing processes listening to ports

1# MAC
2sudo lsof -nP -iTCP -sTCP:LISTEN
3# SLES
4sudo lsof -i -Pn | grep LISTEN

Working with archives

1# -z: Compress archive using gzip program
2# -c: Create archive
3# -v: Verbose i.e display progress while creating archive
4# -f: Archive File name
5tar -zcvf prog-1-jan-2005.tar.gz /home/jerry/prog
6 
7# -x: Extract files
8tar -zxvf prog-1-jan-2005.tar.gz
9tar -zxvf prog-1-jan-2005.tar.gz -C /tmp

Working with docker containers

1# killing all running containers
2for i in $(docker ps -q); do docker kill $i; done
3# remove all (also stopped) containers
4docker rm -f $(docker ps -qa --no-trunc)
5# remove only stopped containers
6docker ps -aq --no-trunc | (xargs docker rm 2>/dev/null)
7 
8# inspecting container IP-address
9docker inspect --format '{{ .NetworkSettings.IPAddress }}' XXXXXXXX
10# IP-addresses of all running containers
11for i in $(docker ps -q); do echo -n $i" "; docker inspect --format '{{ .NetworkSettings.IPAddress }}' $i; done
12 
13# start container, replacing its ENTRYPOINT and use parameters
14# the one below starts "/bin/bash -c ls -l" in example/redis
15docker run -i -t --entrypoint /bin/bash example/redis -c ls -l

Working with docker images

1# removing docker images
2for i in $(docker images -q); do docker rmi -f  $i; done

Machine Information

1# number of CPUs
2grep -ic proc /proc/cpuinfo
3# RAM in MBs
4free -m
5# RAM in GBs
6free -g