Linux Cheat Sheet PDF Print E-mail
Written by Administrator   
Saturday, 18 April 2009 07:25

LINUX CHEAT SHEET //////////////////////////////
===============================
===============================

Basic Actions
File System
Zipping Tarring
Searching
MySQL
Basic Commands
Extras

arrow up
\*scrolls and edits the command history, press enter to activate*\
===============================
===============================

shift+page pp
\*scrolls terminal output up*\
===============================
===============================

shift+page down
\*scrolls terminal output down*\
===============================
===============================


ctrl+alt+del
\*reboots the system*\
===============================
===============================


shutdown -h
\*Now turns the system off*\
===============================
===============================


ctrl+c
\*Kills the current process*\
===============================
===============================


ctrl+s
\*Stops the tranfer to the terminal*\
===============================
===============================


ctrl+q
\*Resumes the transfer to the terminal*\
===============================
===============================

ctrl+z

\*Puts the current process in the background*\
===============================
===============================


middle mouse button
\*Pastes the text that is currently somewhere else*\
===============================
===============================

pwd
\*Shows the current directory*\
===============================
===============================


hostname
\*Shows the host name of the system you are on*\
===============================
===============================


whoami
\*Displays your login name*\
===============================
===============================


date
\*Displays what your machine thinks the date is*\
===============================
===============================


who
\*Shows who is logged into the machine*\
===============================
===============================


rwho -a
\*Shows all users logged into the server network*\
===============================
===============================


finger <user name>
\*Shows info on chosen user*\
===============================
===============================


last
\*Show the last users logged into the machine*\
===============================
===============================


uptime
\*Shows how long the system has been up*\
===============================
===============================


ps
\*Shows the current user processes*\
===============================
===============================


ps -a
\*Shows all process on the system*\
===============================
===============================


uname -a
\* Displays all info on your host*\
===============================
===============================


free
\* Shows the free memory in KB*\
===============================
===============================


df -h
\* Shows the disk space details*\
===============================
===============================


cat /proc/cpuinfo
\* Shows the CPU information*\
===============================
===============================


cat /proc/filesystems
\* Shows the file system information in use*\
===============================
===============================


cat /etc/printcap
\* Shows if any printers are hooked up*\
===============================
===============================


lsmod
\* Shows the kernel modules loaded*\
===============================
===============================


setimore
\* Shows the current user environment*\
===============================
===============================


echo $PATH
\* Shows the content on the environment variable path*\
===============================
===============================


dmesg
\* Prints the boot messages*\
===============================
===============================


BASIC ACTIONS ///////////////////////////////////////
===============================
===============================

[command] –help - gives syntax for using that command
===============================
===============================

man [command] - brings up the manual page for the command, if it exists
===============================
===============================

man [command] > file.txt - dumps the manual page(s) for the command into ‘file.txt’
===============================
===============================

whatis [command] - gives a short description of the command.
===============================
===============================

help - gives a list of commands (GNU Bash).
===============================
===============================

help [command] - gives extra information on the commands listed above. Viewing/editing/creating a text file
===============================
===============================

vi [filename] - opens vi text editor, if the file doesn’t exist, it’ll be created on saving.
===============================
===============================

(when inside vi)
===============================
===============================

- using ‘i’ inserts
===============================
===============================

- pressing ‘escape’ and then ‘:’ goes back to command mode.
===============================
===============================

- ‘/searchstring’ searchs for ’searchstring’ using regular expressions.
===============================
===============================

- ‘:’ followed by ‘w’ writes
===============================
===============================

- ‘:’ followed by ‘qw’ writes then quits
===============================
===============================

- ‘:’ followed by ‘q’ quits.
===============================
===============================

- ‘:’ followed by ‘q!’ quits regardless of whether changes are made.
===============================
===============================

- ‘:’ followed by ‘z’ undo.
===============================
===============================

pico [filename] - launches the pico editor for the filename.
===============================
===============================

more [filename] - shows one screen’s worth of the file at a time.
===============================
===============================

less [filename] - similar to more.
===============================
===============================

head [filename] - Shows the first 10 lines of file, or use -n.
===============================
===============================

tail [filename] - Shows the last 10 lines of file, or use -n.
===============================
===============================

cat [filename] | more - works like more, cat concats 2 strings General/System commands
===============================
===============================

su [user] - changes the login to ‘user’, or to the root if no ‘user’ is given.
===============================
===============================

date - shows the system date
===============================
===============================

whoami - tells you who you’re logged in as
===============================
===============================

uptime - how long the computer has been running, plus other details
===============================
===============================

w - shows who’s logged on, what they’re doing.
===============================
===============================

df - how much disk space is left.
===============================
===============================

du - disk usage by your login, it can also total up directories.
===============================
===============================

uname -mrs - userful info about the system
===============================
===============================

uname -a - all details about the system desktop / x server + client
===============================
===============================

switchdesk {manager - gnome, Enlightenment, etc} - Switches your desktop, what’s running.
===============================
===============================

ps - what’s running.
===============================
===============================

ps ax - shows all processes
===============================
===============================

top - sort of interactive version of ps.
===============================
===============================

kill [pid] - terminates the named process, which can be name or number or other options.
===============================
===============================

killall -HUP [command name] - kill a process, running the command specified, by name.
===============================
===============================

killall -9 [command] - similar to the above
===============================
===============================

xkill - kills a frozen application in X (gnome,kde etc. desktops), you just click on the frozen app.
===============================
===============================




FILE SYSTEM ///////////////////////////////////////
===============================
===============================

ls -la - list all files/directories
===============================
===============================

dir - simple form of ls
===============================
===============================

cd [dir] - change directory
===============================
===============================

cd ~ - go back to the home directory
===============================
===============================

cdup - similar to using “cd ..”, go up one directory.
===============================
===============================

pwd - print which directory you’re in.
===============================
===============================

./[filename] - run the file if it’s executable and in the current directory
===============================
===============================

rm [filename] - delete a file
===============================
===============================

rm -R [directory] - delete a directory
===============================
===============================

mv [oldfilename] [newfilename] - renames the file (or directory)
===============================
===============================

cp [filename-source] [filename-destination] - copy the file from one place to another
===============================
===============================

cp -R [dir-source] [dir-destination] - copy a directory an all its subdirectories
===============================
===============================

mkdir [name] - makes a directory.
===============================
===============================

cat [sourcefile] >> [destinationfile] - appends sourcefile to the end of destinationfile
===============================
===============================

df - how much disk space is available, more options available.
===============================
===============================



ZIPPING / TARRING /////////////////////////////////
===============================
===============================

tar -cvzf mytar.tar.gz sourcefilesordir - creates a new tar file, verbose options on, runs it through gnuzip,f is the filename
===============================
===============================

tar -xvf mytar.tar.gz destination - extracts a tar file (this example is compressed with gzip), verbosely, f is the filename
===============================
===============================

gzip fileordir - compresses a file with gzip.
===============================
===============================

gunzip file.gz - decompresses a file with gzip.
===============================
===============================

NB gzip only compresses files, it doesn’t collect them into a single file like a tarball does.
===============================
===============================

 

SEARCHING //////////////////////////////////////////////
===============================
===============================

locate [filename] - searches the system using an indexed database of files. use updatedb to update the file database
===============================
===============================

locate [filename] | sort - sorts the files alphabetically
===============================
===============================

whereis [filename] - locates an application, such as ‘whereis bash’
===============================
===============================

find [filename] - searches the filesystem as with locate, but without a database so its slower.
===============================
===============================

find /directory -atime +30 -print - searches for files not used in the past 30 days. Setting up links
===============================
===============================

ln -s target linkname - creates a symbolic link, like a shortcut to the target directory or filename.
===============================
===============================

ln target linkname - creates the default hard link. Deleting this will delete the targetted file or directory. Network commands
===============================
===============================

dig domainname - retrieves information about a domain, such as name servers, mx records
===============================
===============================

whois domainname - whois info on a domain
===============================
===============================

finger user - gives info about a user, their group status, but can also be used over a network
===============================
===============================

netstat -ape - lots of info about whos connected to your machine, what processes are doing what with sockets piping
===============================
===============================

piping to another command is straight forward enough:
===============================
===============================

locate filename | grep /usr/local > searchresults.txt - searches for filename, runs the results through grep to filter everything without /usr/local in it, and then outputs the results to searchresults.txt
===============================
===============================

| runs one application via another, and can be used multiple times e.g. cat /usr/group | more | grep root | sort
===============================
===============================

> creates a new file if once doesn’t already exist, overwrites the contents of the file if it does exist
===============================
===============================

>> appends to the end of the file, and creates the file if one doesn’t exist.
===============================
===============================

< sends everything after this to the application, e.g. ./mysql -u bob -p databasename < mysqldump.sql Permissions and directory listing format
===============================
===============================

groups [username] - shows what groups the user belongs to
===============================
===============================

id [username] - shows extended information about a user.
===============================
===============================

finger [user] - give details about a user.
===============================
===============================

passwd [user] - changes the password for a user, or without the user argument, changes your password.
===============================
===============================

chsh [user] - changes the shell for a user.
===============================
===============================

userdel [user] - removes a user from the system, use -r to remove their home directory too.
===============================
===============================

newgrp [group id] - log into a new group.
===============================
===============================

useradd -d /home/groupname -g groupname - add a new user with the d being the homedirectory, g the default group they belong to.
===============================
===============================

groupadd [groupname] - adds a group
===============================
===============================

Take a look at the users/groups on the system with:
cat /etc/passwd | sort
cat /etc/group | sort
===============================
===============================

The stuff below is in the man pages also.
===============================
===============================

The format of passwd is:
username
===============================
===============================

password denoted by x (use cat /etc/shadow | sort to list the shadow password file)
uid - user identifier number
gid - group identifier number
misc information such as real name
users home directory
shell for the user
===============================
===============================

The format of group is:
name of group
password denoted by x (use cat /etc/gshadow | sort to list the shadow group file)
gid - group identifier number
list of additional users assigned to the group
===============================
===============================

Break down of permissions in a directory listing:
-rw-r–r– 1 mainuser devel 9054 Dec 28 12:42 index.html
===============================
===============================

The first character indicates whether it is a directory or file (d for directory).
After that, the next 3 (rw-) are owner permissions.
The following 3 (r–) are group permissions
The following 3(r–) are permissions for other users.
===============================
===============================

After that reads the number of files inside the directory if it’s a directory (which it isn’t so it’s 1) this can also be links to the file, the owner of the file, the group the file belongs to, size in bytes, date and time and then the filename.
===============================
===============================

Chmod and Chown
Owner,group and other permissions can be r,w,x. Translated into their decimal equivalents (actually octal but…)
owner - read=400,write=200,execute=100
group - read=40,write=20,execute=10
other - read=4,write=2,execute=1
===============================
===============================

So add them up and you’ve got your user permissions for chmoding:
chmod [mode] fileordirectory - changes the permissions on a file or directory. use -r to recursively change a whole directory and its sub directories.
===============================
===============================

e.g chmod 755 myfile.txt - changes the permissions on the file to 755 which is : owner read,write,execute; group read,execute; other read,execute.
===============================
===============================

chown [user:group] fileordirectory - changes the user and group ownership of a file or directory. Use -R to recursively change a whole directory and its sub directories.
chgrp [group] fileordirectory - changes the groupownership of a file or directory. Use -R to recursively change a whole directory and its sub directories.
===============================
===============================

 

MySQL ////////////////////////////////////////////////////////
===============================
===============================

mysqldump - Dumps a table,database or all databases to a SQL file. Use the –opt argument for best results e.g.
mysqldump -u username -p –opt database > file.sql
mysql - The mySQL query manager. To import/export a database to or from a SQL try:
mysql -u username -p database < file_to_go_in.sql
mysql -u username -p database > file_to_go_to.sql



Basic Commands //////////////////////////////////////////
===============================
===============================

 
  man {command} Type man ls
\* to read the manual for the ls
\* command.
  man {command} > {filename} Redirect help to a file to download.
  whatis {command} Give short description of command. (Not on RAIN?)
  apropos {keyword} Search for all Unix commands that match keyword, eg apropos file
\*. (Not on RAIN?)

List a directory
  ls {path} It’s ok to combine attributes, eg ls -laF
\* gets a long listing of all files with types.
  ls {path_1} {path_2} List both {path_1} and {path_2}.
  ls -l {path} Long listing, with date, size and permisions.
  ls -a {path} Show all files, including important .dot files that don’t otherwise show.
  ls -F {path} Show type of each file. “/
\*” = directory, “*
\*” = executable.
  ls -R {path} Recursive listing, with all subdirs.
  ls {path} > {filename} Redirect directory to a file.
  ls {path} | more Show listing one screen at a time.
  dir {path} Useful alias for DOS people, or use with ncftp
\*.

Change to directory
  cd {dirname} There must be a space between.
  cd ~ Go back to home directory, useful if you’re lost.
  cd .. Go back one directory.
  cdup Useful alias, like “cd ..”, or use with ncftp
\*.

Make a new directory
  mkdir {dirname}  

Remove a directory
  rmdir {dirname} Only works if {dirname} is empty.
  rm -r {dirname} Remove all files and subdirs. Careful!

Print working directory
  pwd Show where you are as full path. Useful if you’re lost or exploring.

Copy a file or directory
  cp {file1} {file2}  
  cp -r {dir1} {dir2} Recursive, copy directory and all subdirs.
  cat {newfile} >> {oldfile} Append newfile to end of oldfile.

Move (or rename) a file
  mv {oldfile} {newfile} Moving a file and renaming it are the same thing.
  mv {oldname} {newname}  

Delete a file
  rm {filespec} ?
\* and *
\* wildcards work like DOS should. “?” is any character; “*” is any string of characters.
  ls {filespec}
rm {filespec}
Good strategy: first list a group to make sure it’s what’s you think…
…then delete it all at once.

Download with zmodem (Use sx
\* with xmodem.)
  sz [-a|b] {filename} -a
\* = ascii, -b
\* = binary. Use binary for everything. (It’s the default?)
  sz *.zip Handy after downloading with FTP. Go talk to your spouse while it does it’s stuff.

Upload with zmodem (Use rx
\* with xmodem.)
  rz [-a|b] (filename} Give rz
\* command in Unix, THEN start upload at home. Works fine with multiple files.

View a text file
  more {filename} View file one screen at a time.
  less {filename} Like more
\*, with extra features.
  cat {filename} View file, but it scrolls.
  cat {filename} | more View file one screen at a time.
  page {filename} Very handy with ncftp
\*.
  pico {filename} Use text editor and don’t save.

Edit a text file.
  pico {filename} The same editor PINE uses, so you already know it. vi
\* and emacs
\* are also available.

Create a text file.
  cat > {filename} Enter your text (multiple lines with enter
\* are ok) and press control-d
\* to save.
  pico {filename} Create some text and save it.

Compare two files
  diff {file1} {file2} Show the differences.
  sdiff {file1} {file2} Show files side by side.

Other text commands
  grep ‘{pattern}’ {file} Find regular expression in file.
  sort {file1} > {file2} Sort file1 and save as file2.
  sort -o {file} {file} Replace file with sorted version.
  spell {file} Display misspelled words.
  wc {file} Count words in file.

Find files on system
  find {filespec} Works with wildcards. Handy for snooping.
  find {filespec} > {filename} Redirect find list to file. Can be big!

Make an Alias
  alias {name} ‘{command}’ Put the command in ’single quotes’. More useful in your .cshrc
\* file.

Wildcards and Shortcuts
  * Match any string of characters, eg page*
\* gets page1, page10, and page.txt.
  ? Match any single character, eg page?
\* gets page1 and page2, but not page10.
  […] Match any characters in a range, eg page[1-3]
\* gets page1, page2, and page3.
  ~ Short for your home directory, eg cd ~
\* will take you home, and rm -r ~
\* will destroy it.
  . The current directory.
  .. One directory up the tree, eg ls ..
\*.

Pipes and Redirection (You pipe
\* a command to another command, and redirect
\* it to a file.)
  {command} > {file} Redirect output to a file, eg ls > list.txt
\* writes directory to file.
  {command} >> {file} Append output to an existing file, eg cat update >> archive
\* adds update to end of archive.
  {command} < {file} Get input from a file, eg sort < file.txt
\*
  {command} < {file1} > {file2} Get input from file1, and write to file2, eg sort < old.txt > new.txt
\* sorts old.txt and saves as new.txt.
  {command} | {command} Pipe one command to another, eg ls | more
\* gets directory and sends it to more
\* to show it one page at a time.

 


Extras ///////////////////////////////////////////////////////////
===============================
===============================

man commandToFindHelpAbout | col -b >/var/man/mancommandToFindHelpAbout.man.txt
===============================
===============================

echo create a backup file based on the timestamp on bash
===============================
===============================

cp fileName.ext fileName.ext.`date +%Y%m%d%H%M%S`.bak
===============================
===============================

alias ls="ls -a -X -1 --color=tty"
===============================
===============================

alias dir="ls -ba"
===============================
===============================

alias cls="clear"
===============================
===============================

alias deltree="rm -r"
===============================
===============================

alias move="mv"
===============================
===============================

ls -al | more
# work with the history
===============================
===============================

#-- where have I been lately ?
history | grep cd
===============================
===============================

# take the last 5 commands for faster execution to the temp execution script
tail -n 5 /root/.bash_history >> /var/run.sh
===============================
===============================

# I saw the command cd /to/some/suching/dir/which/was/very/long/to/type so I redid it and saved my fingers
!345
===============================
===============================

history >history.txt ---- echo the last 1000 commands into a history.txt file
===============================
===============================

# send that file to myself
cat history.txt | mail -s "test file sending" -c mail1@com yordan.georgiev(boom)gmail.com
===============================
===============================

# remove all trailing spaces from the history file works for TextPad
^([ ]*)([0-9]*)(.*)$
===============================
===============================

\3 --- Replace with the pure commands
history | gawk -F1 'BEGIN {FS=" "};{print $2 , $3}' | less
===============================
===============================

# display history without the line numbers
history | perl -i -pe 's/^([ ]*)([0-9]*)(.*)$/$3/gi'
===============================
===============================

vim fileName anotherFile
# How to deal with command outputs
===============================
===============================

command | filtercommand > command_output.txt 2>errors_from_command.txt
===============================
===============================

# hint after the less filter pressing s will prompt you for saving the output to a file
===============================
===============================

# find all files and folders containing the word linux and pipe it to the less for easier viewing
find / -name "linux" | less
===============================
===============================

# find the files having os somewhere in their names and only those having linux
find . -name '*os*' | grep linux | less
===============================
===============================

# find all xml files and display only the rows having wordToFindInRow
find . -name '*.xml' -exec cat {} \;| grep wordToFindInRow | less
===============================
===============================

# the ultimate "find in files" in Linux
find / -name '*bash*' -exec grep -nH tty {} \;
===============================
===============================

# or even faster , be aware of "funny file names xargs -0
find / -name '*bak' | xargs grep -nH tty
===============================
===============================

ctrl + a --- Go to the beginning of the line you are currently typing on
ctrl + e --- Go to the end of the line you are currently typing on
===============================
===============================

# how-to mount a USB stick
===============================
===============================

# remember to change the path other wise you will get the device is busy errror
===============================
===============================

mkdir /mnt/usbflash
mount /dev/sdb1 -t vfat /mnt/usbflash
umount /mnt/usbflash
===============================
===============================

head -n 20 tooLongFile -- display the first 20 lines of the file
===============================
===============================

# change the uggly prompt
vim /etc/bashrc
PS1="\u@\h \t \w\n\\$ "
===============================
===============================

# How to restart a process initiated at startup
/etc/rc.d/init.d/sendmail start | stop | status | restart
===============================
===============================

# see all the rules associated with the firewall
iptables -L -v
===============================
===============================

gunzip *file.zip
tar -xvf file.tar
===============================
===============================

to access the server download winscp.exe:
===============================
===============================

start winscp with a stored session
winscp oracle@192.168.255.12
===============================
===============================

# to start remote session click on the putty screen , configure putty settings to pull full screen with alt + Enter
===============================
===============================

# right click on the title bar , settings , change the font , copy paste from and to the terminal window text
===============================
===============================

# How to ensure the sshd daemon is running
ps -ef | grep sshd
===============================
===============================

# User and group management
useradd -p winscppass -r winscp -d /home/winscp --- to add a user with specific home directory name and pass
luserdel winscp --- delete the user
===============================
===============================

gpasswd: administer the /etc/group file
groupadd: Create a new group
groupmod: Modify a group
groupdel: Delete a new group
===============================
===============================

useradd: Create a new user or update default new user information
usermod: Modify a user account
userdel: Delete a user account and related files
===============================
===============================

# how to kill process interactively
killall -v -i sshd
===============================
===============================

# disk usage of users under the /home directory
===============================
===============================

du --max-depth=1 /home | sort -n -r
===============================
===============================

# The most efficient way to search your history is to hit Ctrl + R and type the start of the command. It will auto complete as soon as there’s a match to a history entry, then you just hit enter. If you want to complete the command (add more stuff to it ) use the right arrow to escape from the quick search box
===============================
===============================

How to install Perl modules
===============================
===============================

gzip -dc yourmodule.tar.gz | tar -xof -
perl Makefile.PL
make test
make install
===============================
===============================

how to see better which file were opened, which directories were visited
===============================
===============================

type always the fullpath after the vi - use the $PWD env variable to open files in the current directory, thus after opening the file after:
===============================
===============================

vim /$PWD, press tab to complete the name of the current directory , type the name of the file
===============================
===============================

thus after
history | grep vim
===============================
===============================

the full list of opened files is viewed.
===============================
===============================

of course the same could be seen from the /home/username/.viminfo file /files
===============================
===============================

where to set the colors for the terminal (if you are lucky to have one with colors)
/etc/DIR_COLORS
===============================
===============================

open a file containing "sh" in its name below the "/usr/lib" directory
===============================
===============================

:r !find /usr/lib -name *sh*
===============================
===============================

uname -a --- which version of Linux I am using
===============================
===============================

rmp -qa --- show all installed packages
===============================
===============================

passwd [username] --- change the password for the specified user (own password)
===============================
===============================

how to copy paste text in the putty window from client to server - click the right button of your pointing device
===============================
===============================

how to copy paste text from the putty window from server to client - right-click the window title and select copy all to Clipboard.
===============================
===============================

to restart a service
===============================
===============================

service sshd restart
===============================
===============================

service --status-all --- show the status of all services
===============================
===============================

chown -R root:nortel Directory
===============================
===============================

# Perform action recursively on a set of files
find . -name '*.pl' -exec perl -wc {} \;
===============================
===============================

$ for file in *
> do cp $file $file.bak
> done
===============================
===============================

$ for file in `ls -R` ; do cp $file $file.bak; done
===============================
===============================

# Make Bash append rather than overwrite the history on disk:
shopt -s histappend
===============================
===============================

# Whenever displaying the prompt, write the previous line to disk:
PROMPT_COMMAND='history -a'
===============================
===============================

gpm -- general "cut and paste" server

Comments
Search
Only registered users can write comments!

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."

Last Updated on Wednesday, 07 October 2009 17:59
 

Founder MJ12Net

Founder MJ12Net.org

Brian Bills
Founder MJ12Net
System Admin

Stumble Us

Valid XHTML & CSS | Template Design ah-68 | Copyright © 2009 by Firma