Some CLI Basics

From openSUSE

Contents

Introduction

A Command-Line Interface (CLI) is the traditional way to interact with an operating system. Many operating systems (even modern ones) have a CLI, such as DOS, OS/2, Windows, and Linux. A CLI mean is you type a specific commands into a command prompt to perform a specific action. Some things can only be done at a command-line and a lot of the time a command-line is quicker than the graphical way.


Legend

  • ~ = users home directory
  • / = Unix Delimiter symbol

How does a Linux command-line differ from a Windows command-line?

The first thing you will notice is that there is no drive letters in Linux like there are in Windows. Because Windows and DOS use drive letters, their command-line's start with the drive letter you're currently sitting on (eg. C:\> ). A Linux command-line will goto the current users home directory and will not have any drive letter. A command-line in Linux follows the user@host convention, because in a Linux based system disk drives and hard disk partitions are mounted on Linux's root partition. In normal directory's (these directory's are real directory's) and are not assigned any drive letters like each hard disk partition (and disk drive) would be in Windows.


The current directory concept

This concept is the very same in Windows, in that if for example you are currently sitting in the WINDOWS folder on drive C the command-line would look like this: C:\WINDOWS> Therefore WINDOWS is your current working directory because that's the folder (directory) you are sitting in. So you still need to remember which folder you are sitting in when using the command-line, regardless of which system you are using.

The relative and absolute path concept

Which ever folder you are currently sitting in, is the folder all commands you run will be applyed to. Let's say you are already sitting in the folder you want apply the particular command to, you would use a relative path and just type the file or folder name you want to modify in some way. An absolute path is the whole path to a file or folder, like for example the cp command's full path: /bin/cp This concept is the very same in Linux.

Command options/switches

Like in Windows, DOS as well as many other systems most Linux command-line commands have extra options one can use with them. Command options basically narrow or widen the scope of the command. In Windows and DOS (as well as many other systems) command options are usually indicated by the forward slash eg. dir/w Most Linux and Unix commands are indicated by a space and then - , for example ls -l which lists all files and folders with security permissions.


A Linux command-line (from a terminal window) and a Windows XP command-line

  • A Linux terminal shell window - current working directory/folder is my home directory

Image:term1.png

  • A Windows command-line - current working directory is the James folder

Image:wincmd.PNG


A table of some basic Windows and Unix/Linux commands

This is only a very small selection of commands that are available for OpenSUSE 11.x Note the extra 3rd column, the reason for that is because some commands don't exist in all Linux/Unix distributions.


Command Description Windows Command/Program Unix/Linux Command/Program Unix Command common in most distros
Changes the current working folder cd cd Image:22px-Tick.png
Creates folders md, mkdir mkdir
Copy's files and/or folders copy, xcopy cp Image:22px-Tick.png
Moves files and/or folders move mv Image:22px-Tick.png
Renaming files and folders rename mv Image:22px-Tick.png
Delete's files and/or folders del, rd rm Image:22px-Tick.png
Changes the current user account to a different user runas su Image:22px-Tick.png
Adding user accounts Windows Control Panel useradd
Changing the system settings regedit, Windows Control Panel yast, yast2 (for the YaST GUI) -- Yast is SUSE Linux distro only (OpenSUSE and SLED)
Monitoring processes taskmgr (Task Manager) top Image:22px-Tick.png
Monitor your network connections netstat netstat Image:22px-Tick.png

How to bring up a Linux command-line

  • Press ALT+CRTL+F1 and login to the virtual terminal
  • In KDE goto the K Menu (SUSE menu) > Applications > System > Terminal > Terminal Program
  • In GNOME goto the GNOME Menu > Terminal

Some Basic Linux/Unix Commands

The following commands are not specific to openSUSE, therefore you will find them in most Linux/Unix distributions. Some of the following commands may also require root rights.

Getting command-line help

Some commands only have an info page or Unix manual page and not the other, so just be aware of that. But most of them have both a Unix manual page and an info page.

View Unix manual page of the command cp

Type:

man cp


View the info page for the command cp

Type:

info cp

File and Folder Management

These commands relate directly to dealing with the various files systems in use by Linux

Listing files and folders

Type:

ls -l


Create a directory named mydir

Type:

mkdir mydir


Copy a file named /bin/file to your home directory

Type:

cp /bin/file ~


Moving ~/mydir to ~/dir2/mydir

Type:

mv ~/mydir ~/dir2/mydir


Renaming a file named file to file1

Type:

mv file file1


Delete the file named ~/file

Type:

rm ~/file


Delete a directory named mydir that contains files

Type:

rm -r mydir


Display the full path to a command

Type:

which name_of_command_here


View the full contents of a file named file

Type:

cat file


View the first 10 lines of a file named file1

Type:

head file1


View the last 10 lines of a file named file1

Type:

tail file1

Users/Groups

These commands are related directly to user's and groups

Display currently logged in users

Type:

who


Add a user named bob

Type:

useradd -m bob

Delete a user named bob

Type:

userdel -r bob


Send a message to another user

Type:

write username_here

Operating System Related Commands

Warning
The following commands are related directly to the running system and all of them will require administrative (root) access.

Killing a process

Type:

kill process_number_here
get the process number from the top command


Killing a process tree

Type:

killall process_number_here


View current processes that are running

Type:

top


View current system runlevel

Type:

runlevel


Change to runlevel 1

Type:

runlevel 1

or

type:

init 1

Hardware

Warning
These commands are related to dealing with your computers hardware, be careful

Get Information About Kernel Modules

Type:
modprobe -l | less

to get list of all kernel modules installed on the system.

Type:
modinfo module-name

Replace module-name with the real name of kernel module, without leading path and extension, that you want to get information.

I.e: to get information about kernel module for modem sierra type:
modinfo sierra


list all USB devices

Type:

lsusb


list all PCI devices

Type:

lspci

Multimedia

This section is commands for various Linux media players and other multimedia software

Use mplayer to play an audio file named "audio.mp3"

Type:

mplayer /path/to/audio.mp3

Miscellaneous

Clear the screen

Type:

clear


View bash history

Type:

history

Some Specific commands

Specific commands means that you may not find the following commands in some other Linux/Unix distributions. Some of the following commands also may require administrator rights to be run.

Install an RPM software package

Type:

rpm -i package_file_name_here.rpm

or

type:

yast -i package_file_name_here.rpm


Run the command-line version of YaST

Type:

yast

See Also