Monday, October 6, 2008

Linux/Unix Quick Commands Quick Reference Cheat Sheet

Some vital commands (and some explanations) to help navigate (or bluff!) your way through Linux/Unix. Anything between the square brackets you will need to fill in to the command as appropriate.

Kernel Modules:
Insert a kernel module: insmod [path to kernel object]
Remove a kernel module: rmmod [path to kernel object]

Shutting down / Rebooting:
Shutdown system with no reboot:
Shutdown -h now
Shutdown system with reboot: Shutdown -r now
Reboot system: reboot

Network:
Connect remotely to machine: ssh [ip address or machine name]
Connect remotely as machine as a different user (i.e: root): ssh root@[ip address or machine name]
Manual pages for a topic: man [topic]
View I.P address: ifconfig
Restart network on machine: service network restart

Administrative:
Update Linux filesystem (useful after install): Updatedb
Add user account: adduser [the desired username]
Give user account a password: passwd [username you wish to add password for]
Log in a superuser (i.e: root): su
Execute current command as root: sudo [command as normal]

General:
Close mode/connection of current terminal: exit
Mount/Unmount device graphically: usermount
View current processes (selective): ps
View full list of processes: top
Kill process: Kill [process id]
Get listings on a topic: apropos [topic]
Install a program (Look at the README first)but, generally: make or make install
Extract a .tar or .tar.gz: tar -zxvf [path to tar.gz archive]
Install rpm: rpm -ihv [path to rpm]
Compile a C/C++ program: g++ [path to a .c file]
Redirection: ./program > programoutput.txt (run the application called program and redirect the output to the programoutput.txt file instead of displaying it on the terminal)
Get absolute path to program using an alias: which [program name]

Files/Directories:
. (dot) means the current directory (./a.out means execute the program a.out in the current directory)
../ means back one directory (../../ means back two directories, etc etc)

Change Directory: cd [path to directory]
Go back one directory: cd ..
List contents of directory: ls
List contents of directory including all files: ls -a
Copy a file: cp [path to file you want copied] [path to directory you want file copied to
Move/Rename a file: mv [path to file you want moved/renamed] [path to directory you want the file moved/renamed to]
Remove file: rm [path to file]
Remove file and stop asking me questions: rm -f [path to file]
Remove directory: rm -r [path to directory]
Remove directory and stop asking me questions: rm -rf [path to directory]
View file on terminal: cat [path to file]
View and file and show me any updates in real time: tail -F [path to file] (eg: tail -F /var/log/messages to view the kernel log in real time)
Search directory for filename: find [path to directory] -name [filename]
Search directory for file containing specific text: find [directory] -name "[containing text]"
Change permissions on file or directory: chmod 7 7 7 (gives all permissions to all users)

File permissions confused the hell out of me at first, when I looked at "drwxr-xr-x" as the permissions, I haven't a clue what was going on. It breaks down as follows: d stands for directory. If its not a directory (its a file!), it will appear as -. The other 9 are dividied into 3 groups of three, Owner, Group, Others. In the case above the Owner has rwx, read, write, execute. Group has r-x, read and execute, and Others have r-x, also read and execute.

Bash:
Start bash: bash (complex!)
Run a script within bash: source [path to script]

No comments: