Bash Notes
Lovely bash commands worth remembering:
Test
# -d flag for directory
Truethy
test -d mydir && <dosomestuff>
Falsy
test -d myfile || <dosomestuff>
Cut text
easy way to filter more or less arranged dataset with delimiter.
mh@debian:~$ cat > mytime
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dez
mon-tue-wen-thu-fri-sat-sun
12,23,55,56,27,287,43,456,34,234
mh@debian:~$ cut -d, -f1,4,7 mytime
Jan, Apr, Jul
mon-tue-wen-thu-fri-sat-sun
12,56,43
mh@debian:~$ cut -d- -f3-5 mytime
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dez
wen-thu-fri
12,23,55,56,27,287,43,456,34,234
Brace expansion
Besides pathname and filename expansions (e.g. /var/* or *.txt) there are other forms of expansion like aritmetic expansion:
mh@debian:~$ echo $((34 * 72 + 234))
2682
and brace expansion with many forms of nesting and concatenation:
mh@debian:~$ echo mybox-{1,2,3,4}
mybox-1 mybox-2 mybox-3 mybox-4
mh@debian:~$ echo {1..12}
1 2 3 4 5 6 7 8 9 10 11 12
mh@debian:~$ echo {z..p}
z y x w v u t s r q p
mh@debian:~$ echo {001..20}
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020
mh@debian:~$ echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z
mh@debian:~$ echo {1..12}{A..F}
1A 1B 1C 1D 1E 1F 2A 2B 2C 2D 2E 2F 3A 3B 3C 3D 3E 3F 4A 4B 4C 4D 4E 4F 5A 5B 5C 5D 5E 5F 6A 6B 6C 6D 6E 6F 7A 7B 7C 7D 7E 7F 8A 8B 8C 8D 8E 8F 9A 9B 9C 9D 9E 9F 10A 10B 10C 10D 10E 10F 11A 11B 11C 11D 11E 11F 12A 12B 12C 12D 12E 12F
Single quotes and double quotes
Double quotes do not carry out all forms of expansion. They only perform parameter and arithmetic expansion ($) and recognize the escape character (\). They are best used in cases where spaces in filenames or tabs and newlines should be preserved.
mh@debian:~$ echo -e what\'s \t up \n fellas?
what's t up n fellas?
mh@debian:~$ echo -e "what's \t up \n fellas?"
what's up
fellas?
Single quotes on the other hand do not carry out any expansion.
mh@debian:~$ val=text
mh@debian:~$ echo -e "this \t $val \tshouldn't \n \t be on \none \t line"
this text shouldn't
be on
one line
mh@debian:~$ echo -e $'this \t $val \tshouldn\'t \n \t be on \none \t line'
this $val shouldn't
be on
one line
As you can see, single quotes do not expand $val, yet double quotes do.
Note: An apostrophe has to be escaped when the text isn't quoted, whereas inside of single quotes it has also to be escaped but the quoting itself has to be preceded by a dollar sign ($).
What xargs simply does
<preceding_cmd> | xargs <subsequent_cmd>
It takes the result/s from the preceding command and then makes them available as arguments to the following (piped) command. With the -t flag xarg will also print this intermediate step.
System info
There are several commands to get information about the system resp. about different parts of the system like the operating system, the distribution, hostname etc.
# os related
mh@debian:~$ uname -a
# distro related
mh@debian:~$ lsb_release -a
# get/edit hostname related info
mh@debian:~$ hostnamectl
# list hardware
mh@debian:~$ lshw -short
The lsb_release command primarily shows LSB (Linux Standard Base) related information. LSB is a standard for harmonization of the APIs between distributions, so that (os level) application code runs seamlessly on different distros.
Command info
Collection of tools to get different information about a command like its type, location, options etc.
# get the type of a command
mh@debian:~$ type ls
ls is aliased to `ls --color=auto'
mh@debian:~$ type read
read is a shell builtin
mh@debian:~$ type grep
grep is /usr/bin/grep
More little helpers (which, whatis, whereis):
# locations of the executables
mh@debian:~$ which sed
/usr/bin/sed
# short description
mh@debian:~$ whatis sed
sed (1) - stream editor for filtering and transforming text
# get locations of binaries, sources, man page
mh@debian:~$ whereis sed
sed: /usr/bin/sed /usr/share/man/man1/sed.1.gz /usr/share/info/sed.info.gz
Search through all man pages for particular terms:
# -a to match all terms (i.e. "text" AND "editor")
mh@debian:~$ apropos -a text editor
ed (1) - line-oriented text editor
ex (1) - Vi IMproved, a programmer's text editor
gedit (1) - text editor for the GNOME Desktop
...
Aliases
Some commands may be tedious to write or you have to use a combination of multiple commands. In this case there is the alias command. It creates an alias for any form of string:
mh@debian:~$ alias
alias ls='ls --color=auto'
mh@debian:~$ alias mykey=mysteriousValue0230
mh@debian:~$ alias
alias ls='ls --color=auto'
alias mykey='mysteriousValue0230'
mh@debian:~$ unalias mykey
mh@debian:~$ alias
alias ls='ls --color=auto'
As an example we create an alias command called 'etc7r'. etc7r consists of multiple commands, which get the 7th element in the /etc directory and prints its name in reverse.
mh@debian:~$ alias etc7r='ls /etc |sed -n '7p' |rev'
mh@debian:~$ alias etc7r
alias etc7r='ls /etc |sed -n 7p |rev'
mh@debian:~$ etc7r
fnoc.gpa
stat: display file or file system status/info (access, device, modify, change, birth)