| Saturday May 28th 2016

More Linux tips every geek should know

Auto-correct typos
army.mil hackTyping on the command line isn’t easy. First, it takes a lot of time to learn how all the commands work, but then even after that you need to be very precise with your file and directory names, otherwise you’ll need to try and try again.

But there’s a way out: Bash has a built-in command called “shopt” that lets you set various command-line options. For example, running shopt -s cdspell enables automatic typo correction for directory names, so that typing cd /hom/user will get you to the nearest match – /home/user

You can also use shopt -s nocaseglob so that when you type part of a filename and press Tab to auto-complete, Bash does a case-insensitive search.

Sequential command history
Apparently computers are here to make life easier, but that’s news to us – all too often we find people executing the same boring repetitive sequence of commands to get a job done, rather than think a little smarter and really flex the full power of Bash.

For example, if you want to run the last three commands repeatedly, just scroll up to the first one in the sequence, hold down the Control key and tap O as many times as you need. Each time you tap O, one command in the sequence will be executed, and when it hits the last command it goes back to the first one you selected and repeats.

Make working as root safer
Working with root is either great fun or extremely dangerous, depending on how much you like the feeling of supreme executive power.

But if you’re one of the people who fears the mighty power of the superuser to delete files with impunity, what you need is chattr: it lets you set a file as being immutable, which means that not even root can delete it. For example: running chattr +i myfile.txt as root will make that file virtually indestructable no matter what user you are. If you want to remove the shield of steel, use -i rather than +i.

Selective deletion
If you have a directory that contains ten subdirectories and you want to delete three of them, the slow way to do it would be like this:

rm -rf /home/hudzilla/work
rm -rf /home/hudzilla/projects
rm -rf /home/hudzilla/sandbox

But that’s pretty darn slow and open to making mistakes – a much smarter way to is to let Bash perform multiple filename expansion by placing the options inside braces. For example, this would achieve the same as the three lines from above:

rm -rf /home/hudzilla/{work,projects,sandbox}

Locate with style
Everyone loves the locate command, because as long as your index is up-to-date, there is no faster way to find files matching a certain name. Of course, the problem with locate is that it just lists filenames, which is no good if you want more information on those files, such as how much space they take up.

Well, why not try piping the output from locate through ls? For example, this neat little one-liner will find files matching somefile, then pipe that list through ls -lsh so you’ll see all the matching results as well as how big they are on your system:

locate somefile | xargs ls -lsh

Silence the beeps
Sick of your computer speaker beeping like a demented Morse code machine whenever you type something wrongly? Teach it the sound of silence: run the command setterm -blength 0 to mute the alarm bell no matter what kind of terminal you’re typing into. If you want it to happen every time you start a terminal, just add the command to your .bash_profile file.

Cutting text
Awk is a powerful beast for extracting information from lines of text, often far more than is needed. The cut command does the same sort of thing more easily.

cut -c1-10

returns the first ten characters from each line of standard input, while

cut -f 3 -d ' '

returns the third field, where the delimiter for a field is set to a space by -d, so it returns the third word. Cut has more options, but all are simple to use.

Checking your aliases
Some distros ship with a wide selection of aliases, with SUSE often leading the pack. If you want to find out just what Bash is up to behind your back, you can query your aliases by using the type command, like this: type -all ls – that will ask Bash to list precisely what it considers ls to be, and you’ll probably find that ls is aliased to something like ls –color=auto and of course also that it lives as a real program in /bin/ls.

Suppress annoying error messages

Error messages from command line applications can occasionally be helpful but are more often just a nuisance, especially when they swamp the ‘good’ output from a command.

You can easily suppress error messages by redirecting the standard error stream to the pseudo-device /dev/null using the notation 2> /dev/null. Here’s an example using the find command:

$ find / -user chris 2> /dev/null

Get at a DVD virtually
Sometimes even Linux Format’s magical powers can’t squeeze every distro onto a disc in bootable format, which means that sometimes we need to give you ISO files containing the latest and greatest.

These ISOs are great if you have a CD burner and want to install the distro sharpish; however, sometimes you just want to nab a few of the snazzier looking wallpapers (Fedora Core 7’s balloons, anyone?), in which case your best bet is to mount the ISO as a filesystem, then copy off whatever you want, like this:

mkdir /mnt/myiso
mount -o loop -t iso9660 some.iso /mnt/myiso

Turn off the coloured output from ls

In many current Linux distributions, the ls command displays different file types in different colours – directories in bilious blue, symbolic links in nauseous turquoise, and so on. This is because there’s an alias defined for ls that turns on this feature.

To turn it off for one specific shell, type the command unalias ls. To turn it off permanently, just add that command to the .bashrc file in your home directory, so that all your bash shells will see it as they start up and not blind you with garish colour.

Instant spelling suggestions
Looking to reject the GUI life altogether and restrict yourself to the command line like a real Unix geek? Great! But as soon as you miss the OOo spellchecker, don’t fret: most Linux systems come with the look command built-in, which is a command-line spellchecker. To get started, type look followed by part of a word:

look separ

should show you matches like “separate”, “separately”, and so on, whereas entering:

look seper

will show nothing, because “seperate” is a mis-spelling.


Related Posts: On this day...

Leave a Reply

You must be logged in to post a comment.