News

100 ways to master the command line – Part 2

Here's the second part of master the command line feature. Got any extra command line tips? Leave a comment below.

tip96

Search within files using the grep command to save time and find what you need

>_049 Search a file for a term

The basic use of grep is to search through a file for a specific term. It will print out every line with that term in, so it’s best to use it with system files with readable content in. Use it with:

$ grep hello file

>_050 Check for lines

Looking for specific lines in a file is all well and good, but when you then start to hunt them down and you realise the file is hundreds of lines long, you can save yourself a lot of time by getting grep to also print out the line number. You can do this with the -n option:

$ grep -n hello file

>_051 Regular expressions

If you need to make a more advanced search with grep, you can use regular expressions. You can replace the search term with ^hello to look for lines that start with hello, or hello$ for lines ending in hello.

>_052 Wildcards and grep

When searching for lines, you can use a wildcard if you need to look for similar terms. This is done by using a full stop in the search string – each full stop represents one wildcard character. Searching for h…o will return any five-letter string with h at the start of the string and o at the end. Use it like so:

$ grep ‘<h…o>’ file

>_053 More wildcards

You’ll also be using wildcards to find something ending or beginning with a specific string but with no fixed length. You can do this in grep by using an asterisk (*) along with the dot. In the above example, we would have used h.*o instead.

Some terminal tricks for devs to help your command line skills become more efficient

>_054 Stop a system service

A system service is the kind of background software that launches at start up. These are controlled by the system management daemons like init or systemd, and can be controlled from the terminal with the service command. First, you can stop a service using:

$ sudo service name stop

>_055 Start a service

You can start system services that have been stopped by using the same service command with a different operator. As long as you know the service name, start it using:

$ sudo service name start

>_056 Restart a system service

This one is popular with setting up web servers that use Apache, for which restarts may be needed, along with other services that you customise along the way. Instead of running both the stop and start commands sequentially, you can instead restart services by using:

$ sudo service name restart

>_057 Know the ID

The ID that you can get for the software with top can be used to manipulate it, but a common reason to know the ID is so that you can end the process if it’s having trouble stopping itself or using too many resources. With the ID in hand, you can kill it with:

$ kill 1234

>_058 Kill multiple IDs

Sometimes a process can be split up over multiple IDs (this is usually the case with a web browser – Google Chrome is a notorious example), and you need to kill multiple processes at once. You can either try and quickly kill all the IDs, or use the common name for the process and kill it with:

$ killall -v process

>_059 List connections

The standard command for listing your network connections and details in the terminal is merely ifconfig – this will list all of your interfaces and their statuses. If you just need to list a specific one, say for the wireless interface, you can use:

$ ifconfig wlan0

tip59

>_060 List USB devices

You may need to know which USB and USB-related devices are connected to a system, or find our their proper designation. You’ll need to install it first, but once you have, you can use lsusb in the terminal to list all of
the available devices.

>_061 List hard drives and partitions

Whether you need to check the designation of certain drives for working on them, or you just need to get a general understanding of the system’s layout, you can use fdisk to list all the hard drives. Do this in the terminal with:

$ sudo fdisk -l

>_062 Check running software

Sometimes you’ll want to check what’s running on your system and from the terminal this can be done simply with top. It lists all the relevant information you’ll need on your currently running software, such as CPU and memory usage, along with the ID so you can control it.

>_063 Unpack a ZIP file

If you’ve downloaded a ZIP file and you did it from the terminal or you’re working from it, you can to unpack it using the unzip command. Use it like so:

$ unzip file.zip

>_064 Unpack a TAR file

Sometimes Linux will have a compressed file that is archived as a .tar.gz, or a tarball. You can use the terminal to unpack these or similar TAR files using the tar command, although you need the right options. For the common .gz, it’s:

$ tar -zxvf file.tar.gz

>_065 Copy and write disks

Coming from UNIX is a powerful image tool called dd, which we’ve been using a lot recently for writing Raspberry Pi SD cards. Use it to create images from discs and hard drives, and for writing them back. The if is the input file or drive and the of is the output file of the same. It works like so:

$ dd if=image.img of=/dev/sda bs=1M

>_066 Create an empty file

Sometimes when coding or installing new software, you need a file to write to. You could create it manually with nano and then save it, but the terminal has a command similar to mkdir that enables you to create an empty file – this is touch:

$ touch file

>_067 Print into the terminal

The terminal uses echo to print details from files into the terminal, much like the C language. If you’re writing a Bash script and want to see the output of the current section, you can use echo to print out the relevant info straight into the terminal output.

>_068 Check an MD5 hash

When downloading certain files it can help a lot to check to make sure it’s downloaded properly. A lot of sites will offer the ability to check the integrity of the downloaded file by comparing a hash sum based on it. With that MD5 and the file location at hand, you can compare it with:

$ md5sum file

>_069 Run commands to x

Sometimes you need to do something concerning the x display, but the only way you can enter the command line is by switching to an alternate instance with Ctrl+Alt+F2 or similar. To send a command to the main x display, preface it with DISPLAY=“:0” so it knows where to go.

>_070 Create a new SSH key

When you need to generate a strong encryption key, you can always have a go at creating it in the terminal. You can do this using your email address as identification by entering the following into the terminal:

$ ssh-keygen -t rsa -C

“your_email@example.com”

>_071 System details

Sometimes you want to check what you’re running and you can do this with the simple uname command, which you can use in the terminal with the following:

$ uname

>_072 Kernel version

As part of uname, you also get the kernel version. Knowing this can be useful for downloading the right header files when compiling modules or updating certain aspects. You can get purely the kernel version by adding the -r option:

$ uname -r

>_073 CPU architecture

If you’re on an unknown machine, you might need to find out what kind of architecture you’re running. Find out what the processor is with:

$ uname -p

>_074 Everything else

Uname enables you to display a lot of data that is available from the system and you can look at all of this information by simply using the -a option with the command:

$ uname -a

>_075 Ubuntu version

With all the distro updates you do, it can be tricky to keep track of which version of Ubuntu you are on. You can check by using:

$ lsb-release -a

Learn how to view file permissions and then how to modify them properly

>_076 List file permissions

You can check the file permissions of every item, including hidden files and directories, in the terminal using ls -la. It will print out the file permissions as a ten-character string in the first column of output. The first character identifies the file type, with d indicating a directory and – indicating a regular file. We’re interested in the last nine characters, which are actually three sets of three and are interpreted differently. For example:

rwxr-xr-x

R stands for read, w stands for write and x stands for execute. If they’re present instead of a -, it means that it is present in that particular block of permissions. It’s split up over three blocks: the first three being the user you are currently using, the second being the group and the third being for everyone else.

>_077 Change permissions

With the permissions ascertained, you can start editing them if you want via the chmod command. You edit each of the three permissions set by assigning it a number that treats the three-bit permissions set as a binary. So you’d do something like:

$ chmod 777 file

The first number is for the user permissions, the second is for the group and the third is for everyone else. The numbers mean:

7: read, write and execute, 111/rwx

6: read and write, 110/rw-

5: read and execute, 101/r-x

4: read only, 100/r–

3: write and execute, 011/-wx

2: write only, 010/-w-

1: execute only, 001/–x

0: none, 000/—

You can control your tunes while working inside the terminal

>_078 Pause music

Some audio players have command line controls they can use. Rhythmbox has this and it’s a cool thing to use when you’re stuck in the terminal and need to just pause your music for a moment. You can do this by using:

$ rhythmbox-client –pause

>_079 Skip music

The command line controls don’t enable as much as you can get in the interface, however you can at least skip to the next track. Try it with the command below:

$ rhythmbox-client –next

tip79

>_080 Pause video

You can use Mplayer to launch video from the command line to watch. It’s good for testing a video with different settings that you can affix to the video-playing command. What you can also do is control the playing video with keys – specifically, you can pause by using the space bar.

>_081 More video control

Mplayer gives you a few more controls while playing in the command line. You can use Enter to skip to the next item in a list, and otherwise, you can stop playback by using Ctrl+C to end the process entirely.

All the other commands that you might want to know for future reference

>_082 Open files in terminal

If you’ve got a file you can see in a graphical file manager, instead of opening a terminal and navigating to and then executing the file or script, you can usually run it directly from the terminal. To do this you usually just need to right-click and select a ‘run in terminal’ option.

>_083 Find files in terminal

You can search for specific files throughout the filesystem by using the find command. You need to give find a location to search in and a parameter to search for. For simply searching for a file from root with a specific name you can use:

$ find / -name file

>_084 Locate files in terminal

Similar to find is locate, a newer tool that works slightly differently to find. While find also has ways to search for files by age, size, owner and so on, locate only really uses the name to locate, however it can do it so much faster. Use it with:

$ locate file

>_085 Current location

In the terminal you might find yourself a little lost in the filesystem, or want a quick and easy way to pipe your current location into something else. You can print out your current absolute location using pwd.

>_086 Move directories

When moving between directories you might want to be able to quickly return to the previous one that you were using. Instead of using cd for moving to the directory, you can instead use pushd to move and create a ‘stack’ of directories to move between.

>_087 Move back through pushd

Following on from Tip 86, once you want to start moving back up the stack to the first directory, you can use popd in the terminal. You can also check which directories you have stacked up by using the dirs command as well.

>_088 Process priorities

CPU priority for processes can be seen as running from -20 for highest priority or +20 for lowest. Putting “nice -n X” in front of any command enables you to change the priority from 0 to whatever number X is standing in for. Only sudo or root can elevate the priority of a process, but anyone can set one down the priority chain.

>_089 Download via the terminal

If you need to download something via the Internet in the terminal, you’ll want to use the wget command. It will take any URL you specify and download it directly into your current location in the terminal (as long as you have permission to do so). Use it like:

$ wget http://example.com/file.zip

>_090 Change image formats

Instead of loading up an image editor like GIMP, you can actually change images in the terminal using the convert command. You can very simply use it to change the filetype or even change the size. Do it with:

$ convert image.jpg image.png

>_091 Alter image settings

As well as convert there’s also mogrify, which is part of the same software package. You can use it scale, rotate and do more to an image more easily than with some of the convert commands. To resize you can use something like:

$ mogrify -resize 640×480! image.png

>_092 Send message to displays

This is an old school prank that can actually have good uses when done right. Xmessage enables you to send a message prompt to an x display on the same system, as long as you know the display you want to send it to. Try:

$ DISPLAY=:0 xmessage -center “Hello

World!”

>_093 Rename files with cp

This is an extension of the way you can use cp – cp will copy a file and name it to whatever you want, so you can either copy it to another directory and give it a different name, or you can just copy it into the same folder with a different name and delete the original. This also works for renaming with mv.

>_094 Manual screenshots

This is something we have to do during Raspberry Pi tutorials, but it works everywhere else. For GNOME-based desktops you can do it in the terminal by calling gnome-screenshot, in XFCE it’s xfce-screenshooter, in LXDE it’s scrot and so on. This will immediately take a screenshot of what you can see.

>_095 Delayed screenshots

With the functions from above, you can add a delay to set up a screenshot. This is useful if you want to show the contents of a drop-down menu, or need to pose a shot in general. All of the screenshot tools allow you to delay by adding the -d option and then a number of seconds, eg:

$ scrot -d 5

Easter eggs, gags and other attempts by patterless software engineers to be funny

>_096 Do the locomotion

The story behind this is that apparently after finding a lot of people misspelling the list command ‘ls’ as ‘sl’, this joke program was made to particularly hammer home that they were indeed not using the correct command. It plays the animation of a steam locomotive; sl.

tip96

>_097 What does the cow say

A Debian trick as part of apt-get, moo is an easter egg wherein you type apt-get moo and a cow asks you if you have mooed today. If you have, in fact, not mooed today then you should immediately find a quiet room and let out a single, loud moo to appease the terminal cow.

tip97

>_098 Let it snow

This one uses a Bash script to simulate snow in the terminal. We have added the file you will need to use to FileSilo.co.uk, so download it, cd to the location of the file and then run it. It’s completely pointless, especially in the middle of summer, but it can be relaxing.

>_099 What’s in a date

You can actually write date into the terminal and it will print out the actual date according to its own internal clock for you, which is very useful. What you can also do is use ddate to create a ‘discordian date’ that prints out an almost dystopian date system.

tip100

>_100 Crystal terminal

Our last tip is fortune. Call it to get a hint at what your day might involve. Hopefully it’s something inspirational to get you started. Remember though, it’s just a random output from a string of code. Enjoy!

×