I often find myself mentoring young colleagues in the ways of Linux. If they’re coming from a Windows background or just haven’t had a lot of time in IT, there’s a lot to learn to really be proficient in Unix/Linux. You need to figure out an editor, how to move around the filesystem, where things in the filesystem, basic tools (cp, mv, ps, grep, etc.), how permissions work, pipelining commands, etc.
Once people are comfortable, they often enter a mind-expansion phases where they realize all the power they have at their disposal. As they enter their journeyman phase of using Linux, they’re getting into shell scripting, building little tools, learning regular expresisons, and maybe starting to learn languages like python or awk/perl. What was once someone who googled for answers blossoms into someone who reads the man pages.
At the advanced journeyman stage, I often share little tips and tricks that make life easier. Here are a few of my favorites.
Use Braces for Bulk Directory Creation
Did you know you can do this?
mkdir /some/path/{bin,doc,src,test}That will create:
/some/path/bin /some/path/doc /some/path/src /some/path/test
Get Rid of Backslashitis
If you write regular expressions and get tired of writing things like this:
sed 's/\/that\/is\/long/\/new\/path/'then I have very good news for you. Read the post.
The timeout Command
If you’re writing a script that does something like “connect to these 10 hosts and do X” and one of the hosts is down, the script can hang. But you can use the timeout command. Something like
timeout 10s /do/some/command
After 10 seconds, it will kill the command and error out.
Turn Any Command into a Stopwatch with time
Speaking of time, you can run a command like this:
time /some/command
and record how long it took:
# time ./purge_logs.sh real 0m2.608s user 0m2.490s sys 0m0.794s
Use grep -R --include to Narrow Recursive Searches
If you do a grep -R, sometimes you find it chewing through tons of files you’re not interested in. Want to only look at the .conf files in /etc? Try this:
grep -R –include=”*.conf” “listen” /etc
Record Your Session
Type
script session.log
and you’ll find every command you typed in session.log.
Setup Detachable and Reattachable Terminal Sessions
Did you know you can start a session, turn off the session, and later come back to it? Yup – check out tmux.
The Human Flag
Both du (disk used) and df (disk free) support a -h flag which translates the output into human-readable terms. Instead of blocks, get MBs, GBs, or TBs.
# df -h Filesystem Size Used Avail Use% Mounted on udev 3.9G 0 3.9G 0% /dev tmpfs 795M 524K 794M 1% /run /dev/sda1 19G 9.4G 7.9G 55% / tmpfs 3.9G 224K 3.9G 1% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 795M 0 795M 0% /run/user/0
Duplicate a Directory Perfectly with tar
# cd /SRC && tar cf - . | (cd /DST && tar xpf - )
Grab Random Lines From a File
Want some random log entries? Use shuf:
shuf -n 20 bigfile.log
























Leave a Reply