Unix Tutorial
1) ls = This command lists the contents of a directory.
ls -a - Lists all the contents of the current directory, and therefore does not hide entries beginning with a . the way that the unqualified command does.
ls -h - Lists the contents of the current directory in human-readable format, mainly for easy reference of file sizes.
ls -l - Lists the contents of the current directory in long format, providing more information than just filenames.
Switches can be combined to combine their effects, for instance ls -lha will list all the contents of a directly in long, human-readable format.
2) cp = This command copies files from one location to another.
Examples
cp myfile.txt mydirectory/ - Copies the file myfile.txt in the current directory to the mydirectory directory.
cp /home/mydirectory/myfile.txt /home/mydirectory/backup/myfile.bak - Copies the file myfile.txt in the mydirectory directory into the mydirectory/backup/ directory as myfile.bak. The files are identical, however they have different names.
cp *.txt mydirectory/ - Copy all files ending in .txt into the directory named mydirectory.
cp -r /home/files/* /home/backup/ - Copies everything in the /home/files/ directory into the /home/backup/ directory. Note the asterisk (*) that indicates that everything will be copied.
3) rm = This command deletes a file. By default it will do so without confirmation.
rm myfile.txt - Removes the file myfile.txt without prompting the user.
rm -r directory - Removes a directory, even if there are currently files in that directory. It will prompt for every single file.
rm -rf directory - Removes a directory, even if there are currently files in that directory. It will not ask for confirmation for each file.
To remove a file whose name starts with a `-`, for example `-foo`, use one of these commands:
rm - -foo
rm ./-foo
4) mkdir = This command creates a directory.
mkdir mydirectory - This command creates a directory called mydirectory in the current directory.
5) rmdir = This command deletes a directory.
rmdir mydirectory - Removes the directory mydirectory. This will fail to work if files exist in the directory.
rm -r mydirectory - Remove the directory mydirectory, even if files currently exist in that directory.
There is no -r option for rmdir, therefore to remove a directory with files still in it, we can use rm -r or rm-rf.
rmdir -p dir3/dir4/dir5 - Removes dir5, dir4 and dir3 if dir5 were empty, dir4 only contained dir5 and dir3 only contained dir4 (which, in turn, contained dir5). Essentially used to delete empty directory trees.
6) mv = This command renames a file or moves it from one directory to another directory.
Syntax
mv [-f] [-i] oldmyfile.txt newmyfile.txt (to rename)
mv [-f] [-i] myfile.txt mydirectory/
-f - mv will move the file(s) without prompting even if it is writing over an existing target. Note that this is the default if the standard input i s not a terminal.
-i - Prompts before overwriting another file.
oldmyfile - The old name of the file being renamed.
newfilename - The new name of the file being renamed.
myfile.txt - The name of the file you want to move.
mydirectory - The name of the directory where you want the file to go.
Examples
mv myfile.txt mydirectory/ - This command moves the file myfile.txt to the directory mydirectory.
mv myfile.txt ../ - This command moves the file myfile.txt back one directory (if possible).
mv myfile.txt ~/mydirectory/ - This command moves the file myfile.txt to the directory mydirectory under your home directory. Note the presence of the tilde, which indicates the home folder of the current user.
7) less = This command has many features, the most basic of which will allow you to quickly read the contents of a text file.
less myfile.txt - Displays the contents of the text file for reading.
8) grep = This command is the basic search tool of the Unix command line which will allow you to search files for specific strings of characters.
grep mystring myfile.txt - This command searches the file myfile.txt for the string "mystring".
grep -w mystring myfile.txt - This switch can be given to grep in order to search for exact matches of whole words, and would find the word "mystring" but not "mystringisthis".
grep -i mystring myfile.txt - This switch causes grep to become case-insensitive and ignore whether the string matches the case you used precisely, which it WILL otherwise care about. So this will find "MYSTRING" as easily as "mystring".
9) man = This command displays the manual information for whatever other command it is used on.
man ls - This command would display the manual pages for the ls command.
10) cat = This command is another one that can flexibly perform functions on text files, including reading, changing and creating.
cat myfile.txt - This is the most basic structure for this command, and would result in the text inside the file being displayed on screen.
cat >myfile.txt - This command will actually take whatever you type next and store it in the file myfile.txt. It's important to note that when you're done typing, you press Ctrl+D to inform Unix to store what you've typed in the indicated file.
11) pwd = This is a very basic command that will simply print the name and path of the directory you're in at the moment, if you need reminding of where you are in the filesystem.
12) vi = This is a very functional, but quite complicated text editor. For a good cheatsheet of commands that can be used inside of vi, check here.
13) piping = Unix commands can be piped together using the "|" character in order to feed the results of one into another and combine their functionality.
ls|grep mystring - This command will call up a list of items in the current directory that contains the string mystring in the title.
ls|grep -i mystring - This command does the same as the one above, but will call up any title containing mystring regardless of case. It will, for instance, detect myString where the above command would not.
14) tar = This is a command used to create, extract, and otherwise manipulate files archived in the tar format.
tar -xf mytarfile.tar - Unless you know what you're doing, this is probably the only version of the tar command that you should use, as it's somewhat easy to delete your files when messing with this program. In this case mytarfile.tar is the name of the tar archive that you want to extract from, the -x switch tells the program that you want to extract and the -f switch tells the program that you're going to feed it the filename you want to extract from. Once again, be very careful. Dragons be here.
Miscellaneous Tips
When using the command line, pressing the up arrow key will allow you to recall recent commands that you have entered, to prevent the need to retype them repeatedly.
Back up your work regularly. This is even more important in Unix environments than it is in other operating systems, and should be done very regularly for safety's sake. An example of a simple command that will let you create a backup is cp myfile.txt myfile.bak which will create a backup copy of the file myfile.txt named myfile.bak.
From time to time, when working with Unix and Unix-based operating systems, you will hear reference made to a shell. This is simply a program that handles commands for the operating system. There are many shells for Unix-based operating systems. The most common one is called "bash". In the event that for some reason you need to access a shell which is not currently installed on your machine, you can likely find it on the Internet.