bash tips
Track Live Changes
See changes in a file as it changes:
tail -f *somefile*
See changes in a directory, as it changes:
watch -d ls *directory*
Or use the -g
flag to exit once the output changes.
This command will look at whether you're connected to the internet, and turn into a rainbow once the connection hits.
watch -g ip address && clear && ip address | lolcat
Automatic Renaming
There are a bunch of files:
- Column CV.aux
- Column CV.log
- Column CV.out
- Column CV.pdf
- Column CV.tex
- tccv.cls
Goal: swap the word "Column" for "Alice" in all files.
1IFS=$'\n'
2for f in $(find . -name "Col*"); do
3mv "$f" $(echo "$f" | sed s/Column/Alice/)
4done
IFS is the field separator. This is required to denote the different files as marked by a new line, and not the spaces.
(Alternatively, just install renameutils
and do rename Column Alice *
)
Arguments and Input
The rm' program takes arguments, but not
stdin' from a keyboard, and therefore programs cannot pipe results into rm.
To fix this, use xargs
to turn the stdin into an argument.
For example, if we have a list of files called `list.txt' then we could use cat as so:
1cat list.txt | xargs rm
Of course if spaces are included in the file, you would have to account for that.
Numbers
Add number to variables with:
let "var=var+1"
let "var+=1"
let "var++"
((++var))
((var=var+1))
((var+=1))
var=$(expr $var + 1)
((n--))
works identically.
POSIX WARNING
The number commands above work in bash
, but not in bare-ass POSIX shells, such as dash
.
Instead, you might do:
1x=2
2x=$(( x +1 ))
3x=$(( x*x ))
Finding Duplicate Files
1find . -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 15 > all-files.txt
Output random characters
1cat /dev/urandom | tr -cd [:alnum:] | dd bs=1 count=200 status=none && echo
Temporary Working Directory
Try something out in a random directory in /tmp
so the files will be deleted when you next shut down.
1mktemp -d
That gives you a random directory to mess about in.
1 dir=$(mktemp -d)
2 for x in {A..Z}; do
3 fortune > "$dir"/chimpan-$x
4 done
5 cd $dir
POSIX WARNING
These smart-brackets are a bash feature.
If you try to use {A..Z}
in dash, it will think of this as a single item.