Difference between revisions of "Command line tutorial"

From Dreamwidth Notes
Jump to: navigation, search
(first pass, saving)
 
(checkpoint commit)
Line 1: Line 1:
[[Category: Getting Started]][[Category: Development]][[Category: Dreamhack]
+
[[Category: Getting Started]][[Category: Development]][[Category: Dreamhack]]
  
 
<b>Help! My Dreamhack has a command line!</b>
 
<b>Help! My Dreamhack has a command line!</b>
Line 32: Line 32:
  
 
It's useful to have a long command line like that, because it'll remind you where you are, who you're logged in as, and what directory you're in. (Those of us who often have five or six shell windows open all the time, to various machines, sometimes get lost.)
 
It's useful to have a long command line like that, because it'll remind you where you are, who you're logged in as, and what directory you're in. (Those of us who often have five or six shell windows open all the time, to various machines, sometimes get lost.)
 +
 +
==Command flags==
 +
 +
By default, to execute a command, you just type the command. There are generally other options to a command, though -- things that make the command behave differently -- and those are known as 'flags'. To use a command with a flag, you type the flag after the command name, with a dash in front of it.
 +
 +
As we go through the tutorial, you'll see commands with flags for some other options. Flags are generally single letters, and remembering what single letter stands for what can often be annoying, but as time goes on you'll start to remember them.
 +
 +
Some commands have both flags and "long options" that are aliases of each other. Long options are full-word options, and they're called with two dashes instead of a single dash. (You generally won't need to know this, but you might see references to this style of option in tutorials elsewhere.)
  
 
==Listing files and directories==
 
==Listing files and directories==
Line 46: Line 54:
 
On Dreamhacks, the output is color-coded, so directories are blue, executable files are green, and plain files are white. (More about permissions and the difference between executable and non-executable files in a bit.)
 
On Dreamhacks, the output is color-coded, so directories are blue, executable files are green, and plain files are white. (More about permissions and the difference between executable and non-executable files in a bit.)
  
 +
By default, 'ls' doesn't show you all the files in a directory, though. Any file that starts with a period (sometimes known as a dot-file) is a "hidden", system file.  They're usually used for autogenerated preferences files. You generally shouldn't need to mess with them, but there are times when you explicitly want to set your preferences for something, or want to see what the hidden files in a directory are.
 +
 +
To show all files in a directory, use the -a flag (for "all"):
 +
 +
<source lang="bash">dh-rahaeli@hack:~$ ls -a
 +
.              .bash_logout  .joe_state    .vim      apache       
 +
..            .bash_profile  .nano_history  .viminfo  bin
 +
.bash_aliases  .bashrc        .plan          .vimrc    crontab-import
 +
.bash_history  .forward      .subversion    README    dw
 +
</source>
 +
 +
You can also get more information about a file, in a "long form" directory listing. This includes information like who owns the file, what the permissions on the file are, and when it was last changed. (Permissions and ownership are something you shouldn't have to worry much about, but 'last changed' can be useful.) To get the long form, use the -l flag:
 +
 +
<source lang="bash">dh-rahaeli@hack:~$ ls -l
 +
total 32
 +
-rw-r--r--  1 dh-rahaeli dreamhack  3542 Feb 13 11:53 README
 +
drwxr-xr-x  5 dh-rahaeli dreamhack  4096 Jul 28  2008 apache
 +
drwxr-xr-x  2 dh-rahaeli dreamhack  4096 Jul 20 04:40 bin
 +
-rw-r--r--  1 dh-rahaeli dreamhack  1617 Feb 20 13:42 crontab-import
 +
drwxr-xr-x 16 dh-rahaeli dreamhack  4096 Jul 20 04:40 dw
 +
</source>
 +
 +
The output of the -l flag is, in order: permissions, block size (ignore this), owner, group owner, file size (in bytes), date last modified, and the file name.
 +
 +
You can combine those two flags to get "long form listing of all files" by using "ls -al".
 +
 +
If you find the raw byte file size to be hard to read, you can use the flags -lh instead of just -l. This shows you the size in human-readable form:
 +
 +
<source lang="bash">dh-rahaeli@hack:~$ ls -lh
 +
total 32K
 +
-rw-r--r--  1 dh-rahaeli dreamhack 3.5K Feb 13 11:53 README
 +
drwxr-xr-x  5 dh-rahaeli dreamhack 4.0K Jul 28  2008 apache
 +
drwxr-xr-x  2 dh-rahaeli dreamhack 4.0K Jul 20 04:40 bin
 +
-rw-r--r--  1 dh-rahaeli dreamhack 1.6K Feb 20 13:42 crontab-import
 +
drwxr-xr-x 16 dh-rahaeli dreamhack 4.0K Jul 20 04:40 dw</source>
  
 
==Moving from directory to directory==
 
==Moving from directory to directory==
 +
 +
Moving from directory to directory is done with the 'cd' command, for '<em>c</em>hange <em>d</em>irectory'. It takes the argument of the directory you want to move into. For instance, if I'm in my home directory and I want to move into the 'dw' directory, I type:
 +
 +
<source lang="bash">dh-rahaeli@hack:~$ cd dw
 +
dh-rahaeli@hack:~/dw$ </source>
 +
 +
(Notice that the prompt changes to reflect what  directory I'm in right now.)
 +
 +
There's an easy shortcut for "directory above the directory I'm in right now" so you don't have to type out the absolute path, which is '../'. If you type that, it brings you to the directory above the directory you're in:
 +
 +
<source lang="bash">dh-rahaeli@hack:~/dw$ cd ../
 +
dh-rahaeli@hack:~$ </source>
 +
 +
You can combine directories to go through multiple levels at once, too. If I wanted to cd from my home directory into the ~/dw/cvs/dw-free directory, I don't have to do it one level at a time; I can just type:
 +
 +
<source lang="bash">dh-rahaeli@hack:~$ cd dw/cvs/dw-free
 +
dh-rahaeli@hack:~/dw/cvs/dw-free$ </source>
 +
 +
And you can go up and then over, using the ../ reference multiple times to mean "directory above the directory above", etc. If I'm in the ~/dw/cvs/dw-free/htdocs/manage/settings directory, and I want to go to the ~/dw/htdocs/manage/settings directory, that would look like:
 +
 +
<source lang=bash>dh-rahaeli@hack:~/dw/cvs/dw-free/htdocs/manage/settings$ cd ../../../../../htdocs/manage/settings
 +
dh-rahaeli@hack:~/dw/htdocs/manage/settings$ </source>
 +
 +
If you just type 'cd' without any arguments, it brings you to your home directory.
 +
 +
You can combine using relative directory paths with listing files, copying files, removing files, etc. For instance, if you were in your home directory, and you want to list the contents of the ~/dw/cvs/ directory in long mode, you could type:
 +
 +
<source lang=bash>ls -l dw/cvs</source>
 +
 +
If you were in the ~/dw/cvs/dw-free directory, and wanted to list the contents of the ~/dw/cvs/dw-nonfree directory, that would look like:
 +
 +
<source lang=bash>dh-rahaeli@hack:~/dw/cvs/dw-free$ ls ../dw-nonfree
 +
bin  cgi-bin  cvs  etc  htdocs</source>
  
 
==Copying files==
 
==Copying files==
 +
 +
To copy one file to a new file name, the command is 'cp'. If I have a file named "file.txt", and I want to make a copy to "file.txt.backup", it would look like this:
 +
 +
<source lang=bash>dh-rahaeli@hack:~$ ls
 +
README  apache  bin  crontab-import  dw  file.txt
 +
dh-rahaeli@hack:~$ cp file.txt file.txt.backup
 +
dh-rahaeli@hack:~$
 +
dh-rahaeli@hack:~$ ls
 +
README  apache  bin  crontab-import  dw  file.txt  file.txt.backup
 +
</source>
 +
 +
This <em>will</em> overwrite anything that already exists in file.txt.backup. If you're can't remember whether or not you already have a "file.txt.backup" file, and you want it to check before doing the moving, use the -i flag, which will ask you if you want to overwrite it:
 +
 +
<source lang=bash>dh-rahaeli@hack:~$ ls
 +
README  apache  bin  crontab-import  dw  file.txt  file.txt.backup
 +
dh-rahaeli@hack:~$ cp -i file.txt file.txt.backup
 +
cp: overwrite `file.txt.backup'? n
 +
dh-rahaeli@hack:~$ </source>
 +
 +
You can combine using directory paths with copying files. If you're in your home directory, and you want to copy "file.txt" to ~/dw/cvs/dw-free, you can do that like this:
 +
 +
<source lang=bash>cp file.txt dw/cvs/dw-free</source>
 +
 +
(If you specify a directory for the destination, it will copy it into the directory. If you specify a file for the destination, it will overwrite that file. It's a good idea to use the -i flag if you're copying things around, in case you get the directory structure wrong.)
 +
 +
If the directory path you specify doesn't exist, you'll get an error message:
 +
 +
<source lang=bash>dh-rahaeli@hack:~$ cp file.txt dw/cvs/dw-missing/foo/bar/blah
 +
cp: cannot create regular file `dw/cvs/dw-missing/foo/bar/blah': No such file or directory
 +
</source>
 +
 +
This just means that you got your directory structure wrong.
 +
 +
==Moving files==
 +
 +
The command to move or rename files is 'mv'. It works exactly like 'cp', only instead of copying the source file to the destination file, it renames it.
 +
 +
You can use it to rename files:
 +
 +
<source lang=bash>dh-rahaeli@hack:~$ ls
 +
README  apache  bin  crontab-import  dw  file.txt
 +
dh-rahaeli@hack:~$ mv file.txt newfilename.txt
 +
dh-rahaeli@hack:~$ ls
 +
README  apache  bin  crontab-import  dw  newfilename.txt
 +
dh-rahaeli@hack:~$ </source>
 +
 +
You can also use it to move files from one directory to another:
 +
 +
<source lang=bash>dh-rahaeli@hack:~$ ls
 +
README  apache  bin  crontab-import  dw  newfilename.txt
 +
dh-rahaeli@hack:~$ ls dw
 +
LICENSE  bin      cvs  etc    locks  src      t    test
 +
README  cgi-bin  doc  htdocs  logs  ssldocs  temp  var
 +
dh-rahaeli@hack:~$ mv newfilename.txt dw
 +
dh-rahaeli@hack:~$ ls dw
 +
LICENSE  bin      cvs  etc    locks  newfilename.txt  ssldocs  temp  var
 +
README  cgi-bin  doc  htdocs  logs  src              t        test
 +
dh-rahaeli@hack:~$ ls
 +
README  apache  bin  crontab-import  dw </source>
 +
 +
If you want to combine moving a file from one directory to another <em>and</em> changing the file name, you need to specify the new file name and the directory location all at once when you move it:
 +
 +
<source lang="bash">dh-rahaeli@hack:~$ ls
 +
README  apache  bin  crontab-import  dw  filename.txt
 +
dh-rahaeli@hack:~$ mv filename.txt dw/newfilename.txt
 +
dh-rahaeli@hack:~$ ls
 +
README  apache  bin  crontab-import  dw
 +
dh-rahaeli@hack:~$ ls dw
 +
LICENSE  bin      cvs  etc    locks  newfilename.txt  ssldocs  temp  var
 +
README  cgi-bin  doc  htdocs  logs  src              t        test
 +
dh-rahaeli@hack:~$</source>
 +
  
 
==Deleting files==
 
==Deleting files==
Line 60: Line 208:
  
 
==Wildcards==
 
==Wildcards==
 +
 +
==Tab complete==
  
 
==Processes and jobs==
 
==Processes and jobs==
Line 65: Line 215:
  
  
 +
==Getting help==
 +
 +
If you ever get stuck and can't remember how to work a command, you can get help on it. The help files are known as 'man pages' (for manual), and are accessed with the 'man' command. For instance, to get help on the help command, you'd type:
 +
 +
<source lang="bash">man man</source>
  
 +
To exit the help viewer, hit q. Not all of the man pages for the basic utilities are necessarily helpful, but in a pinch they might give you a hand.
  
 
==Outside Resources==
 
==Outside Resources==

Revision as of 14:50, 6 August 2009


Help! My Dreamhack has a command line!

If you've never used a Unix-based operating system before, the command line can be intimidating. Fear not! Working with files and data on the command line is actually fairly easy, and once you get used to it, you'll wonder how you ever lived without it.

The key thing to remember in unix commands is that they date from the days when vowels were an endangered species they are often shortened, contracted, or otherwise non-obvious at first glance. This is to make them easier to type -- the general theory is that it's nicer to type 'ls' sixteen thousand times a day than it is to type 'list'. They're generally designed to be typed quickly on a QWERTY keyboard.

Connecting

The window where you connect to your Dreamhack and type commands is also known as a 'shell window', for reasons that don't need explaining at this juncture in time. In order to connect to your Dreamhack, you'll need a program capable of doing SSH (Secure Shell) connections. For Windows, the best program is PuTTY. If you're on a Mac you won't need a separate program; the Terminal (Applications/Utilities/Terminal) will let you connect.

Directories

Everything in Unix is either a process (a running program), a file, or a directory. Files live in directories, which can themselves contain directories, and so on.

When you first log into your Dreamhack, you'll be in your home directory. This is a special directory that stores all of your files and folders, and serves as the root for a lot of commands if you don't otherwise specify an argument. Your home directory is also known as "~" for short.

On the Dreamhack system, your home directory is located in /dreamhack/home/8XXX-yourusername. For instance, if my Dreamhack username is 'dh-rahaeli', and my assigned number is 8083, my home directory is located at /dreamhack/home/8083-rahaeli.

Directory paths are given with slashes separating the directory tree -- each slash represents another directory down the tree. If the directory path starts with a slash, it is an absolute path: it's counted from the root of the machine, the directory that holds all other directories. If a directory path doesn't have a slash before it, it's a relative path, and is usually counted from the directory you're in at the moment. (There are some exceptions to this, but I'll get into that in a bit.)

Command prompt

The command prompt is where you type in what you want the system to do. On a Dreamhack, it looks like this:

dh-rahaeli@hack:~$

Breaking that down into component pieces, it's: your username @ the machine you're on : (your present working directory). So, if you're in the ~/dw/cvs/dw-free directory (and remember, ~ means your home directory, so in this case it's shorthand for '/dreamhack/home/8083-rahaeli'), your prompt will look like:

dh-rahaeli@hack:~/dw/cvs/dw-free$

It's useful to have a long command line like that, because it'll remind you where you are, who you're logged in as, and what directory you're in. (Those of us who often have five or six shell windows open all the time, to various machines, sometimes get lost.)

Command flags

By default, to execute a command, you just type the command. There are generally other options to a command, though -- things that make the command behave differently -- and those are known as 'flags'. To use a command with a flag, you type the flag after the command name, with a dash in front of it.

As we go through the tutorial, you'll see commands with flags for some other options. Flags are generally single letters, and remembering what single letter stands for what can often be annoying, but as time goes on you'll start to remember them.

Some commands have both flags and "long options" that are aliases of each other. Long options are full-word options, and they're called with two dashes instead of a single dash. (You generally won't need to know this, but you might see references to this style of option in tutorials elsewhere.)

Listing files and directories

To list files:

ls

This returns all the files in a directory, formatted like this:

dh-rahaeli@hack:~$ ls
README  apache  bin  crontab-import  dw

On Dreamhacks, the output is color-coded, so directories are blue, executable files are green, and plain files are white. (More about permissions and the difference between executable and non-executable files in a bit.)

By default, 'ls' doesn't show you all the files in a directory, though. Any file that starts with a period (sometimes known as a dot-file) is a "hidden", system file. They're usually used for autogenerated preferences files. You generally shouldn't need to mess with them, but there are times when you explicitly want to set your preferences for something, or want to see what the hidden files in a directory are.

To show all files in a directory, use the -a flag (for "all"):

dh-rahaeli@hack:~$ ls -a
.              .bash_logout   .joe_state     .vim      apache         
..             .bash_profile  .nano_history  .viminfo  bin
.bash_aliases  .bashrc        .plan          .vimrc    crontab-import
.bash_history  .forward       .subversion    README    dw

You can also get more information about a file, in a "long form" directory listing. This includes information like who owns the file, what the permissions on the file are, and when it was last changed. (Permissions and ownership are something you shouldn't have to worry much about, but 'last changed' can be useful.) To get the long form, use the -l flag:

dh-rahaeli@hack:~$ ls -l
total 32
-rw-r--r--  1 dh-rahaeli dreamhack  3542 Feb 13 11:53 README
drwxr-xr-x  5 dh-rahaeli dreamhack  4096 Jul 28  2008 apache
drwxr-xr-x  2 dh-rahaeli dreamhack  4096 Jul 20 04:40 bin
-rw-r--r--  1 dh-rahaeli dreamhack  1617 Feb 20 13:42 crontab-import
drwxr-xr-x 16 dh-rahaeli dreamhack  4096 Jul 20 04:40 dw

The output of the -l flag is, in order: permissions, block size (ignore this), owner, group owner, file size (in bytes), date last modified, and the file name.

You can combine those two flags to get "long form listing of all files" by using "ls -al".

If you find the raw byte file size to be hard to read, you can use the flags -lh instead of just -l. This shows you the size in human-readable form:

dh-rahaeli@hack:~$ ls -lh
total 32K
-rw-r--r--  1 dh-rahaeli dreamhack 3.5K Feb 13 11:53 README
drwxr-xr-x  5 dh-rahaeli dreamhack 4.0K Jul 28  2008 apache
drwxr-xr-x  2 dh-rahaeli dreamhack 4.0K Jul 20 04:40 bin
-rw-r--r--  1 dh-rahaeli dreamhack 1.6K Feb 20 13:42 crontab-import
drwxr-xr-x 16 dh-rahaeli dreamhack 4.0K Jul 20 04:40 dw

Moving from directory to directory

Moving from directory to directory is done with the 'cd' command, for 'change directory'. It takes the argument of the directory you want to move into. For instance, if I'm in my home directory and I want to move into the 'dw' directory, I type:

dh-rahaeli@hack:~$ cd dw
dh-rahaeli@hack:~/dw$

(Notice that the prompt changes to reflect what directory I'm in right now.)

There's an easy shortcut for "directory above the directory I'm in right now" so you don't have to type out the absolute path, which is '../'. If you type that, it brings you to the directory above the directory you're in:

dh-rahaeli@hack:~/dw$ cd ../
dh-rahaeli@hack:~$

You can combine directories to go through multiple levels at once, too. If I wanted to cd from my home directory into the ~/dw/cvs/dw-free directory, I don't have to do it one level at a time; I can just type:

dh-rahaeli@hack:~$ cd dw/cvs/dw-free
dh-rahaeli@hack:~/dw/cvs/dw-free$

And you can go up and then over, using the ../ reference multiple times to mean "directory above the directory above", etc. If I'm in the ~/dw/cvs/dw-free/htdocs/manage/settings directory, and I want to go to the ~/dw/htdocs/manage/settings directory, that would look like:

dh-rahaeli@hack:~/dw/cvs/dw-free/htdocs/manage/settings$ cd ../../../../../htdocs/manage/settings
dh-rahaeli@hack:~/dw/htdocs/manage/settings$

If you just type 'cd' without any arguments, it brings you to your home directory.

You can combine using relative directory paths with listing files, copying files, removing files, etc. For instance, if you were in your home directory, and you want to list the contents of the ~/dw/cvs/ directory in long mode, you could type:

ls -l dw/cvs

If you were in the ~/dw/cvs/dw-free directory, and wanted to list the contents of the ~/dw/cvs/dw-nonfree directory, that would look like:

dh-rahaeli@hack:~/dw/cvs/dw-free$ ls ../dw-nonfree
bin  cgi-bin  cvs  etc  htdocs

Copying files

To copy one file to a new file name, the command is 'cp'. If I have a file named "file.txt", and I want to make a copy to "file.txt.backup", it would look like this:

dh-rahaeli@hack:~$ ls
README  apache  bin  crontab-import  dw  file.txt 
dh-rahaeli@hack:~$ cp file.txt file.txt.backup
dh-rahaeli@hack:~$ 
dh-rahaeli@hack:~$ ls
README  apache  bin  crontab-import  dw  file.txt  file.txt.backup

This will overwrite anything that already exists in file.txt.backup. If you're can't remember whether or not you already have a "file.txt.backup" file, and you want it to check before doing the moving, use the -i flag, which will ask you if you want to overwrite it:

dh-rahaeli@hack:~$ ls
README  apache  bin  crontab-import  dw  file.txt  file.txt.backup 
dh-rahaeli@hack:~$ cp -i file.txt file.txt.backup
cp: overwrite `file.txt.backup'? n
dh-rahaeli@hack:~$

You can combine using directory paths with copying files. If you're in your home directory, and you want to copy "file.txt" to ~/dw/cvs/dw-free, you can do that like this:

cp file.txt dw/cvs/dw-free

(If you specify a directory for the destination, it will copy it into the directory. If you specify a file for the destination, it will overwrite that file. It's a good idea to use the -i flag if you're copying things around, in case you get the directory structure wrong.)

If the directory path you specify doesn't exist, you'll get an error message:

dh-rahaeli@hack:~$ cp file.txt dw/cvs/dw-missing/foo/bar/blah
cp: cannot create regular file `dw/cvs/dw-missing/foo/bar/blah': No such file or directory

This just means that you got your directory structure wrong.

Moving files

The command to move or rename files is 'mv'. It works exactly like 'cp', only instead of copying the source file to the destination file, it renames it.

You can use it to rename files:

dh-rahaeli@hack:~$ ls
README  apache  bin  crontab-import  dw  file.txt 
dh-rahaeli@hack:~$ mv file.txt newfilename.txt
dh-rahaeli@hack:~$ ls
README  apache  bin  crontab-import  dw  newfilename.txt 
dh-rahaeli@hack:~$

You can also use it to move files from one directory to another:

dh-rahaeli@hack:~$ ls
README  apache  bin  crontab-import  dw  newfilename.txt 
dh-rahaeli@hack:~$ ls dw
LICENSE  bin      cvs  etc     locks  src      t     test
README   cgi-bin  doc  htdocs  logs   ssldocs  temp  var
dh-rahaeli@hack:~$ mv newfilename.txt dw
dh-rahaeli@hack:~$ ls dw
LICENSE  bin      cvs  etc     locks  newfilename.txt  ssldocs  temp  var
README   cgi-bin  doc  htdocs  logs   src              t        test
dh-rahaeli@hack:~$ ls
README  apache  bin  crontab-import  dw

If you want to combine moving a file from one directory to another and changing the file name, you need to specify the new file name and the directory location all at once when you move it:

dh-rahaeli@hack:~$ ls
README  apache  bin  crontab-import  dw  filename.txt 
dh-rahaeli@hack:~$ mv filename.txt dw/newfilename.txt
dh-rahaeli@hack:~$ ls
README  apache  bin  crontab-import  dw 
dh-rahaeli@hack:~$ ls dw
LICENSE  bin      cvs  etc     locks  newfilename.txt  ssldocs  temp  var
README   cgi-bin  doc  htdocs  logs   src              t        test
dh-rahaeli@hack:~$


Deleting files

Searching files

Skimming through files

Redirecting output

Wildcards

Tab complete

Processes and jobs

Getting help

If you ever get stuck and can't remember how to work a command, you can get help on it. The help files are known as 'man pages' (for manual), and are accessed with the 'man' command. For instance, to get help on the help command, you'd type:

man man

To exit the help viewer, hit q. Not all of the man pages for the basic utilities are necessarily helpful, but in a pinch they might give you a hand.

Outside Resources

Good tutorials on Unix/Linux work: