bash - useful commands

🗞Shell
bash

Great general resource for shell scripting

Run bash/sh script (alternatives)

bash my_script.sh

or

source my_script.sh

or

./my_script.sh

Don’t forget: Script has to be executable, e.g. via

chmod +x my_script.sh

create several files at once

touch {1,2,3,4}.md

Create directory and cd to it right away

mkdir foo && cd $_

$: Contents of a variable (more information can be found here) _: Last argument of previous command (more information)

Write

Redirect stdout & stderr

Example:

grep -i 'abc' content 2>/dev/null
// or
npm list -g --depth=0 2>/dev/null

Syntax:

  • 1> file redirects stdout to file
  • > file redirects stdout to file (shorthand for 1>)
  • 2> file redirects stderr to file
  • &> file redirects stdout and stderr to file

/dev/null is the null device. It takes any input you want and throws it away. It can be used to suppress any output.

Some more information on null

Redirect stderr to stdout before piping

Example

app 2>&1 | grep hello

Points file descriptor 2 to where file descriptor 1 is already pointing. Here the file descriptor to redirect (i.e. 2) gets redirected to the already set up file descriptor 1.

Run script, but show stderr in stdout:

#!/bin/sh
exec 2>&1
exec node server/run.js

create-file.sh:

#!/bin/bash

file=/wait/done
>&2 echo "Sleeping for 10 s."
sleep 10
touch $file
>&2 echo "Created file $file."

Search / Find

Shell

  • Find something in directory tree.

    find /root/directory/to/search -name 'filename.*'
  • Find some process/file which runs for an app

    E.g. for mysql there is a my.cnf file. To find which one is in use run

    sudo fs_usage | grep my.cnf

    fs_usage shows running programs live

VIM

```vim
:e **/filename.cpp
```

User generation

Give group ownership of Laravel directory structure to the web group www-data

sudo chown -R :www-data /var/www/laravel

Commands

Find (file or directory names)

  • e.g. set chmod for folders and separately for files

    To change all the directories to 755 (-rwxr-xr-x):

    find /opt/lampp/htdocs -type d -exec chmod 755 {} \;

    To change all the files to 644 (-rw-r—r—):

    find /opt/lampp/htdocs -type f -exec chmod 644 {} \;

Permissions, chmod

The following two commands are equivalent

```bash
chmod u=rwx,g=rx,o=r myfile

chmod 754 myfile
```
  • The letters u, g, and o stand for ”user”, ”group”, and ”other
  • 4 stands for “read”,
  • 2 stands for “write”,
  • 1 stands for “execute”, and
  • 0 stands for “no permission.”

Combinations

Automate git checkout — my_file

git status | grep modified | sed 's/^.*modified:   //' | xargs git checkout
  • xargs: —> UNDER CONSTRUCTION!

Move files/folders with rsync

Alternative to mv backup/ backupArchives/

rsync -a backup/ backupArchives/
rm -rf backup/*

Internet

download files

Downloads file to current folder from web

wget http://files.fast.ai/files/setup_p2.sh

visit websites

curl http://somewebsite.com

Count

Number of files in folder train/dogs:

ls -l train/dogs/ | wc -l

Pipeline continuation / Process expansion

Taken from this SO answer:

Arguments and are made available to rm through a special variable (called argv internally). The standard input, on the other hand, looks to a Unix program like a file named stdin. A program can read data from this “file” just as it would if it opened a regular file on disk and read from that.

Use xargs to circumvent this.

find . -name ".txt" | grep "foo" | xargs rm

via `…` or $(...)

e.g. docker rmi $(docker images | grep "^<none>" | awk '{ print $3 }')

$#, $?, $@ etc.

"$@" stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" …).

See this nice overview

For example for the following command

./command -yes -no /home/username
  • $@ would give you the array {"-yes", "-no", "/home/username"}
  • And $0 = ./command, $1 = -yes etc.

Background jobs (new threads / processes)

  • Append & to run command/script as background job

    sleep 5 && ls | grep "foo" &
  • View all currently running background jobs (-p add more information)

    jobs -p
  • Kill job

    kill %1
  • Kill application

    Example: To kill Docker application on Mac OS X:

    killall Docker

    Restart Docker Application:

    killall Docker && open /Applications/Docker.app

Screens

xrandr

CLI

Build a CLI

A nice guide from Twilio

Auto completion

How to add bash auto completion:

  1. To activate completions for zsh you need to have bashcompinit enabled in zsh:

    autoload -U bashcompinit
    bashcompinit
  2. Afterwards you can enable completion for pipx:

    eval "$(register-python-argcomplete pipx)"

taken from instructions printed to screen when running pipx completions

Discuss on TwitterImprove this article: Edit on GitHub

Discussion


Explain Programming

André Kovac builds products, creates software, teaches coding, communicates science and speaks at events.