Useful Shell Commands

Find largest files in the current directory

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

Find largest directories or files in the current directory

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

Find files matching the name pattern and apply a command

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

An option for recursive grep

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

Grepping multiple filesĀ and highlighting the results

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

Dealing with ls results

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

Listing processes listening to ports

sudo lsof -nP -iTCP -sTCP:LISTEN
sudo lsof -i -Pn | grep LISTEN

Working with archives

# -z: Compress archive using gzip program
# -c: Create archive
# -v: Verbose i.e display progress while creating archive
# -f: Archive File name
tar -zcvf prog-1-jan-2005.tar.gz /home/jerry/prog

# -x: Extract files
tar -zxvf prog-1-jan-2005.tar.gz
tar -zxvf prog-1-jan-2005.tar.gz -C /tmp

Working with docker containers

# killing all running containers
for i in $(docker ps -q); do docker kill $i; done
# remove all (also stopped) containers
docker rm -f $(docker ps -qa --no-trunc)
# remove only stopped containers
docker ps -aq --no-trunc | (xargs docker rm 2>/dev/null)

# inspecting container IP-address
docker inspect --format '{{ .NetworkSettings.IPAddress }}' XXXXXXXX
# IP-addresses of all running containers
for i in $(docker ps -q); do echo -n $i" "; docker inspect --format '{{ .NetworkSettings.IPAddress }}' $i; done

# start container, replacing its ENTRYPOINT and use parameters
# the one below starts "/bin/bash -c ls -l" in example/redis
docker run -i -t --entrypoint /bin/bash example/redis -c ls -l

Working with docker images

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

Machine Information

# number of CPUs
grep -ic proc /proc/cpuinfo
# RAM in MBs
free -m
# RAM in GBs
free -g