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!

NOTE 3: All the commands have been tried on Ubuntu 18.04.4 LTS


1. 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.

Resources:

  • feel free to use the man command to read more about the programs above:
    • man ls
    • man grep
    • man awk
    • man xargs
    • man rm


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

git remote -v
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

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.

Resources:



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

find . -type f \( -name file1 -o -name file2 \) | zip -@ -j files
mv files.zip ~/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.

Resources:

  • feel free to use the man command to read more about the commands above:
    • man find
    • man zip


4. Manage processes from the Terminal

# display Linux processes
top

# top improved, you might need to install it
htop

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

# display processes in a tree like format
pstree

# 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
xkill

Resources:

  • feel free to use the man command to read more about the commands above


5. 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'
line1
line2

Resources:

  • feel free to use man tr to read more about the command


6. 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).

Resources:

  • man awk to read more about awk language


7. 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 ..