You are on page 1of 8

Linux Commands

To add another user:


- useradd “name” (e.g. useradd dustin)
- passwd dustin
- type in password, hit enter
- type again, hit enter

Changing Directories:
- cd “directory name” (e.g. cd /home)
- pwd (tells you what directory you are currently in)
- cd .. (goes out of your current directory)
- cd will bring you to your user’s home directory

Directory Contents:
- ls (will show you the files and/or directories in your location)
- ls –a (will also show hidden files)
- Not sure how to spell the directory?
- start typing the beginning and press tab (file should come up)
- e.g. type cd m and press tab a few times (all files with m come up)
- ls –l (this will give you more details about your directory contents)
- the command dir will also do the ls command

View Previous Commands:


- simply press Up on your keyboard
- you may also type history to see previous 400 commands

Shutting down the machine:


- Two ways:
- type shutdown –h now
- type init 0
- Optional:
- type shutdown –h +5 (system will shut down in 5 minutes)

Rebooting the machine:


- Two ways:
- type shutdown –r now
- type init 6

Change to non-graphical mode:


- Press CTRL-ALT-F6 to go to non-graphical login
- Press ALT-F7 to return to GUI
Changing a file with a Text Editor:
- type vi “name of file” (e.g. vi lilo.conf)
- type i for insert and begin typing in the file
- press ESC and SHIFT-Z-Z to write and quit the file

Clear the terminal screen:


- type clear

Adding an alias:
- go to your home directory (e.g. cd /home/dustin)
- find .bashrc (type ls –a to find it)
- type vi .bashrc
- type alias mv=‘mv –i’ (anything can be substituted there)
- exit and save
- type source .bashrc

Check a File’s Type:


- to see what type of file something is
- type file “file name” (e.g. file home)

To View Details of File or Directory:


- type more “file name” (e.g. more dustin)
- type less “file name” (e.g. less dustin)
- Type q to get out of the details and return to command prompt

Copying a File:
- type cp “file name” “new file name”
- e.g. cp crazy crazy2

Moving a File:
- type mv “file name” “location”
- e.g. mv craziness /home

Removing a File:
- type rm “file name”
- e.g. rm testpage
- type rm * -f to remove everything with no questions asked
- To remove a directory and everything inside of it:
- rm –rf “directory name”

Making a Directory:
- type mkdir “directory name”
- e.g. mkdir myfiles
Removing a Directory:
- type rmdir “directory name”
e.g. rmdir myfiles
- Note: You can’t remove directory unless it is empty

Piping Commands:
- To connect commands, use the vertical line
- e.g. ls –l | more
- this list the contents of the directory (ls –l) with the more command
- Page-Up and Page-Down work with scrolling through a long list after more

Retrieving Documentation:
- Many different ways
- type man “command” (e.g. man cp)
- type “command” –help (e.g. cp –help)
- type info “command” (e.g. info cp)
- type apropos “command” (e.g. apropos cp)
- apropos gives you available manuals based on your typed word

Touch Command:
- touch can be used to create a file
- e.g. touch crazyfile
- touch can also alter a file’s creation date
- e.g. touch –t 04090912 crazyfile.txt

Find Command:
- find command can find files
- e.g. find –name cr* (all files beginning with cr will come up)
- another example: find –mmin +0 –mmin -10
- this brings up all files created and/or worked on in last 10 minutes

Grep Command:
- searches inside the files
- e.g. You have three files with the word Mary in them
- type grep Mary mary*.txt
- all three files get returned
- only one of those files has the word cow in it
- grep cow mary*.txt
- only one file gets returned

Who and Last Commands:


- this command lets you know who has been logged into the system
- type who and a list of users will come up
- another command is last
- this will tell you who was logged in and for how long
Tee Command:
- this command takes what is on the screen and places it in a file
- e.g. ls –l | tee directory_listing
- you may also stamp a date in the file
- e.g. date | tee –a directory_listing
- the –a adds to the file, if no –a is added, it will write over the file

Alternative to Tee:
- you may use the > symbol to perform tee
- e.g. ls –l > directory_listing
- you can stamp a date as well
- e.g. date >> directory_listing
- both greater than symbols with add, one will overwrite

2> Command:
- this command is used for creating a file for an error
- e.g. crazyfile 2> program_X_error
- error message will be saved

Whoami Command:
- this tells you who the current user is
- type whoami

Whatis Command:
- this tells you what a command is used for
- e.g. whatis cp

Whereis Command:
- this is used to find the exact location of a program
- e.g. whereis vi
- similar command is which
- e.g. which vi

Echo Command:
- used mainly in shell scripts to repeat what you have typed
- e.g. echo hello
- echo can be used for other things
- e.g. echo Russell owes me money | tee –a russellnote.txt
- this create a text file with the note you typed

WC Command:
- this command tells you the number of lines, words, and characters in a file
- e.g. wc crazyfile.txt
Date Command:
- type date to get today’s date
- to find out what day of the week a date falls on:
- e.g. date --date ‘Feb 28’
- to get a calendar:
- e.g. cal 2 2006

Exiting:
- exit will get you out of the x-window
- logout will allow you to switch users

Memory and Process Commands:


- df will tell you how much hard drive space is used and available
- free will tell you how much RAM is being used and available
- the du command will tell you how big a file is
- e.g. du –b myfile
- top will give you a list of processes and what is running on the machine
- ps will give you a list of processes as well
- ps –u and ps –au will give more details

Killing a Program or Process:


- to kill a troublesome program:
- type ps to find process and get PID number
- type kill “PID number” (e.g. kill 441)
- another way to kill a certain process (like if you typed find *):
- CTRL-C

Formatting a Floppy:
- insert floppy disk
- type mke2fs /dev/fd0 and press enter
- type e2fsck /dev/fd0 and press enter

Mounting File Systems:


- To mount a CD:
- type mount /dev/cdrom /media/cdrom
- Note: You may need to replace cdrom with cdrecorder after media
- Go into /media/cdrecorder to see contents of the cd

Mounting a Server:
- To mount a server:
- type mkdir /ts
- type mount 10.0.1.5:/ts /ts
- Note: Replace the IP with your appropriate IP
Unmounting File Systems:
- To unmount a CD:
- type umount /media/cdrom (or cdrecorder)

Tip on Mounting:
- If you’re trying to unmount something but refuses to do so, here are some steps:
- Make sure you are not inside the area you are trying to unmount (pwd)
- type lsof | grep cdrec
- this will show a list of open files from the cdrom
- at this point, you can kill any open files and unmount it

File Permissions:
- permissions are done in 3-part 4-2-1 setup
- the 3 parts, in order, are Owner, Group, and Others
- 4 represents read, 2 represents write, 1 represents execute
- e.g. chmod 755 crazyfile
- this means the owner has full rights, while the group and others can read
and execute, but can NOT write (4 + 1 = 5)
- e.g. chmod 644 awesomefile
- owner can read and write, while group and others can only read

Changing Ownership of a file:


- type ls –l to see the owner of a particular file
- type chown user:user “file name”
- e.g. chown dustin:dustin crazyfile.txt

Backing Up Files:
- When you have a number of files and you want to combine them in a single file
- type tar –cvf “file created” “files”
- e.g. tar –cvf linuxnotes.tar notes*.txt
- this will take all files named notes and tar them into linuxnotes.tar
- this does NOT compress the files
- To compress the file, you must now zip it
- type gzip “file name”
- e.g. gzip linuxnotes.tar
- Tip: Tar and Zip can be done in one step
- e.g. tar –czvf linuxnotes.tar.gz notes*.txt

Untarring and Unzipping:


- To untar and unzip a file at the same time:
- type tar –zxvpf “file name”
- e.g. tar –zxvpf linuxnotes.tar.gz
- If the file is zipped and not tarred:
- e.g. gzip –d notes1.txt.gz
Installing a Program:
- Programs being installed are rpm’s
- type rpm –i “program name.rpm”
- To see if a program has been installed:
- type rpm –q “program name”
- To install an update:
- type –U “program name.rpm”
- EXAMPLE:
- rpm –ivh VMWare5.5.rpm

Network Configuration:
- multiple options:
- type in netconfig
- small screen will appear to configure network
- Use the GUI and go to network in system settings
- Go into etc/sysconfig/network-scripts and vi into ifcfg-eth0

Troubleshooting Tip:
- Getting an unknown error?
- go into /var/log
- type tail messages
- e.g. tail –n 100 messages | less (last hundred messages)
- tail –f messages (places message log in real time)

Delegating Authority:
- As the root user, you can choose what other users can do
- For example:
- type visudo
- Under user alias specification, give trusted users
- e.g. User_Alias TRUSTED = dustin
- Under cmnd alias specification, give commands to be used
- e.g. Cmnd_Alias TOOLS = /bin/mount, /bin/umount
- Under user privilege specification
- e.g. TRUSTED ALL = TOOLS

Changing Users:
- su will allow you to work as the root user
- e.g. su, type password
- e.g. su dustin (if you were root, a password will not be needed)

Checking The Network Status:


- this command will show you which ports are available and open on a network
- nmap –P0 “IP address”
- e.g. nmap –P0 63.14.23.30
Turning Services On and Off:
- this command makes sure certain services are turned on or off when Linux boots
- chkconfig “service” on
- service “service” start
- e.g. chkconfig samba on
- service samba start

You might also like