linux

Ubuntu Linux shell tips part I

Reading time of 1655 words
8 minutes
Reading time of 1655 words ~ 8 minutes


Did you find this article helpful?
Please consider tipping me a coffee as a thank you.
Ko-fi Buy Me a Coffee
Did you find this article helpful? Please consider tipping me a coffee or three as a thank you.
Tip using Ko-fi or Buy Me a Coffee

I love the Linux shell. While cryptic and horribly daunting for a first time user the text only command line offers an amazing amount of flexibility and power without the need to actually develop or code. Yet unlike a graphical user interface of which I don’t tend to like in Linux. The text shell is fast and allows you to remotely connect to a headless device with the bare minimum of configuration.

Take the Raspberry Pi, it only needs a plugged-in Ethernet cable and a micro-USB power cable. And this credit-card size computer can be controlled with Linux shell from your favourite Windows PC, Mac, tablet or mobile phone.

This series of entries is going to be a run-down of most of the useful Linux shell commands and configuration options that I use daily on my Linux boxes and images.

Understanding the Prompt

ben@developers-tidbit:/$ _

ben Is the logged in user.

@ At symbol.

developers-tidbit Is the server name. On some systems the server’s IP address maybe listed.

: Divider.

/ Is the current path. ~ Means the current path is the home directory.

$ Dollar states that the user is a standard user. It is replaced by a hash # if the user is a root administrator.

_ Is the cursor to show the point of keyboard input, text placement.

Linux supports auto-completion at prompt, so cl Tab+Enter will finish the rest of the command clear and launch it for you. Auto-complete also works with directories, files and apt-get repositories. Everything in Linux is case-sensitive so this means Clear, clear and CLEAR are all treated as different commands.

The prompt also keeps a history of the previously used commands. The up and down arrow keys cycle through these.

Directories

cd Changes the active directory.

cd / Goes to the root of the file system. Like the C:\ on a Windows system.

cd ~ Goes to your personal home directory.

cd /var Goes into the directory var.


ls Lists information about the files and is probably one of the more useful tools in the Linux toolbox.

ls Lists the active directory.

ls / List the root of the file system.

ls /var List the content of the directory /var.

ls -l / List the root of the file system using a long detail format.

ll Ubuntu has an alias (a short-cut) for the ls -l command.

ll

ls -lh / Lists the root of the file system using a long detail format that humanises the file size. Alternatively ll -h / will display the same results.

To only list the directories of the active directory a hack will be required. It lists all the items returned by the ls command that end with a forward slash / directory tag.

ls -d */ List the directories of root.

ls -d /var/*/ Lists the directories of /var.

Chaining Commands; Running Multiple Commands in Sequence

The semicolon character ; allows the running of multiple commands in a sequence. The following changes the active directory to home, lists the content using a detail list, then changes the active directory to root.

cd ~ ; ls -lh ; cd /
ls -lh

Viewing Text Files

Linux is highly dependant on plain text files for its configuration and settings so it would be natural that it includes many tools to handle these. The few common tools to display the content of a text file are cat, more, less.

cat Is short for concatenate and prints the file on-screen.

more Operates the same as cat except that it pauses the printing of the file at the end of each page.

less Operates the same as cat except you can use the up / down arrow keys or the Pg Up / Pg Down keys to scroll through the document.

To quit from both less and more press the Q key.

cat /etc/mime.type
more /etc/mime.type
less /etc/mime.type

To clean the screen use the clear command.

To display multiple files in a sequence more than one file can be supplied to cat.

cat file1 file2 file3

Another useful choice is the -n option that appends line numbers to the file in view.

cat -n /etc/mime.type

To remove the display of double-line spacing use the -s option.

The head tool allows the partial display of a file.

head -n [number of lines to display]
head -n 29 /etc/mime.type

The use of a negative number will display the complete file except the last X lines.

The tail tool is the reverse of head, it displays the footer of a file.

tail -n 3 /etc/mime.type

Tail has as a really useful option that allows you to follow the most recent changes that have been append to a text file. This is an excellent tool for reading only the most recent entries in a large log file. You may need to press Ctrl+C to exit tail.

cat /var/log/syslog
clear
tail -f /var/log/syslog

Some configuration files such as the Linux user and group settings consist of comma-delimiter data. To extract data from these files you can use the cut tool. By default cut uses the tab character as a delimiter but this maybe changed with the -d “[delimiter]” option. The -f option with a list or a range of numbers provides the columns for display.

cut -d ":" -f "[columns]" [file]

Lists the first 3 columns in the group settings file.

cut -d ":" -f "1,2,3" /etc/group
cut -d ":" -f "1-3" /etc/group
cut -d ":" -f "1,2-3" /etc/group

To search within a file the grep tool is most useful.

grep "[search term]" [file]

A few grep options. -i Will ignore letter cases in the [search term], so “Abc”, “abc”, “ABC” will be considered matches for grep “abc” [file] -c Returns a count of the number of search term matches rather than a summary of the finds. -n Includes line numbers in the summary of the finds. -m [limit value] Only returns the first X number of results.

grep -i "system v" /var/log/boot.log
grep -in "system v" /var/log/boot.log
grep -inm1 "system v" /var/log/boot.log
grep -ic "system v" /var/log/boot.log

In-built Help

Linux shell has a number of help tools while you are in terminal. The quickest way to discover what a command’s purpose is the use of the whatis tool.

whatis more

Some terminal commands include a basic usage help which appears when you run the command with no options.

more

Many commands have an extended help which is trigged with the –help option.

whatis --help

Finally most commands have usage manuals that are accessible at any time with the tool man [command]. Reading manuals through the terminal is pretty tedious so fortunately you can find these same texts online at http://manpages.ubuntu.com/manpages/. Use the website’s search box to find the tool’s manual.

man more

Or read it at http://manpages.ubuntu.com/manpages/precise/en/man1/more.1.html

Piping and Input/Output redirection

This may sound a little abstract but it is easier to grasp when explained. Linux shell gives you the very powerful ability to take the output of one command and feed it into another.

ls --help

With the ls --help there is more text then what the terminal can display. A pipe using the | character will feed the output text into a secondary tool such as less or more. To exit either tool press the Q key.

ls --help | more
ls --help | less

It’s not just help text that you can pipe. Any text output could be fed into another tool if it is supported.

ls -lh / | more

Output text can also be saved to a file using the greater-than > character. In the example below the output of ls is saved to result.txt that will be created in the Linux temporary directory.

ls -lh / > /tmp/result.txt
ls -l /tmp/result.txt
less /tmp/result.txt

The greater-than > character will always overwrite an existing file with the new text content. Double greater-than >> will append the text to the end of the file.

echo "Hello world." > /tmp/hi.txt
cat /tmp/hi.txt
echo "Test 1, 2, 3." >> /tmp/hi.txt
cat /tmp/hi.txt

The less-than < character will redirect the file content into a command.

less < /tmp/result.txt

Easy Text editing

Like in less you can use the arrow keys or Pg Up / Pg Down to scroll through the text. The left and right arrow keys can reposition the cursor. Any text typed will be inserted at active position of the cursor.

To cut a line of text press F9

To cut a paragraph of text use F9 multiple times.

To paste text press F10

To go to row position press Ctrl+_

For a help that unfortunately is slightly cryptic press Ctrl+G. In the help text the ^ symbol means the Ctrl key while the M- symbol means Altkey.

To exit nano press Ctrl+X

When you exit nano you will be asked to Save modified butter (ANSWERING “No” WILL DESTROY CHANGES) ? To save the changes to file type Y otherwise type N and the file will remain unedited and any changes lost.

Piping output text into nano doesn’t work as expected.

ls -lh / | nano

Nano expects to read text sourced from a file. To force nano to read text from standard input (aka stdin which is the Linux term for piped or streamed text) a dash is used for the input file name.

ls -ln / | nano -

To get some basic statistics of a text file the wc word count tool is useful. wc without any options displays the number of lines, words and the byte count for the file. The -w option counts the words, the -m counts the number of characters while the -l counts the number of lines.

wc /proc/cpuinfo
wc -w /proc/cpuinfo
wc -m /proc/cpuinfo
wc -l /proc/cpuinfo

That is the end of Part I of my Linux shell tips. The 2nd part that will be online at a later date will cover administration, file, network system and user tools.

Written by Ben Garrett

Did you find this article helpful?
Please consider tipping me a coffee as a thank you.
Ko-fi Buy Me a Coffee
Did you find this article helpful? Please consider tipping me a coffee or three as a thank you.
Tip using Ko-fi or Buy Me a Coffee