# Linux Guide/merge/Linux For Newbies/Command Line
## Why Should I Learn The Command Line, Anyway?

Simply put, the command line is powerful; an army of tools exist that can take what would be a tedious job in a graphical program and turn it into a task that takes just a few seconds. Removing the last four lines in every row of a large file would be a lengthy process in a graphical application, but can become trivial and automated on the command line.
Flexibility aside, it's also important to note that some Linux systems lack a graphical interface at all and that some systems may become damaged in such a way as to make it impossible to bring up anything other than the command prompt. In these cases it's important to be able to navigate on the command line with enough proficiency to perform whatever tasks need to be done from backing up some files to disabling a dying piece of hardware.
The commands used at the command line may seem a little cryptic due to their tendency to be very short. This is because the roots of the Linux command line are from systems where a single letter entry could take a significant amount of time to travel from a terminal, to a central server and back to the terminal where it was printed onto a roll of paper. In those old systems, the shorter the input was, the better, as it meant less time waiting to issue your command and receive output. The best thing you can do to remember what commands stand for is to find out what word the command is an abbreviation for. This can go a long way to remembering the command later.

## Shell Prompt

For every Linux distribution the command line prompt will look a little different. For example, on one system you might see your username, the '@' symbol, the machine name, your current directory and some kind of symbol to indicate the end of the prompt (here, a dollar sign).
This is a very common prompt. You may also see your username, a space, the fully qualified domain name of the computer, the full path to your present working directory followed by a symbol to indicate the end of the prompt (a dollar sign).
The prompt varies from system to system based on a number of things. For example, it may be the default configuration set by the creators of your particular Linux distribution. It could also have been configured by the person who administers the computer. Maybe you, yourself, configured how you wanted your prompt to look. The way you configure the look of your command prompt depends on which shell you use.
The shell is the piece that most people commonly refer to as, "the command line" when, in reality, it is simply a piece of software that provides an interface to the services of a kernel. The distinction between a 'shell' and the 'command line' is simply that that a shell refers to a specific piece of software (e.g. BASH, Tch, ksh, etc.) that provides a command line interface. Most modern Linux systems use BASH (Bourne Again SHell) as the default shell.

## Some Useful Terminal Commands
#### Summary of Common Commands

* ls - This command 'lists' the contents of your present working directory.
* pwd - Shows you what your present working directory is.
* cd - Lets you change directories.
* rm - Remove one or more files.
* rmdir - Remove an empty directory.
* mkdir - Make a directory.
* ps - Provides a list of currently running processes.
* cp - Copy a file.
* mv - Move a file (this is also used to rename a file, "moving" it from one file name to another.)
* grep - The global regular expression print program lets you search through a file or output of another program.
* find - Find a file on the filesystem (100% accurate, but not fast).
* locate - Find a file on the filesystem from a cached list of files (Fast, but not 100% accurate).
* man - Displays the manual for most commands (including 'man').
* clear - clear the screen
* less - view the contents of a file
* nano - a simple text editor
* sudo - execute a command as another user (default as root)
* su - switch user (default to root)
* view - Start in read-only mode. You will be protected from writing the files. Can also be done with the "-R" argument.

### Command Arguments

Most ( tar cvf tarFile * ) ( gzip ) ( gzip -d & tar xvf )commands ( cp ) mv [filename} [location] )will need some extra information in order to do something useful. For example, the copy command will need to know what file you want to copy. The extra information is given in the form of arguments. ( while read names __ do __ ### __ done < filename )Arguments are written as extra words after the command name and separated by spaces. If you want to invoke the command ps with the argument -A, you would write:

#### NOTE

    The words argument, parameter and flag are synonyms, while a "switch" is actually a type of argument that indicates an on or off status. Arguments often need additional information (such as a filename) while a switch will never require additional information except maybe "on/off", "true/false", "yes/no".
Most Linux commands use the GNU fashion of arguments: each option is either a letter preceded by a hyphen, like -o, -W or -L, or a word or phrase preceded by two hyphens, like --symbolic, --no-target-directory etc. Some commands also take file names. Each file has to be supplied as a single argument, so if your filename contains spaces, you have to either enclose it in double or single quotes, or precede the spaces with backslashes. These three commands open the same file with less:
Some characters are recognized by the shell and used for special tasks. If your file name contains the characters: <>&$\"'; and you experience problems, you should put backslashes before these characters. Note that in Linux, filenames can contain any characters except /, which is used as the directory separator, and the null character, which can't be entered from the keyboard anyway.

### Using Common Commands
#### Using ls - List

You may notice from the example, default output of 'ls' that the files are not in strict alphabetical order. This is because the output is sorted on the numeric ASCII value of each letter. Because upper case letters have a lower ASCII value than lowercase letters, the returned result will always be files starting with upper case letters first. For example, if we add the file AZTRIP to the directory we will get 'AZTRIP' as the file listed first followed by 'Afilez' because the ASCII value of 'Z' is numerically lower than the ASCII value of 'f'.
If the ASCII values of letters has you confused, the important thing to remember here is that uppercase letters trump lowercase letters and will appear first in listings. The result is that files are first sorted by case and sorted alphabetically second.
The default output of ls isn't as useful as it could be. For example, given the output in the example, we don't know what are files and what are directories. We can likely guess that "Afilez", "aFilea" and "vacation pictures" are all directories as they don't have an extension after the file name. To confirm this, we can pass the 'l' parameter to 'ls' to get "long listing format" output.
The first character in the output above indicates the file type; where a dash indicates a file and the letter 'd' indicates a directory. From this output we see that the only directory is "vacation pictures"

##### NOTE

    Most systems have an alias on the ls command to include the '--color' flag so that the command highlights different file types with different colors. Directories are commonly shown in blue. If the output of your 'ls' command has everything as the same color, try "ls --color".

##### TIP

    Another common parameter to the ls command is 'a' which shows all files including any hidden files and folders that start with a period. Arguments may also be combined together in a single block of letters. For example, "ls -l -a" can also be expressed as "ls -la".
The command ls -l outputs information in the following order. The first column of data lists the permissions that apply to that file or directory. The next column of information lists the number of 'hard links' to that file. If you're not sure what that means, then don't worry about it. For more information about hard links, read the manual page for ln by running man ln. The next column lists the name of the user that owns the file or directory, followed by the name of the group that owns the file or directory. The next column lists the size of the file or directory. In the case of directories, it does not list the size of everything within that directory. In the example above, it lists the size of both directories as 4096 bytes. That is because that's how much space having a directory takes up, it does not mean that there are 4096 bytes of stuff within that directory. The next column lists the date that the file or directory was last modified. Finally, the name of the file or directory is listed. To find out more about the command ls, run the command man ls and you will be presented with ls's manual page.

#### Using cd - Change Directory

    Also see the entry for cd in the Guide to Unix/Commands wikibook.
The cd command lets you navigate from directory to directory on your system. The first thing you may need to know when using the cd command is that, unlike windows, Linux does not use the concept of drive letters. Instead, everything is somewhere within the root directory, '/', including other disk drives.
There's a couple ways to use cd to navigate to the directory you want. These are known as "full path" and "relative path".
Full path always begins with the root directory '/'. For example, if we're in our home directory '/home/user' and we want to navigate to the 'vacation pictures' directory, we would issue the following command.
You may note that our prompt changed a little to indicate the directory we're currently in. This will be the case on most systems, but as previously discussed, this depends on how your shell is configured. You may also note the backslash between "vacation" and " pictures". This backslash is known as the "escape character" and tells the cd command that the space is part of the directory name. If we omitted the backslash, the cd command would error and tell us that it could not find the directory "vacation". If you don't like typing backslash or have a directory name with many spaces in it, you can use quotation marks around the path.
Relative path can start a number of ways, but always describes how to get to a specific directory from the directory you're currently in. To do this, we need to know how to move to directories that immediately surround the directory we're currently in. These directories are parent directories and sub-directories
A sub-directory is any directory that is contained within the directory we're currently. Given the output below of "ls -l", we see that chicago, florida and "new york" are all sub-directories to the "vacation pictures" directory.
A parent directory is the directory that contains the folder that we are in. This makes the "vacation pictures" folder the parent of the "chicago", "florida" and "new york" directories.
To cd to a sub-directory, you can start the path name with "./" which indicates the directory we're already in, or you can immediately start with the name of the sub-directory. For example, both of the following commands will change you to the "florida" directory.
To cd to a parent directory, you always specify "../". The following command will return us to the "vacation pictures" directory.
If we are already in the "chicago" directory and want to cd to "new york", we must first tell the system we want to go up one level to the parent directory and then to the "new york" directory. We can do this by saying "cd ../" and then "cd new\ york" or we can do it as one step using the command below.

##### NOTE

    Notice how we used quotation marks around the path to cd to the "new york" directory. We could have, instead used the backslash before the space and omitted the quotes to get the same result.
Similar to "./" and "../", there's also "~/" which indicates the home directory of the user we're logged in as. If we wanted to cd to the "vacation pictures" folder while in the "/usr/lib" directory, we could do this with any of the following commands.

##### TIP

    Using the cd command without specifying a directory will return you to your home directory.
echo -n "Please Enter your name" read name useradd $name

#### Using pwd - Print Working Directory

    Also see the entry for pwd in the Guide to Unix/Commands wikibook.
To display the full path that you are in at any time, simply run the command pwd (print working directory), and it will print the path out for you. For example:

#### Using mkdir - Make a Directory

The mkdir command allows you to create new directories.

##### NOTE

    Like we've previously seen with the cd command, we can create new directories by specifying either relative and absolute paths. You'll find you can do this with all command line programs what work with files and folders.

##### TIP

    You can pass the -p parameter to mkdir to make it create all directories to make that path valid. For example, in place of the first and second mkdir commands, we could instead run mkdir -p playground/test1 to create both the playground and test1 directories in a single step.

#### Using rmdir - Remove a Directory

The rmdir command allows you to delete empty directories.
Like Windows, Linux has the concept of hidden files. This is important to note because there may be times that you try to delete a seemingly empty directory only to be presented with an error.
To see hidden files and folders, pass the -a (all files and folders) parameter to the ls command.
All hidden files in Linux start with a period. The '.' and '..' files we see listed are the system folders that allow us to specify relative paths.

##### TIP

    If you're tired of seeing the '.' and '..' folders every time you ls -a, you can instead run ls -A for "almost all" files. This gives you all the files and hidden files excluding '.' and '..'

#### Using rm - Remove Files

The rm command will delete a specific or group of files. Be a little cautious when using this command as there's no 'recycle-bin' for the command line!
To delete the file .hiddenfile in the previous example we simply specify the file after the rm command.

##### TIP

    In addition to having several parameters, the rm command also supports the use of asterisk (*) to represent any set of characters. This can be useful when you want to delete all of specific type of file. For example, rm *.jpg will remove all the files ending in '.jpg' residing in your current directory.

##### TIP

    Until you feel comfortable with the rm command (especially when used in conjunction with an asterisk), you might want to specify the -i parameter to have rm confirm the deletion before it happens.

#### Using locate - find a file

The locate utility is very useful even when working in a graphical environment. On systems where it is installed, it periodically indexes the whole filesystem and stores the names of files in an internal database. This makes finding files by name very fast (a search for a particular name takes under a second). Here is an example:
This example searches the hard drives for all files which have "stock_3dsound" anywhere in their path. You can also use wildcards * and ? in the pattern.
Since the filenames are kept in a database, locate will not be able to find recently created files, or might find files which have been deleted. While the database is periodically updated by a script, you might want to force an update. To do this, use the command locate -u, run as root (in Ubuntu, sudo locate -u; in other distributions, first su, then locate -u).

#### Using su/sudo - run commands with Super User privileges

The super user, more commonly known as "root", has complete access to everything on the system. The root user can do silly things like treat input from the mouse as audio to things catastrophic to the system like delete every file on the filesystem. It's largely because of this last reason that the command line has a reputation as being something to be feared.

##### WARNING

    If you've been instructed to run the rm command in conjunction with asterisk (*), question it seriously before running it. There are many pranksters trolling Linux support forums hoping to trick you into deleting every file on your filesystem. Other variations have you mv your files to /dev/null which has the same effect. Other variations also involve rm -r -f as root which deletes every file on your filesystem.

##### su

The function of the `su` (super user) command allows you to switch to a different user on the system. Typically it is used to switch to the 'root' user on the system. To become root on most systems, give the su command with no parameters. You will then be prompted for the root password.
Note the changes to the prompt. These are the typical changes you can expect to see. The username changed from 'user' to 'root' to reflect the change in which user we're operating as. The directory changed because the home directory for 'user' (e.g. /home/user) is not the same as the home directory for root (typically /root). The last change is character at the end of the prompt. The pound sign (#) is the typical symbol used to indicate you are now running commands as root.
To switch to a different user, use the su command followed by the username you want to switch to.
Here we've successfully switched to the user 'fred'. Fred is not the root user, so the end symbol remains a dollar sign. However, Fred has made custom changes to how his prompt looks. It provides the fully qualified domain of the machine followed by the full path the user is currently in.

###### NOTE

    The password needed in this example is Fred's user password. The root password will not work.
Once you su to the root user, you can su to any other user without needing their password. To su to 'fred' without needing his password, we first su to root as an intermediary step.

##### sudo

The sudo command is similar to su in that it lets you run commands with super user privileges. However, the philosophy on how super user privileges are give are quite a bit different.
| su | sudo |
| Run once and all subsequent commands will be ran as root. | Must be ran every time as a prefix to the command you wish to run as root |
| Gives total access to all commands and files on the system | Which commands (and subsequently, files) a user has access to can be limited. |
| One password that must be shared with users you wish to give root access to. Note that the act of sharing a password increases the chance that the password will be intercepted. | Password used for root access is the same as the users' password. Note that the greater the number of people with root access, the more likely one of these passwords can be guessed or phished from a user. |
| User must remember to log out of root when done. | User must wait for the command to time-out before it will prompt for a password again. User can potentially become root as though they ran su and subsequently may still need to remember to log out of root when done. |
To become the user fred using the sudo command looks a bit redundant.

###### NOTE

    The password needed in this example is your own user password.

###### NOTE

    You can still use the command su fred to become the fred user, but again, you'll need Fred's password to do this.
Think of the sudo command as meaning something like, "Become root and run the following command." This makes the above command the sudo equivalent to the su example when we became root as an intermediary step before running su to become the user fred.
There are time when you may need to run more than one command as the root user. This can make using the sudo tedious and redundant, having to retype 'sudo' each time as illustrated in the following example.
We can cut down on the amount of redundant typing by using sudo on bash to become root, first.

###### NOTE

    sudo and bash weren't made with the expectation they'd be used this way. This is why it's important to exit back to our own user account when we're done.

###### TIP

    To become root, you can also use the command sudo -s.

## Getting Help on Commands - Man Pages

Many shell commands will accept various options. To get some information about a command and a list of the available options, use the man (short for 'manual') command, giving the name of the command you want to find more about as its argument. After entering the command man who, the following output is displayed:
Most manuals are several screens long. Use the arrows or Page Up / Page Down keys to navigate in the text. Space shows the next line of text. Pressing q quits the manual and returns to the shell.
We can see from the man page that we can use the -a option to display more information about the currently active users sessions:
Besides reading help files for programs, man can also be used to get information on shells, functions, and libraries.
TODO: Add sections about these commands (except clear)

### View a File

    Also see the chapter on file viewing in the Guide to Unix/Commands wikibook.
There are many ways to view and edit files on Linux. This section will only cover viewing files (not editing). In order to simply print out the contents of a text file, the program cat is normally used. (The name cat is an abbreviation for 'concatenate'). For example:
This works well for short files, but if the file is longer than your terminal the beginning of the file will be past the top of your terminal and you won't be able to read it. Luckily, there is a program called less that will help us here. Running the program less with the command less file1 will allow you to scroll up and down through the contents of the file by using the up and down arrow keys. In order to quit less hit the 'Q' key. If you want to use less to search for a word in the file, type the '/' key, enter the word you want to search for and hit the <ENTER> key. You can then hit the 'N' key to jump to each occurrence of the word you searched for. For more information about less or cat, run the commands man less and man cat respectively to read each program's manual page.

## Directory and File Completion

Some files have very long names and typing them is not fun. Because of this, the standard Linux shell has an auto-completion feature. Try typing in the first few letters of the name of a file that is in your current directory and then pressing Tab. The name will be automatically completed. If there is more than one file beginning with those letters, the shell will complete the name only partially, and hitting Tab twice will give you a listing of all matching files. If the list is very long, the shell will ask for confirmation. Here is an example:
The shell knows about cd and will not list files when auto-completing the argument to this command.