bash snippet

We can discuss for ages about which parameters should be default or not, but here are some basic commands with parameters I would use a lot

mkdir -p /tmp/some/sub/directory/please

which will make the entire path, instead of not making the full path, if any sub-directory wouldn't exist yet

cp -p source target
or
cp -v source target
or
cp -pv source target

The P is to keep meta-file info like timestamp when copied,
the V to display what is being copied.
I do hate commands that have NO output, that should in my opinion never be a default behavior. For that, the option is usually called "silent".
But as said, we can discuss for ages on what should be default behavior or not. Some cases are clear, some are not.

But, if you often use a given parameter, that could be a sign a wrong default choice has been made.
 


Display the number of open TCP connections per IPv4 (I often use this to detect brute forces or stupid bots):
Code:
netstat -ntu | awk '{print $5}' | grep '\.' | grep -vE '^(127.0.0.1)' | sed 's/:.*//' | sort | uniq -c | sort -n
 
Display the number of open TCP connections per IPv4 (I often use this to detect brute forces or stupid bots):
Code:
netstat -ntu | awk '{print $5}' | grep '\.' | grep -vE '^(127.0.0.1)' | sed 's/:.*//' | sort | uniq -c | sort -n


And then 'whois' on the resulting IP addresses ?
correct ?
 
Execute command and write result to log file at the same time. Useful if, for example, you start a command in the screen and also want to have the result as a log file.

Code:
<COMMAND> 2>&1 | tee -a <LOGFILE>
#Example
ping6 12-u.vs2-free-users.de 2>&1 | tee -a log.txt
 
If you want to view the content of a file but also see all latest changes to a file (if a file is actively being written to):

cat file.log ; tail -f file.log

CTRL-C to end the tailing

Often I would start tailing, but the time you submit the TAIL command, a lot may have already been written to it.

Note that in the above command, there may be duplicate lines, if the writing to the file is slow.
If it is extremely fast, content may be missing as well, between the CAT and the TAIL command.

Also note that if the file is very large, 100 Meg, 1 Gig, 5 Gig ... it's better to not CAT a file, cause it will take quite some time. In that case, this will do:

tail -n 100 file.log ; tail -f file.log

The number specified may be different, depending on how fast lines may be written to it.
 
Last edited:

Members online


Top