BasicCommands

Revision 32 as of 2006-01-04 02:46:07

Clear message

attachment:BasicCommandsSample.png

"Under Linux there are GUIs (graphical user interfaces), where you can point and click and drag, and hopefully get work done without first reading lots of documentation. The traditional Unix environment is a CLI (command line interface), where you type commands to tell the computer what to do. That is faster and more powerful, but requires finding out what the commands are." -- from man intro(1)

Using this page

  • This page will make you familiar with basic GNU/Linux shell commands.
  • It is not intended to be a complete guide to the command line, just an introduction to complement Ubuntu's graphical tools.
  • All command names will be in bold.

  • Commands needing to be typed will be in "bold with quotes".

  • All of the commands on this page are to be issued from a command prompt in a terminal.
  • To get a terminal window in Ubuntu Hoary: Applications menu - System Tools - Terminal
  • Note that Linux is case sensitive. User, user, and USER are all different to Linux.

Starting a Terminal in Gnome

In Ubuntu 5.04 (Hoary Hedgehog): Applications menu - System Tools - Terminal; or right-click on any empty area of desktop and choose "Terminal".

In Ubuntu 5.10 (Breezy Badger): Applications menu - Accessories - Terminal. To get the right-click Open Terminal option back in 5.10, search [:SynapticHowto:Synaptic] for nautilus-open-terminal and install it.

Commands

sudo: Executing Commands with Elevated Privileges

  • All of the following commands will need to be prefaced with the sudo command if you will be working with directories or files not owned by your account. Please see RootSudo for information on using sudo.

File & Directory Commands

  • pwd: The pwd command will allow you to know in which directory you're located (pwd stands for "print working directory"). Example: "pwd" in the Desktop directory will show "~/Desktop". Note that the Gnome Terminal also displays this information in the title bar of it's window - see the example screenshot at the top of this page.

  • cd: The cd command will allow you to change directories. When you open a terminal you will be in your home directory. To move around the file system you will use cd. Example: "cd ~/Desktop" will move you to your desktop directory.

    • To navigate into the root directory, use "cd /"

    • To navigate to your home directory, use "cd"

    • To navigate up one directory level, use "cd .."

    • To navigate to the previous directory (or back), use "cd -"

    • To navigate through multiple levels of directory at once, use, "cd /var/www" for example, which will take you directly to the /www subdirectory of /var/.

  • cp: The cp command will make a copy of a file for you. Example: "cp file foo" will make a exact copy of "file" and name it "foo", but the file "file" will still be there. When you use mv that file would no longer exist, as when you use cp the file stays but a new copy is made.

  • ls: The ls command will show you the files in your current directory. Used with certain options, you can see sizes of files, when files where made, and permissions of files. Example: "ls ~" will show you the files that are in your home directory.

  • man: The man command is used to show you the manual of other commands. To get a good understanding, use the example: "man man" this will give you the man page for man. See the "Man & Getting Help" section down the page for more information.

  • mkdir: The mkdir command will allow you to create directories. Example: "mkdir music" will create a music directory.

  • mv: The mv command will move a file to a different location or will rename a file. Examples are as follows: "mv file foo" will rename the file "file" to "foo". "mv foo ~/Desktop" will move the file "foo" to your Desktop directory but will not rename it. You must specify a new file name to rename a file.

    • To save on typing, you can substitute '~' in place of the home directory.
    • Note that if you are using mv with sudo you will not be able to use the ~ shortcut, but will have to use the full pathnames to your files.

System Information Commands

  • df: The df command displays filesystem disk space usage for all partitions. "df -h" is probably the most useful - it uses megabytes (M) and gigabytes (G) instead of blocks to report. (-h means "human-readable")

  • free: The free command displays the amount of free and used memory in the system. "free -m" will give the information using megabytes, which is probably most useful for current computers.

  • top: The top command displays information on your Linux system, running processes and system resources, including CPU, RAM & swap usage and total number of tasks being run. To exit top, press "q".

  • uname -a: The uname command with the -a option prints all system information, including machine name, kernel name & version, and a few other details. Most useful for checking which kernel you're using.

  • lsb_release -a: The lsb_release command with the -a option prints version information for the Linux release you're running, for example:

user@computer:~$ lsb_release -a
LSB Version:    n/a
Distributor ID: Ubuntu
Description:    Ubuntu (The Breezy Badger Release)
Release:        5.10
Codename:       breezy

Adding A New User

  • "useradd newuser" command will create a new general user called "newuser" on your system, and to assign a password for the newuser account use "passwd newuser".

Options

The default behavior for a command may usually be modified by adding a --option to the command. The ls command for example has an -s option so that "ls -s" will include file sizes in the listing. There is also a -h option to get those sizes in a "human readable" format.

Options can be grouped in clusters so "ls -sh" is exactly the same command as "ls -s -h". Most options have a long version, prefixed with two dashes instead of one, so even "ls --size --human-readable" is the same command.

"Man" and getting help

Warning /!\ command --help and man command are the two most important tools at the command line.

Virtually all commands understand the -h (or --help) option which will produce a short usage description of the command and it's options, then exit back to the command prompt. Try "man -h" or "man --help" to see this in action.

Every command and nearly every application in Linux will have a man (manual) file, so finding them is as simple as typing "man "command"" to bring up a longer manual entry for the specified command. For example, "man mv" will bring up the mv (Move) manual. Move up and down this entry with the arrow keys, and quit back to the command prompt with "q". "man man" will bring up the manual entry for the man command, which is a good place to start!

"man intro" is especially useful - it displays the "Introduction to user commands" which is a well-written, fairly brief introduction to the Linux command line.

There are also info pages, which are generally more in-depth than man pages. Try "info info" for the introduction to info pages.

Searching for man files

If you aren't sure which command or application you need to use, you can try searching the man files.

  • man -k foo will search the man files for foo. Try "man -k nautilus" to see how this works.

    • Note that this is the same as the apropos command.

  • man -f foo searches only the titles of your system's man files. Try "man -f gnome", for example.

    • This is the same as the whatis command.

More Information

  • HowToReadline - information on some more advanced customization for the command line.

For more detailed tutorials on the Linux command line, please see:

Head back to UserDocumentation

CategoryDocumentation