Bash

Echo

Change and reset colors

01-08-2023, source: StackOverflow; SuperUser

echo "$(tput setaf 4)==>$(tput sgr0) Hello World"

Change the printed text

01-08-2023, source: StackOverflow

echo -n " 50% complete."$'\r'; sleep 1; echo "100% complete."

List the colors

01-08-2023, source: self

for i in {0..50}
do
   echo "$(tput setaf $i) ### $i$(tput sgr0)"
done

List the file types (estimate)

05-12-2022, source: StackOverflow

find . -type f -exec file -- {} +
  • -type f determines file type

  • -f NAMEFILE/--files-from NAMEFILE specifies a particular file

List the file sizes

22-11-2022, source: StackOverflow

du -hs .
  • du displays disk usage statistics

  • -h stands for a human-readable format.

  • -s stands for the files only (depth is 0, i.e., -d 0).

  • By default, the du program recursively lists the directories.

List recursively files and occurrences containing a text

17-08-2022, source: self

grep -R 'text' .
  • -R/-r/--recursive forces recursive search

Find empty JAR archives

25-07-2023, source: StackOverflow

find $HOME/.m2/repository -iname "*.jar" -size 0
find $HOME/.m2/repository -iname "*.jar" -size 0 -exec rm {} \;

Manipulation

Move all files (but not directories)

15-02-2023, source: self + AskUbuntu

for i in `find documents/source -type f -maxdepth 1` ; do
  cp $i documents/destination ;
done
  • -type f determines file type

  • -maxdepth MAXDEPTH sets the depth limit from the source directory, remove for recursive search

Programs

Is a program installed

29-11-2022, source: StackOverflow

# Node.js for example
if which node > /dev/null
then
    echo "Using Node.js version: $(node -v)"
else
    echo "Node.js not installed"
    exit 1
fi

Users and Rights

Make the file executable

23-12-2022, source: self

chmod +x script.sh
chmod +x *.sh

List all users

30.10.2023, source: StackOverflow

dscl . list /Users | grep -v '^_'