Unix Commands

Turns out there just isn't a fancy drag-and-drop, user-friendly way to customize and build all of the software we will need for sensor networks and embedded controllers. Often, we will need to use old-fashioned text commands, interacting through a "shell" or "console" or "terminal". You can't be expected to know all the ways of working with these older-style interfaces. Here are some tips on the commands used most often.

Shell vs Program

When you type something on a terminal, you are in conversation with some program, which is reading your keystrokes and making other things happen in response. Of all such programs there are two categories:

  1. The shell program. Examples are "bash", "csh", "ash", "zsh", "ksh" --- and there are many more besides. The shell program has conventions for keystroke abbreviations, sometimes autocompletion (type a tab midway through typing a command and the shell program may guess the rest for you), backspaces, redo, history of previous commands, and so on. When you type in something that is not a shell command, then the shell program will look for some system program to take care of what you typed.

  2. A command program. Examples are "cp", "ls", "vi", "more", and hundreds more. Each of these is actually a program. If you try the command "which cp" you will probably get the response "/usr/bin/cp" which just tells you that there is an executable file (/usr/bin/cp) that the shell invokes when you type "cp". After you type the name of a command program, then that program takes over control -- it may ask you for more input, in which case the keystrokes will go to the command program, instead of the shell. In typical unfriendly fashion, the terminal doesn't provide an indication to you whether you are typing keystrokes to the shell or to a program.

A Beginnning List of Commands

Now for some commands. All of these, with the exception of "cd", are command programs.

location commands
these commands either change the current directory where the shell is looking for files, or show you the current directory of the shell, or show you what's in a directory:
change directory, as in "cd /tmp" changes to the "/tmp" directory
display the current directory
the command "ls" shows you what's in the current directory; "ls -l" shows you the same thing but with more information; "ls -al" shows you even more, including the usually non-visible things that start with a period (.) character. Also, you can "ls /tmp" or "ls /usr/local" to see information about things not in the current directory.
moving and copying
commands to move or copy files and entire directories:
copy a file or directory, as in "cp mydata.txt /tmp/mydata.txt" --- the source location is first, the target second; you can omit part of the name if the target is a directory and the source is a file: "cp mydata.txt /tmp". To copy a directory, you need to specify the recursive copy option: "cp -r mydir /tmp/otherdir".
move a file or directory, as in "mv mydata.txt /tmp/mydata.txt"; similar to cp, you can omit part of the name if the target is a directory: "mv mydata.txt /tmp/". No need to specify recursion if you're moving a directory: "mv mydir /tmp/otherdir" or "mv mydir /tmp" will do.
creating, deleting
output to a file can usually just create the file if it isn't there, otherwise it overwrites the old copy. There are also these commands:
to create a directory, as in "mkdir Jan" creates Jan as a subdirectory of the current directory; "mkdir /tmp/X" creates X as subdirectory of /tmp.
to delete an empty directory.
to delete a file, as in "rm mydata.txt". You can also use wildcard characters, as in "rm mydata*" to remove all files beginning with mydata. To delete a directory and everything within it, the command "rm -r somedir" can be used. The ultimate killer delete command is "rm -rf *". There's no undo for this!
outputting content
commands to output what's in a file are mainly used to show things on the terminal:
"cat myfile.dat" will output the contents of everything in myfile.dat, by default on the terminal. If you do this with a non-text file, it may put the terminal in some strange state where characters no longer display properly. The shell command "reset" is your way out of this predicament. You can also use wildcarding with cat, as in "cat myfile*" to see the contents of all files that start with myfile.
"more myfile.dat" is like cat, but nicely pauses after each screenful of output. The more command is typically used with piping, for instance "ls -al /tmp | more" shows you all of what is output from "ls -al", but pausing for each screenful.
"less myfile.dat" is like more, but more advanced. It includes line-by-line scrolling forwards and backwards, character-by-character scrolling horizontally, as well as forward and backward search (by typing '/' or '?', respectively, followed by your search term and enter).
since unix is inherently a multiprocessing system, sometimes you need to keep track of what's going on.
the command "ps" tells you what are the currently running processes; "ps -ef" gives more output.
attempts to stop and remove a running process, by its id number. Example "kill 1097" will try to stop and remove process number 1097; "kill -9 1097" says the same thing, but more strongly.
not available on really lean systems, this commands does kill by name of what is running rather than process number.
volumes of data
unix has this concept of "mounted" areas of filespaces; though "/" is always the root file system, there can be other mounted file systems.
shows you what is mounted and how much space is being used.
attempts to mount a device or area of a device at a "mount point" (which is nothing more than a directory name).
unmounts a currently mounted device or area of a device -- if you fail to do this, the file system on that device could get messed up.

There are probably others that will turn out to be useful. Let experience be your guide as you assemble your own list of what works best for you.

Compressing, Backups

Lots of compression facilities run under Linux, including zip, gzib, bzip2, compress, and one of the newest, xz. (Note: for media like video and music, there are better ways to compress since compression for these can be lossy, which is why mp3 files can be so small.) Here are just some compression/decompression commands:

used to compress a file, as in "gzip myfile.txt". This typically replaces the file with a new, hopefully smaller file named "myfile.txt.gz".
does the reverse of gzip, that is, "gunzip myfile.txt.gz" decompresses and renames.
creates a zip archive.
extracts from a zip archive.
like gzip, but does more compressing and takes longer (maybe don't use if your CPU is slow)
does the reverse of bzip2.

For backups of file systems or parts of file systems, it is convenient to convert a directory and all its contents into a file. The tar (file format) is a standard way of doing this. The tar program has lots of complex options and can do many flexible things, so rather than explain it all, it's best to see a few examples:

File Transfer, Remote Login

Some commands are important for embedded linux because the target operating system is built on a desktop computer (more powerful), then transferred over to the embedded machine. Here are some noteworthy commands:

ftp or tftp

these are commands for file transfer; in modern Internet usage, they are mostly obsolete because browsers take care of the "ftp://..." type of URL, and because they offer no security. But, for setting up an embedded machine, having something lightweight and simple wins over security, at least initially.

the secure way to transfer a file, but it does slow things down due to encryption overhead.
the command to log in (without security) to another machine, provided that machine has a telnet "daemon" running.
the command to log in securely to another machine, provided that machine has an sshd server running.
  • Note: sometimes, especially when you build and install systems, you see some alarming message when you try to use scp or ssh, warning you of a possible break-in, when you know this cannot be possible. The remedy is to purge your current cache of the keys for other machines, because one or more is stale: "rm ~/.ssh/known_hosts" and retry.

Where is it?

When you have lots of files and directories, information overload can be a problem. Linux has some commands to help you find things.


this command can be used to search through the content of files and directories; this can be VERY SLOW, but sometimes you need it. For example,

     egrep -r "Creat*dev" /var
looks through all files, subfolders, etc (recursively, that's what "-r" means) for any file containing "Creat" followed by some characters, then "dev" in some string.

looks for files with some desired characteristic, like having a certain kind of name, a modification date, and more. Here's a command to find any file ending with ".txt" in the /tmp directory or any of its subdirectories:

     find /tmp -name "*.txt" 

only on desktop systems (so far as I know), this command uses a recent database of filenames and directories to find something by its name: it's fast, but of course it is searching a database possibly out of date. To refresh the database, the command "updatedb" can be run (as root, of course). Here are two examples of locate:

     locate sysparm
     locate mp3 | egrep india 
The first example finds any file or directory containing sysparm in its name; the second example files any file or directory with mp3 in its name, but the piping to egrep then trims the results further to show only things with india also in the name.

What is it?

Online documentation is provided for virtually every command using the man program. For example,

  man locate 

will give you the man page for the locate command, detailing all of the known options for the program.

There is another documentation system you may come across called texinfo. You access this with the info command. Usually man pages will indicate if there is an info page for the command.

Going Beyond Root


this command is used to create a new user on the system. For example,

    useradd joeschmoe 

creates a new user called 'joeschmoe'. You probably want to include the command line option -m so that useradd will create joeschmoe's home directory if it doesn't exist.


After you create a user, you will need to create a password for her. To do this, type

    passwd joeschmoe 
and follow the prompts.
/etc/shadow, /etc/passwd
Linux stores encrypted passwords in /etc/shadow. It used to store them in /etc/passwd, but now that file is used only for other user information. /etc/shadow is readable only by root; /etc/passwd is readable by everyone.

As root, you can become any other user with the command su. For example,

    su joeschmoe 

lets you become joeschmoe. sudo lets you run a single command as root after prompting for the root password, but in order to use it a user has to be explicitly allowed to do so in /etc/sudoers.


File permissions in Unix works by associating permissions with every file corresponding to the owner, group, and everyone else. You can change them by using chmod. For example,

    chmod a+x ./filename 

gives permission to all users ('a') to execute ('x') filename.

    chmod o-r ./filename 
removes read ('r') permissions from everyone other than the owner and group ('o') associated with filename. If you want alter owner permissions, use the letter 'u', and for group permissions use 'g'. Use 'w' for write permissions, '+' to add and '-' to subtract. (Note: there is a more complicated syntax for chmod where you specify the bytes directly by giving a number in octal, but you can check the man page if you want to know more about that.)

allows you to change the owner of a file. For example,

    chown joeschmoe ./filename 

gives joeschmoe ownership over filename. You can also change the group of a file like so:

    chown :joesgroup ./filename 

and you can change both at once like so:

    chown joeschmoe:joesgroup ./filename 


[ TO-DO: the "sh" command and simple scripts ]


[ TO-DO: commands like "gcc", "make", "ldd" ]

unix commands (last edited 2014-05-25 18:15:48 by localhost)