NOTE 1: This page contains some shell magic which I came across in my daily business. I will update this page continuously and this will be reflected on this page in the date information displayed under the post name.

NOTE 2: Please be advised that some commands listed here could have unwanted effects if you are not completely sure of what they are doing and how they are doing it. Some of them might also require sudo access, so I am just going to assume that you know what you are doing. You have been warned!

11. From uppercase to lowercase and from space to underscore

echo 'Building REST services with Spring' | tr '[:upper:]' '[:lower:]' | tr ' ' '_' | xclip -sel c

Transform tr uppercase letters [:upper:] to lowercase [:lower:], then replace the space with an underscore _ and finally copy xclip the result to clipboard -sel c. After this you can paste the text wherever you need it. To prove the usefulness of this, instead of copying the result into the clipboard, pipe it into xargs to, for instance, create a folder, like: ...| xargs mkdir -p.

10. Execute a program periodically

watch du -sh <folder>

Execute the du command periodically, every 2 seconds, until interrupted. The command displays only the total size -s of the <folder>, in a human readable format -h.

9. Display file space usage in human readable format

du -hc . | sort -rh | head -5

Display size in human readable format -h, producing a grand total -c, then sort the output in reversed order -r by comparing human readable sizes -h, and finally taking only the first five lines of the output.

8. Search for a pattern inside files

grep -rnw . -e 'pattern'

Search in the current . directory, recursively -r, matching -e the pattern pattern, considering only whole words -w, displaying the line number -n of the match.

7. Forcibly removing all directories and their content recursively

ls -l | grep '^d' | awk '{print $9}' | xargs rm -rf

In plain English, list the content of the current directory ls using the long listing format -l, filter out everything else that is not a directory grep '^d', extract the 9^th^ column (the last containing the name of the directory) awk '{print $9}', pass each name of the directory as an argument xargs to the command rm, which removes recursively -r and forcibly -f the respective directory.

6. Switching between GitHub remote URLs (HTTPS vs. SSH)

git remote -v
git remote set-url origin
git remote set-url origin

First we check what kind of remote URLs we have, using option -v of command git remote, afterwards we change the remote URL to either HTTPS or SSH using one of the last two commands (note the two different URL styles for setting the remote name origin). Do not forget to replace the USERNAME and REPOSITORY with the real values.

5. Searching for files and creating an archive out of them

find . -type f \( -name file1 -o -name file2 \) | zip -@ -j files
mv ~/tmp
cd $_
xdg-open .

First we search in the current . directory for entries denoting files -type f having the name -name file1 or -o file2. The zip command takes the list of files -@ and creates an archive with the name files containing only the files without storing the directories where they reside. After moving mv the archive to the tmp directory we cd into it, $_ references the last argument of the previous command, and open xdg-open the current directory . with the appropriate program - in my case this is Files.

4. Manage processes from the Terminal

# display Linux processes

# top improved, you might need to install it

# list all running processes and look for a specific process
ps -A | grep chrome

# display processes in a tree like format

# look up a process based on name
pgrep chome

# kills process identified by the PID, use -9 or -KILL to force kill
kill PID

# kill process given its name
pkill chrome
killall chrome

# allows killing graphical programs
# run it and your cursor will turn into x, then click a program's window

3. Print content of $PATH line-by-line

echo $PATH | tr ':' '\n'

Pipes the content, obtained via echo, of the PATH variable into the translate tr command which replaces each occurrence of : with a new line \n.

Another example:

echo line1:line2 | tr ':' '\n'

2. Show all users

cat /etc/passwd | awk -F: '{print$1}'

The content of the file passwd is pipped into the awk, which takes each line and splits it into columns by the defined -F separator : printing out the first value (the user name).

1. Show all hidden files

ls -lap | grep -v / | awk '{print $9}' | grep '^\.'

List ls all -a content of the current directory using the long listing format -l appending -p a forward slash / if the entry is a directory, then print only those lines which do not end with a forward slash (grep performs a pattern matching, while -v inverts the match). awk prints out the nineth column of the line - the name, and the last grep shows only those which begin with a ..