Linux Best Practices and Tips
Linux is powerful, flexible, and can be adapted to a broad range of uses. While best practices for administrating Linux servers are not hard to find due the popularity of the operating system, there is always a need for up-to-date Linux advice, along with the best tips, from our experienced Toptal Linux administrators.
How to Avoid Frustration After Forgetting To Use Sudo Command
<p>
Have you ever typed a command in your terminal, only to find out you forgot to prefix it with the
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
sudo
</div>
</td>
</tr>
</table>
</div>
<p>
command? You have to retype the whole command again just to add the
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
sudo
</div>
</td>
</tr>
</table>
</div>
<p>
in front of it. Frustrating!
</p>
<p>
Well, you can add this simple alias to your
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
.bashrc
</div>
</td>
</tr>
</table>
</div>
<p>
to help you reduce the frustration:
</p>
<pre>
|
1
|
Trello – backup to your linux machine
Just in case, you are looking for backing up your trello account boards, you can use the following bash script to do so:
#!/bin/bash -
#===============================================================================
#
# FILE: backup-trello.sh
#
# USAGE: ./backup-trello.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Amit Agarwal (aka)
# ORGANIZATION: Mobileum
# Last modified: Thu Dec 22, 2016 01:14PM
# CREATED: 08/12/2016 09:41:08 AM IST
# REVISION: $Revision: 1.0 $$
#===============================================================================
# Your backup directory
BDIR=/backup
# Your trello api token and key goes here :)
token=<>
key=<>
# IDs of the boards go here. This is easy to get, just go to your
# board and check the last part of URL
BOARDS=( a b )
URL="https://trello.com/b/"
POST='&actions=all&actions_limit=1000&cards=all&lists=all&members=all&member_fields=all&checklists=all&fields=all'
for i in ${BOARDS[*]}
do
/usr/bin/curl -H 'Accept-Encoding:gzip, deflate, br' -H 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' "$URL$i.json?&key=$key&token=$token$POST" > "$BDIR/$i.json.gz"
# gzip -f "$BDIR/$i.json"
done
Continue reading
change the output format for time command
If you are doing some scripting and using ‘time’ command, then you know sometimes it becomes difficult to capture the output as the output would be something like this:
: amit ; time ls real 0m0.002s user 0m0.000s sys 0m0.001s
So, it is better to change that format. Here is simple example:
|
1
|
- TIMEFORMAT=’real %3R user %3U sys %3S pcpu %P’ time ls
- amit ; TIMEFORMAT=’real %3R user %3U sys %3S pcpu %P’ time ls
- 0.00user 0.00system 0:00.00elapsed 0%CPU (0text+0data 2432max)
- 0inputs+0outputs (0major+109minor)pagefaults 0swaps
- amit ; TIMEFORMAT=’real %3R user %3U sys %3S’
- amit ; time ls
real 0.001 user 0.001 sys 0.000 - amit ;
- amit ; TIMEFORMAT=’TIMEOUTPUT = real %3R user %3U sys %3S’
- amit ; time ls
TIMEOUTPUT = real 0.001 user 0.001 sys 0.000 - amit ;
Generate random string for various use case
Some times I need random string, for example to use as email seperator or to use in some API. One way is to use tools like /dev/[u]random or od and other such. But they seem cubersome after I figured this out.
openssl rand <length> openssl rand 10
This alone without some parameters is not interesting thoug. You can use ‘-base64’ or ‘-hex’ to select the encoding.
So if you execute the above you will get something like this
Continue readingGet your local IP address like pro
In shell scripts if you need to get you local IP address corresponding to your hostname then you can use this command
hostname -i
Related articles across the web
image ordering by Original Date Time using bash script
Here is the script:
#!/bin/bash -
#===============================================================================
#
# FILE: imgOrg.sh
#
# USAGE: ./imgOrg.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Amit Agarwal (aka)
# REVISION: ---
#===============================================================================
for i in *
do
if [[ $(file $i) == *image* ]]
then
echo "Image file is :: $i"
dir=$( exiftool -s -DateTimeOriginal $i | awk -F':' '{print $2"/"$3}')
mkdir -p $dir
cp $i $dir/
else
echo "Excluding $i"
fi
done
Continue reading
Disk usage by file type
Trying to find the total usage for each of the file types by extension, then here is a quick bash function for you :
disk_usage_type ()
{
find . -name '*'$1 -ls | awk '
BEGIN{
a[0]="Bytes";
a[1]="KB";
a[2]="MB";
a[3]="GB";
}
{sum+=$7; files++;}
END{
print "Total sum is ::\t" sum;
print "Total files ::\t" files;
while (sum > 1024) {
sum=sum/1024;
count++;
};
print sum" "a[count];
}'
}
Just define the function in one of your bash startup files. After that to use the function pass in the extension for which you would like to find the total size. Output should be something like below:
Continue reading

