Commands

Now in the 21st century computers have graphical user interfaces (GUI). However if something goes wrong, you can find yourself in front of a text console and now?

Additionally embedded devices can have Linux inside and offer a console! Dedicated PC's for servers, routers, or multimedia boxes should not have too much functionalities for reasons of security and maintenance effort, therefore a text console for maintenance might be the way to go.

It is not the idea here to give a complete list of all commands, this would create a book on itself and would not be very useful, since simply typing man <command> does it better. Knowing the commands, bash scripts can be written.

Midnight commander

The midnight commander mc https://midnight-commander.org/ is similar to the DOS Norton Commander.

It has it configuration files under /etc/mc and ~/config/mc

man mc and mc -h

To have mouse support start gpm and use a terminal that can deal with this as xterm

When working under X11, use terminal that supports the mouse as konsole.

To enable exiting to latest working directory, put this into your ~/.bashrc

 . /usr/libexec/mc/mc.sh

Type mcedit in a console to just open the mc's editor as standalone console editor

To have the mouse running in Gentoo turn on the slang useflag

Text Web browser Links

In text mode you can even browse around the Internet and download files using links the text based web browser. Within links press ESC to get the menu. An other text based browser is lynx. To download something from the web use wget<url>

cat

to show contents of a text file. Use cat instead of opening files in /proc

cat /proc/cpuinfo (shows you the bogomips of your computer)

Since such files are not real, they can crash a text editor => result text editor will never finish to open the file.

Copy this into a shell and type return

cat > hello.sh << "EOF"

#!/bin/bash

echo "Hello World"

EOF

Now you have a file hello.sh and run it:

bash hello.sh

Looking at the details, since no input file is given cat takes the console and routes it into the output file hello.sh until EOF is read.

cd

change directory.

cp

is the copy command. The

cp -arT <directory 1><directory 2>

copies directory 1 to directory 2 and makes an exact copy including subdirectories, links, owner rights.

Important options are:

-a (archive) same as --preserve=all

-r (recursive) to copy also contents of subdirectories

There is also rcp (remote copy available).

cut

Cut can be used to cut out text coming from a pipe. It can therefore be used to format things derived using grep. A line of text is considered to have text fields with the TAB delimiter. The -d option allows to use an other character as =. The -f <n> defines the field not being cut (n>0).

dd

Per default dd reads raw bytes from stdin and writes them to stdout and allows some formatting and conversion. The dd options if and of are passed to read from a file and write to a file instead.

Instead of reading a file also a device can be given, so commands as:

dd if=/dev/hda3 of=/backup/mybackup-07-July-08-hda3.bak.dd

write an entire partition and save them to a file.

A classic example is reading the master boot record:

dd if=/dev/hda of=/backup/mbr.bak bs=512 count=1

and restore it:

dd if=/backup/mbr.bak of=/dev/hda bs=512 count=1

Often dd is also used for other kinds, when the whole data has been read see MD5 checksum verification.

find

To find a file

find <path where to look> -name <filename>

But also other criteria than the file name can be selected. Look for files in the users directory that belong to root.

find ~ -group root

Look for files larger than 100MBytes

find ~ -size +100M

To see files in home directory that got modified within the last 10 min

find ~ -mmin -10

To see files created more than 1 year ago

find ~ -mtime +365

To see files accessed more than one year ago

find ~ -atime +365

And now a challenge

find /usr/bin -type l ! -xtype f ! -xtype d -ok rm -f {} \;

It looks in the /usr/bin directory and makes some tests, since no operators are given all test have and operators meaning all test need to be true. -type l tests if the file is a link. The following tests need their result to be inverted, this is done with the ! sign put in front. So ! -xtype f gets true if the link found by -type l does not point to a regular file and -xtype d if it does not point to a directory. Finally -ok<command> ; runs a command requiring the user to confirm it with a y. The {} characters are replaced by the file found. The \ character is an escape charter to prevent the shell to interpret the ; character.

grep

lets you print out just certain lines of a file. grep '<a word>' <filename>

Example find errors in /var/log/Xorg.0.log

grep -n3 "(EE)" /var/log/Xorg.0.log the option -n3 shows 3 rows before and after the row containing "(EE)"

or in a combination wit cat

cat /proc/cpuinfo | grep bogo

Or to find a text in files grep -rnw '<path>' -e '<text>'

The options are:

Table 2.1. grep

n number of lines before and after to be printed
C As n but without line number
A number of lines after to be printed
w Match whole words only


ls

to list the stuff in a directory.

ls -l or ls -la including hidden files

gives you a nice list with file details. The first character identifies what it is:

- regular file

d directory

l link

p pipe (or FIFO)

s socket

c character device

b block device

To list all files and directories that have the name ebuild and go recursive into the subdirectories:

ls -R Urs/ApplicationData/overlay/ | grep ebuild

This are two commands connected via pipe, the ls command prints everything out and the grep filters.

more or less

Commander Data from Star Trek does not need the programs more and less, since he is able to read faster than the computer puts text on the screen. He would type cat /var/log/Xorg.0.log and would be able to read and remember all the lines.

Regular human beings would miss the first lines, so they can type:

more /var/log/Xorg.0.log or less /var/log/Xorg.0.log. The command less is more advanced than the command more and therefore called less, it allows you to scroll back. Q quits from everywhere. Both more and less can be used with other programs as. An other example how to use less:

ls /dev -l |less

Note to the insiders: The program man is configured to use the program less.

An alternative is using a video camera and record a movie on the things that come to the screen too fast. It is the only way to get the text that BIOS puts to the screen when it boots.

mv

Move files or rename them. The -f option allows to overwrite files that have the same name.

rm and rmdir

Two different commands exists:

The command rm removes files what basically means delete them. A useful option is -i that asks before it does the delete.

To delete directories, the directories must be empty before rmdir removes them. There is the --ignore-fail-on-non-empty option to make the damage.

strings

strings - print the strings of printable characters in file. It is a hacker trick to find e.g. expected paths in programs.

strings /bin/bash | grep bashdb-main.inc

/usr/local/share/bashdb/bashdb-main.inc

Shows where bash expects the bashdb scripts to be.

tail

Tail can be used to see the last lines of a file. It is useful to observe what the message logger writes:

tail -f /var/log/messages

time

time <command> executes <commend> and returns the time it used.

uname

To see about your computer (Linux, cpu, ....) type:

uname -a

Linux sempron 2.6.21-gentoo-r4 #1 SMP PREEMPT Mon Jul 23 09:11:32 CEST 2007 i686 AMD Sempron(TM) 3000+ AuthenticAMD GNU/Linux

Or to see just the Kernel version

uname -r

2.6.21-gentoo-r4

and might be used in script programs dealing with the kernel.

watch

Watch executes a program periodically (default 2 seconds). Example to see live what is going on watch grep \"cpu MHz\" /proc/cpuinfo.


Linurs startpage