Random Collection of Info
Using find to chmod multiple files: Use
find
tochmod
ONLY filesUsing awk to Modify Output: Using
awk
to manipulate some output - small amount of examplesUsing \ as New Lines: Example of using the
\
to signify a return carriage to a line of commands - IE keep processing as if its one lineShell Script Location: bash line to assign the current location of the bash script to a variable.
Using sed to Make Updates : Using
sed
to update the same text in multiple files.
This page is a decent place for me to put my collection of information like shell scripting one liners, or little nuggets of “this-isn’t-enough-to-warrant-an-entire-solo-page-but-is-important-enough-to-document” type of ites.
Using find
to chmod
multiple files
When you find yourself needing to change a plethora of items within the current working directory - but only the files or directories and not both - one way is to use find
find . -type f -exec chmod 644 {} \;
Break Down
find
- this is obviously the specific program/command here. It is an incredibly useful tool, but we’re only covering a small portion of its abilities here..
- the.
signifies we’re searching in the current working directory, or if you typedpwd
, its the same idea. You can change this to any location in your directory you so choose.-type f
Using awk
to Modify Output
When a program outputs information in a standardized way, like pip freeze
or pip list
, you can manipulate that output to fit your wants and needs.
So, lets take pip list
, below is a truncated output.
alabaster (0.7.9)
ansible (2.2.0.0)
appnope (0.1.0)
argh (0.26.2)
astroid (1.4.8)
Babel (2.3.4)
You can also see pip freeze
has its own means of outputting info. Each command has its own reasons.
alabaster==0.7.9
ansible==2.2.0.0
appnope==0.1.0
argh==0.26.2
astroid==1.4.8
Babel==2.3.4
But, lets say we want to pipe all of that to a pip Requirements File for easy updating/reinstallation. If you’re using said file for updates/upgrades, having the included version numbers would not help at all here, since pip install -r requirements
would install those specific versions.
So, what do we do?
pip list | awk '{ print $1 }'``
or
pip freeze | awk -F'==' '{ print $1 }'``
Remove text, like Commas
tl;dr
awk -F'[, ]' '{print $2}'
Where what is inside the []
is what you want removed.
Breakdown
the
pip freeze
andpip list
we’ve established.awk
- is a language in and of itself, as complicated and large and useful as a language as well.-F
- this says “use the following text inside the ‘ ‘ as the break point or escape character to seperate out all of the info.”\`{ print $1 }\`
- this tells awk to show the first column of information only.
If you were to say pip list | awk `{print $1,$2}`
you would get the original information once again. Why? becuase the (#.#.#) is $2
or option 2 or what have you. The ,
says “insert space”. Without the comma, no space.
Again, awk
is a massive language. This is a simple explainer here.
Using \
as New Lines
Often times, you’ll see \
used at the end of code lines and you’ve wondered what on EARTH thats about??
Well, those are used as so-called next line
signifiers, or on the naked command line, it tells the system to keep expecting more text/code input on the next line.
Shell Script Location
Are you wanting an easy way for your shell script to know where its at in the plethora of unix directories? Use the below line!
"$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
Its able to give you that info, no matter where its called from, which is a typical issue with almost all other so-called “one-liners”.
Random Number Generator
SLEEP="$(($RANDOM%3600+3600))"
Details
The $RANDOM
bash variable is a builtin variable for generating random numbers, random options between True and False, mimic rolling a dice, drawing cards, etc.
its a nice and simple way to use a randomized Sleep length, or anything else you want randomized thats not reliant upon anything security-wise. (Its no where near random enough for using with security needs)
Using sed
to Make Updates
If you are wanting to make the same change in multiple files, sed
is the way to go!
sed -i 's/brightyellow/,yellow/g' /usr/share/nano-syntax-highlighting/*.nanorc
or, if you are on a mac, you have to add ''
after the -i
, and before the text to replace.
sed -i '' 's/brightyellow/,yellow/g' /usr/share/nano-syntax-highlighting/*.nanorc
A great website to go look at for a plethora of how-tos is tldp_randomvar.