Saturday 9 February 2013

Using Linux the CLI way - Cheat Sheet

Using Linux the CLI way - Cheat Sheet


Linux, like any other Unix system is case sensitive so type them exactly as they are written in this article.

The File Separator

An important feature that differentiates Unix systems from Windows systems is the way one refers to the File Separator. On UNIX based systems it’s as follows.
/usr/local/apache/httpd.conf -----> the forward file separator. While on Windows systems. C:\webserver\apache\httpd.conf -----> the backward file separator.

any_command --help | more
Display a brief help file on a command (works with most commands). –help works similar to DOS /h switch. The more pipe is needed if the output is longer than one
screen. | This isn’t an L, it is what we call the redirection operator. If you are trying to find it on your keyboard, you’ll probably see it on the top row with the numerical keys. The main purpose of the redirector is to redirect output from one process to another. It works at the shell level and is very handy to have around. More on this
later.

ls
Lists the content of the current directory. Under Linux, the command dir is an alias to ls. Many users have ls to be an alias to ls --color.

ls -al | more
List the content of the current directory, all files (also those starting with a dot), and in a long form. Pipe the output through the more command, so that the display
pauses after each screen fill.

cd /path_to_directory
Change directory. cd - will take you to your previous directory and is a convenient way to toggle between two directories.

cp /path_to_copy_form /path_to_copy_to
Copy files from source to destination.

mv source destination
Move or rename files. The same command is used for moving and renaming files and directories.

ln source destination
Create a hard link called destination to the file called source. The link appears as a copy of the original files, but in reality only one copy of the file is kept, just two (or more) directory entries point to it. Any changes in the file are automatically visible throughout. When one directory entry is removed, the others stay intact. The
limitation of the hard links is that the files have to be on the same file system. Hard links to directories or special files are impossible.

ln -s source destination
Create a symbolic (soft) link called destination to the file called source. The symbolic link just specifies a path where to look for the file. In contradistinction to hard links, the source and destination do not have to be on the same file system. Compared with hard links, the drawback of symbolic links is that if the original file is removed, the link is broken. Symbolic links can also create circular references (like circular references in spreadsheets or databases, ( e.g., a points to b and b points back to a).

rm files
Remove (delete) files. You must own the file in order to be able to remove it.

mkdir directory
Make a new directory.

rmdir directory
Remove an empty directory.

rm -r files
(Recursive remove) Remove files, directories, and their subdirectories. Be careful with this command when logged in as root-- you can easily remove all files on the system if you are not careful.

 cat filename | more
View the contents of a text file called filename, one page a time. The | is the pipe symbol (on many keyboards it is a shift \ ). The pipe makes the output stop after each screen. For long files, it is sometimes convenient to use the commands head and tail that display just the beginning and the end of the file. If you happened to use cat on a binary file and your terminal displays funny characters afterwards, you can restore it with the command reset.

less filename
Scroll through the contents of a text file. Press q when done. Less is roughly equivalent to more, the command you just read about.

pico filename
Edit a text file using the simple and standard text editor called pico. (comes with the pine mail program)

pico -w filename
Edit a text file, while disabling the long line wrap. Handy for editing configuration files. e.g. /etc/fstab. We advise you to disable the word wrap feature for the simple
reason that you can easily mess up the configuration files with Word Wrap on,  because the line length will differ from system to system.

lynx file.html
View an html file or browse the net from text mode.

tar -xvzf filename.tar.gz
Untar a tarred and compressed tarball (*.tar.gz or *.tgz) that you downloaded from the Internet.

tar -xvf filename.tar
Untar a tarred but uncompressed tarball (*.tar).

gunzip filename.gz
Decompress a zipped file (*.gz or *.z). Use gzip (also zip or compress) if you want to compress files.

bunzip2 filename.bz2
Decompress a file (*.bz2) zipped with bzip2 compression utility. Used for big files.

unzip filename.zip
Decompress a file (*.zip) zipped with a compression utility compatible with PKZIP for DOS.

 find / -name filename
Find the file called filename on your file system starting the search from the root  directory / . The filename may contain wildcards (*,?).

rsh server (remote shell)
Yet another way to connect to a remote machine. The login name/password from  your current session is used;  if it fails you are prompted for a password.

ftp server
Ftp to another machine.Ftp is good for copying files to/from a remote machine.
The essential ftp commands are:
ls - list the files on the remote system
ASCII - set the file transfer mode to text mode
binary set the file transfer mode to binary mode
get - copy a file from the remote system to the local system
mget - get many files at once - (use *.*)
put - copy a file from the local system to the remote system
mput - put many files at once
bye - disconnectfind / -name filename rsh server (remote shell)
For automation in a script, you may want to use ncftpput and ncftpget, for example: ncftpput -u my_user_name -p my_password -a remote.host.domain remote_dir *local.html

pine
A good text-mode mail reader. Pine will let you read your local mail, e.g. the mail you get from a computer on your home network.

talk username1
Talk to another user currently logged on your machine (or use talk username1@machinename to talk to a user on a different computer). To accept the invitation to the conversation, type the command talk username2. If somebody is trying to talk to you and it disrupts your work, your may use the command mesg n to refuse accepting messages. You may want to use who or rwho to determine the users who are currently logged-in.

mc
Launch the Midnight Commander file manager (looks like Norton Commander) for Linux.

telnet server
Connect to another machine using the TELNET protocol. Use a remote machine name or IP address.You will be prompted for your login name and password--you must have an account on the remote machine to login. Telnet will connect you to another machine and let you operate on it as if you were sitting at its keyboard (almost). Telnet is not very secure-- everything you type goes in open text, even your password!.

rlogin server
(Rremote login) Connect to another machine. The login name/password from your current session is used; if it fails you are prompted for a password. (Not very secure!)

minicom
Minicom program. It’s a communication program that somewhat resembles the famous old DOS communication program, Telix. You could always use your existing shell accounts via minicom.

./program_name
Run an executable in the current directory, which is not on your PATH

xinit
Start a barebone X-windows server (without a window manager).

startx
Start an X-windows server and the default window manager. Works like typing win under DOS with Win3.1

startx -- :1
Start another X-windows session on the display 1 (the default is opened on display 0). You can have several GUI terminals running concurrently. Switch between them using Ctrl+Alt+F7, Ctrl+Alt+F8, etc.

xterm (an X terminal)

Run a simple X-windows terminal. Typing exit will close it.

shutdown -h
Shut down the system to a halt. Allowed only when logged in as root. From the console you may use Ctrl+Alt+Del to issue a shutdown -r now. This can be done by any user.

halt
Halt the machine, simpler to type than the previous command.

man topic
Display the contents of the system manual pages (help) on the topic. Try man man first. Press q to quit the viewer. The command info works in a similar fashion and may contain more up-to-date information. Manual pages can sometimes be hard to read.

--help
Brings up a short, easy to digest help on a command. If you need more info, go to the directory /usr/doc and look for the command.


who
List the users logged in on the machine. --

rwho -a
List all users logged in on your network. The rwho service must be enabled for this command to work.

finger user_name
System info about a user. Try: finger root last. This lists the users last logged-in on your system.

history | more

Show the last (1000 or so) commands executed from the command line on the current account. The | more causes the display to stop after each screen fill.

pwd
Print working directory,

free
Memory info (in kilobytes)

hostname
Print the name of the local host

df -h
Print disk info about all the file systems in a human readable form.

whoami
Print your login name.


du / -bh | more
Print detailed disk usage for each subdirectory

killall -9 program_name
Kill program(s) by name.

lsmod
to see modules currently loaded.

xkill
Kill a GUI-based program

set
Show the current user environment.

lpc
(as root) Check and control the printer(s).
                          
id
Print user id (uid)

date
Print or change the operating system date and time. E.g., change the date and time to 2000-12-31 23:57 use this command date 123123572000

time
Determine the amount of time that it takes for a process to complete+ other info.

uptime
Amount of time since the last reboot

ps
List the processes that are beeing run by the current user.

echo $PATH
Show the content of the environment variable PATH. This command can be used to show other environment variables as well. Use set to see the full environment.

dmesg | less
Print kernel messages (the current content of the so-called kernel ring buffer). Press q to quit less. Use less /var/log/dmesg to see what dmesg dumped into the file right after bootup.

ps aux | more ps
List all the processes currently running, even those Display the list of currently running processes with their together with the name process IDs (PID) numbers.

top
Keep listing the currently running processes, sorted by cpu usage (top users first).


kill PID
Force a proc

lpq
Show the content of the printer queue.

lprm job_number
Remove a printing job job_number from the queue.

nice program_name
Run program_name adjusting its priority. Since the priority is not specified in this example, it will be adjusted by 10 (the process will run slower), from the default value (usually 0). The lower the number (of niceness to other users on the system), the higher the priority. The priority value may be in the range -20 to 19. Only root may specify negative values. Use top to display the priorities of the running processes.

fg
PID Bring a background or stopped process to the foreground.

uname -a
Info on your system.

No comments:

Post a Comment