Difference between revisions of "Command line tutorial"
(useful caveat) |
(copying, moving ... how about editing?) |
||
(12 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{Cleanup|text=As mentioned on talk page, searching section is also on Dev Finding Things. This part should probably be rewritten to link to the other article instead.}} | |
+ | |||
<b>Help! My Dreamhack has a command line!</b> | <b>Help! My Dreamhack has a command line!</b> | ||
Line 9: | Line 10: | ||
To execute a command, you type the command and hit enter/return. The command won't execute until you hit enter. | To execute a command, you type the command and hit enter/return. The command won't execute until you hit enter. | ||
− | + | Command not working? Check case as well as spelling. | |
+ | |||
+ | =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 [http://www.chiark.greenend.org.uk/~sgtatham/putty/ PuTTY]. If you're on a Mac you won't need a separate program; the Terminal (Applications/Utilities/Terminal) will let you connect. | 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 [http://www.chiark.greenend.org.uk/~sgtatham/putty/ PuTTY]. If you're on a Mac you won't need a separate program; the Terminal (Applications/Utilities/Terminal) will let you connect. | ||
Line 15: | Line 18: | ||
Details on connecting to your Dreamhack can be found in [[Dreamhack getting started]]. | Details on connecting to your Dreamhack can be found in [[Dreamhack getting started]]. | ||
− | + | =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. | 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. | ||
Line 25: | Line 28: | ||
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 <em>absolute</em> 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 <em>relative</em> 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.) | 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 <em>absolute</em> 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 <em>relative</em> 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: | 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: | 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.) | 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= | |
There are generally other options to a command -- 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. | There are generally other options to a command -- 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. | ||
Line 45: | Line 48: | ||
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.) | 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.) | ||
− | + | =Tab complete= | |
If you have a file called thisisareallylongandridiculousfilenametobetyping.txt, or a directory called thisisaridiculousdirectoryname, the good news is that you don't need to type it out every time you need to refer to it. The Dreamhack shell offers tab completion. Type a few characters and hit tab, and the machine will complete the name for you. | If you have a file called thisisareallylongandridiculousfilenametobetyping.txt, or a directory called thisisaridiculousdirectoryname, the good news is that you don't need to type it out every time you need to refer to it. The Dreamhack shell offers tab completion. Type a few characters and hit tab, and the machine will complete the name for you. | ||
Line 52: | Line 55: | ||
If you get a beep and part of the filename fills in, but not all, it means there are multiple matches that have some text in common past the point you typed, but not all. It will fill in as much as it can and then wait for you to either hit return or type more of the filename. For instance, if you have the files "update.bml", "update.bml.txt", and "uploadpic.bml.text", and you type "upd" and hit tab, it'll beep and fill in "update.bml", then wait to see if you mean "update.bml" or "update.bml.txt". | If you get a beep and part of the filename fills in, but not all, it means there are multiple matches that have some text in common past the point you typed, but not all. It will fill in as much as it can and then wait for you to either hit return or type more of the filename. For instance, if you have the files "update.bml", "update.bml.txt", and "uploadpic.bml.text", and you type "upd" and hit tab, it'll beep and fill in "update.bml", then wait to see if you mean "update.bml" or "update.bml.txt". | ||
+ | |||
+ | If you hit tab twice, it'll show you all of the possible completions. This can be useful if you forgot what the heck you called the file and don't feel like deleting the command to go look. | ||
Tab complete only works for files and directories, not for commands. | Tab complete only works for files and directories, not for commands. | ||
− | + | =Don't use spaces= | |
You can't use spaces in Unix directory or filenames. If you try to, the system will think you mean two separate files or directories. | You can't use spaces in Unix directory or filenames. If you try to, the system will think you mean two separate files or directories. | ||
− | + | (This is actually a lie, but the process of dealing with files and directories with spaces is complicated. None of the existing DW files or directories have spaces, though, so you shouldn't need to know how to handle them, and you shouldn't make files or directories with spaces yourself.) | |
− | To list files: | + | =Listing files and directories= |
− | < | + | |
+ | To list files, type: | ||
+ | |||
+ | <code>ls</code> | ||
This returns all the files in a directory, formatted like this: | This returns all the files in a directory, formatted like this: | ||
− | + | dh-rahaeli@hack:~$ '''ls''' | |
− | README apache bin crontab-import dw | + | 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.) | 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.) | ||
+ | |||
+ | If you would like the information in a format that's accessible without color-coding, you can use the -F flag (note that the F is upper-case): | ||
+ | |||
+ | dh-daily@newhack:~$ '''ls -F''' | ||
+ | README apache/ bin/ crontab-import dw/ | ||
+ | |||
+ | In this mode, directories will have a slash ("/") added, executable files will have an asterisk ("*") added, and plain files won't have anything added. | ||
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. | 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. | ||
Line 76: | Line 90: | ||
To show all files in a directory, use the -a flag (for "all"): | 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_logout .joe_state .vim apache |
− | .. .bash_profile .nano_history .viminfo bin | + | .. .bash_profile .nano_history .viminfo bin |
− | .bash_aliases .bashrc .plan .vimrc crontab-import | + | .bash_aliases .bashrc .plan .vimrc crontab-import |
− | .bash_history .forward .subversion README dw | + | .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: | 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 | + | total 32 |
− | -rw-r--r-- 1 dh-rahaeli dreamhack 3542 Feb 13 11:53 README | + | -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 5 dh-rahaeli dreamhack 4096 Jul 28 2008 apache |
− | drwxr-xr-x 2 dh-rahaeli dreamhack 4096 Jul 20 04:40 bin | + | 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 | + | -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 | + | 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. | 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. | ||
Line 100: | Line 112: | ||
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: | 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 | + | total 32K |
− | -rw-r--r-- 1 dh-rahaeli dreamhack 3.5K Feb 13 11:53 README | + | -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 5 dh-rahaeli dreamhack 4.0K Jul 28 2008 apache |
− | drwxr-xr-x 2 dh-rahaeli dreamhack 4.0K Jul 20 04:40 bin | + | 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 | + | -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 | + | 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 '<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: | 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: | ||
− | + | dh-rahaeli@hack:~$ '''cd dw''' | |
− | dh-rahaeli@hack:~/dw$ | + | dh-rahaeli@hack:~/dw$ |
(Notice that the prompt changes to reflect what directory I'm in right now.) | (Notice that the prompt changes to reflect what directory I'm in right now.) | ||
Line 119: | Line 131: | ||
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: | 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:~$ | + | 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: | 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$ | + | 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: | 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$ | + | dh-rahaeli@hack:~/dw/htdocs/manage/settings$ |
If you just type 'cd' without any arguments, it brings you to your home directory. | If you just type 'cd' without any arguments, it brings you to your home directory. | ||
Line 136: | Line 148: | ||
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: | 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: | ||
− | < | + | <code>ls -l dw/cvs</code> |
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: | 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 | + | 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: | 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 | + | README apache bin crontab-import dw file.txt |
− | dh-rahaeli@hack:~$ cp file.txt file.txt.backup | + | 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 | |
− | README apache bin crontab-import dw file.txt file.txt.backup | + | |
− | + | ||
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: | 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: | ||
− | + | dh-rahaeli@hack:~$ '''ls''' | |
− | README apache bin crontab-import dw file.txt file.txt.backup | + | README apache bin crontab-import dw file.txt file.txt.backup |
− | dh-rahaeli@hack:~$ cp -i file.txt file.txt.backup | + | dh-rahaeli@hack:~$ '''cp -i file.txt file.txt.backup''' |
− | cp: overwrite `file.txt.backup'? n | + | cp: overwrite `file.txt.backup'? '''n''' |
− | dh-rahaeli@hack:~$ | + | 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: | 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: | ||
− | < | + | <code>cp file.txt dw/cvs/dw-free</code> |
(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 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.) | ||
Line 171: | Line 181: | ||
If the directory path you specify doesn't exist, you'll get an error message: | 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 | + | 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. | This just means that you got your directory structure wrong. | ||
− | == | + | =Editing files= |
+ | Which text editor to use is the topic of multiple religious (we're not kidding) wars in the greater Unix community. | ||
+ | |||
+ | General tips: | ||
+ | * If one text editor doesn't work well with how you think, try another one. | ||
+ | * Dreamwidth files can be VERY picky about indentation. | ||
+ | * Look up how to save and exit the text editor before opening a file to edit. | ||
+ | |||
+ | =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. | + | The command to move or rename files is 'mv'. It works exactly like 'cp' (Including overwriting the destination file if it already exists, and the -i flag to warn you first), only instead of copying the source file to the destination file, it renames it. It might seem strange to use a command whose name is obviously derived from the word "move" for renaming, but <s>it made sense to the founding fathers of unix at the time</s> with the way unix file systems work, it's exactly the same operation to both move and rename a file. |
You can use it to rename files: | You can use it to rename files: | ||
− | + | dh-rahaeli@hack:~$ '''ls''' | |
− | README apache bin crontab-import dw file.txt | + | README apache bin crontab-import dw file.txt |
− | dh-rahaeli@hack:~$ mv file.txt newfilename.txt | + | dh-rahaeli@hack:~$ '''mv file.txt newfilename.txt''' |
− | dh-rahaeli@hack:~$ ls | + | dh-rahaeli@hack:~$ '''ls''' |
− | README apache bin crontab-import dw newfilename.txt | + | README apache bin crontab-import dw newfilename.txt |
− | dh-rahaeli@hack:~$ | + | dh-rahaeli@hack:~$ |
You can also use it to move files from one directory to another: | 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 | + | README apache bin crontab-import dw newfilename.txt |
− | dh-rahaeli@hack:~$ ls dw | + | dh-rahaeli@hack:~$ '''ls dw''' |
− | LICENSE bin cvs etc locks src t test | + | LICENSE bin cvs etc locks src t test |
− | README cgi-bin doc htdocs logs ssldocs temp var | + | README cgi-bin doc htdocs logs ssldocs temp var |
− | dh-rahaeli@hack:~$ mv newfilename.txt dw | + | dh-rahaeli@hack:~$ '''mv newfilename.txt dw''' |
− | dh-rahaeli@hack:~$ ls dw | + | dh-rahaeli@hack:~$ '''ls dw''' |
− | LICENSE bin cvs etc locks newfilename.txt ssldocs temp var | + | LICENSE bin cvs etc locks newfilename.txt ssldocs temp var |
− | README cgi-bin doc htdocs logs src t test | + | README cgi-bin doc htdocs logs src t test |
− | dh-rahaeli@hack:~$ ls | + | dh-rahaeli@hack:~$ '''ls''' |
− | README apache bin crontab-import dw | + | README apache bin crontab-import dw |
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: | 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: | ||
− | + | dh-rahaeli@hack:~$ '''ls''' | |
− | README apache bin crontab-import dw filename.txt | + | README apache bin crontab-import dw filename.txt |
− | dh-rahaeli@hack:~$ mv filename.txt dw/newfilename.txt | + | dh-rahaeli@hack:~$ '''mv filename.txt dw/newfilename.txt''' |
− | dh-rahaeli@hack:~$ ls | + | dh-rahaeli@hack:~$ '''ls''' |
− | README apache bin crontab-import dw | + | README apache bin crontab-import dw |
− | dh-rahaeli@hack:~$ ls dw | + | dh-rahaeli@hack:~$ '''ls dw''' |
− | LICENSE bin cvs etc locks newfilename.txt ssldocs temp var | + | LICENSE bin cvs etc locks newfilename.txt ssldocs temp var |
− | README cgi-bin doc htdocs logs src t test | + | README cgi-bin doc htdocs logs src t test |
− | dh-rahaeli@hack:~$ | + | dh-rahaeli@hack:~$ |
− | + | =Deleting files= | |
The first thing you need to remember is that on a Unix machine, <em>there is no undo</em> for deleting files. You're probably used to "delete" on your home machine meaning "move to Trash" or "move to the Recycle Bin", so if you screw up and delete the wrong thing, you can fish it back out of the Trash. You can't do that on your Dreamhack. Once you hit 'return' on the delete command, the file is gone and you can't get it back. | The first thing you need to remember is that on a Unix machine, <em>there is no undo</em> for deleting files. You're probably used to "delete" on your home machine meaning "move to Trash" or "move to the Recycle Bin", so if you screw up and delete the wrong thing, you can fish it back out of the Trash. You can't do that on your Dreamhack. Once you hit 'return' on the delete command, the file is gone and you can't get it back. | ||
Line 224: | Line 241: | ||
The command to delete a file is 'rm', for 'remove': | The command to delete a file is 'rm', for 'remove': | ||
− | + | dh-rahaeli@hack:~$ '''ls''' | |
− | README apache bin crontab-import dw newfilename.txt | + | README apache bin crontab-import dw newfilename.txt |
− | dh-rahaeli@hack:~$ rm newfilename.txt | + | dh-rahaeli@hack:~$ '''rm newfilename.txt''' |
− | dh-rahaeli@hack:~$ ls | + | dh-rahaeli@hack:~$ '''ls''' |
− | README apache bin crontab-import dw | + | README apache bin crontab-import dw |
− | dh-rahaeli@hack:~$ | + | dh-rahaeli@hack:~$ |
If you want the system to ask you whether or not you're really sure, use the -i flag: | If you want the system to ask you whether or not you're really sure, use the -i flag: | ||
− | + | dh-rahaeli@hack:~$ '''ls''' | |
− | README apache bin crontab-import dw newfilename.txt | + | README apache bin crontab-import dw newfilename.txt |
− | dh-rahaeli@hack:~$ rm -i newfilename.txt | + | dh-rahaeli@hack:~$ '''rm -i newfilename.txt''' |
− | rm: remove regular file `newfilename.txt'? Y | + | rm: remove regular file `newfilename.txt'? '''Y''' |
− | dh-rahaeli@hack:~$ ls | + | dh-rahaeli@hack:~$ '''ls''' |
− | README apache bin crontab-import dw | + | README apache bin crontab-import dw |
− | dh-rahaeli@hack:~$ | + | dh-rahaeli@hack:~$ |
Like everything else, you can use relative directory paths to delete files, so you don't need to be in the directory that the file you want to delete is in. | Like everything else, you can use relative directory paths to delete files, so you don't need to be in the directory that the file you want to delete is in. | ||
− | + | =Making new directories= | |
To make a new directory, the command is "mkdir": | To make a new directory, the command is "mkdir": | ||
− | + | dh-rahaeli@hack:~$ '''ls''' | |
− | README apache bin crontab-import dw | + | README apache bin crontab-import dw |
− | dh-rahaeli@hack:~$ mkdir new-directory | + | dh-rahaeli@hack:~$ '''mkdir new-directory''' |
− | dh-rahaeli@hack:~$ ls | + | dh-rahaeli@hack:~$ '''ls''' |
− | README apache bin crontab-import dw new-directory | + | README apache bin crontab-import dw new-directory |
− | dh-rahaeli@hack:~$ | + | dh-rahaeli@hack:~$ |
− | + | =Deleting directories= | |
To delete a directory, the command is 'rmdir': | To delete a directory, the command is 'rmdir': | ||
− | + | dh-rahaeli@hack:~$ '''ls''' | |
− | README apache bin crontab-import dw new-directory | + | README apache bin crontab-import dw new-directory |
− | dh-rahaeli@hack:~$ rmdir new-directory | + | dh-rahaeli@hack:~$ '''rmdir new-directory''' |
− | dh-rahaeli@hack:~$ ls | + | dh-rahaeli@hack:~$ '''ls''' |
− | README apache bin crontab-import dw | + | README apache bin crontab-import dw |
− | dh-rahaeli@hack:~$ | + | dh-rahaeli@hack:~$ |
A directory has to be empty before you delete it, or else you get an error message: | A directory has to be empty before you delete it, or else you get an error message: | ||
− | + | dh-rahaeli@hack:~$ '''rmdir new-directory''' | |
− | rmdir: failed to remove `new-directory': Directory not empty | + | rmdir: failed to remove `new-directory': Directory not empty |
If you get this error message, and you're <em>absolutely positively sure</em> that you want to delete the directory, you can use the 'rm' command with the -rf (recursive, force) commands. <em>This is not a good idea</em>, especially if you're also using wildcard matching (see below). Everyone, and I do mean everyone, has a horror story about being in the wrong directory when they do an rm -rf and losing years of data. (Mine involved all my saved email from 1992-1999. No, I didn't have backups. Sigh.) | If you get this error message, and you're <em>absolutely positively sure</em> that you want to delete the directory, you can use the 'rm' command with the -rf (recursive, force) commands. <em>This is not a good idea</em>, especially if you're also using wildcard matching (see below). Everyone, and I do mean everyone, has a horror story about being in the wrong directory when they do an rm -rf and losing years of data. (Mine involved all my saved email from 1992-1999. No, I didn't have backups. Sigh.) | ||
Line 274: | Line 291: | ||
If you're <em>really really really sure</em> you want to do this, it would look like this: | If you're <em>really really really sure</em> you want to do this, it would look like this: | ||
− | + | dh-rahaeli@hack:~$ '''rm -rf new-directory''' | |
− | dh-rahaeli@hack:~$ ls | + | dh-rahaeli@hack:~$ '''ls''' |
− | README apache bin crontab-import dw | + | README apache bin crontab-import dw |
− | dh-rahaeli@hack:~$ | + | dh-rahaeli@hack:~$ |
− | + | ||
<em>Be really really really sure</em>. | <em>Be really really really sure</em>. | ||
− | + | =Skimming through files= | |
If you want to see whether a file you're looking at is really the file you think it is, you can call it up on your screen and page through it with the 'less' command: | If you want to see whether a file you're looking at is really the file you think it is, you can call it up on your screen and page through it with the 'less' command: | ||
− | < | + | <code>less filename.txt</code> |
− | To get out of the pager, hit q. | + | To get out of the pager, hit '''q'''. |
− | + | There's an older version of 'less' called 'more'. It does slightly different things, and some of us can't retrain ourselves out of using it. For the most part, the two are functionally interchangeable, though you might prefer one or the other. | |
− | = | + | =Wildcards= |
− | + | Most commands will accept wildcard matching. The wildcard operator is *. You can combine it with other bits and pieces to (for instance) only show you things that match certain patterns. For instance, if you only want to see a listing of files that end in .text: | |
− | + | <code>ls *.text</code> | |
+ | If you only want to see files that end in .bml: | ||
+ | <code>ls *.bml</code> | ||
− | ==Getting help | + | If you only want to see files that have "pic" somewhere in the name: |
+ | |||
+ | <code>ls *pic*</code> | ||
+ | |||
+ | <em>Be very, very, very careful</em> if you are combining wildcard matching with the 'rm' command. It's a very good idea to use the -i flag if you're using a wildcard match with the 'rm' command, so that it prompts you to confirm each deletion, or do a "ls" first to make the system show you what it thinks the wildcard will match. | ||
+ | |||
+ | =Searching files= | ||
+ | |||
+ | If you're looking for files that match a certain pattern, the command is "find". It's a complex command that takes a bunch of different possible arguments, but here are a few useful bits of syntax: | ||
+ | |||
+ | <source lang="bash">find . -name "*.text" | ||
+ | # finds all files ending in .text, starting in the current directory and working through all subdirs | ||
+ | |||
+ | find . -name "*.bml" | ||
+ | # finds all files ending in .bml, starting in the current directory and working through all subdirs | ||
+ | |||
+ | find . -newer filename.txt | ||
+ | # finds all files that have been modified more recently than filename.txt | ||
+ | |||
+ | find -name *.orig -o -name *.rej | xargs rm | ||
+ | # finds all .orig and .rej files, then deletes them | ||
+ | </source> | ||
+ | |||
+ | The second way to search files involves looking into the actual files. This is done with "grep" (don't ask what it stands for), or "rgrep", which works recursively (goes into each of the directories and subdirectories). It's a good idea to use the -i flag, which means "case insensitive" -- that way, it treats "friend" and "Friend" as the same thing. | ||
+ | |||
+ | The arguments it takes are: rgrep (flags) (what to search for) (where to search). In practice, you generally want to search everything, so you can use the wildcard (*). An example, where we search everything for the string "friend" to see if we've forgotten to change any of them in the code: | ||
+ | |||
+ | <code>rgrep -i friend *</code> | ||
+ | |||
+ | If you want to search for multiple words, you need to put them in quotes, like such: | ||
+ | |||
+ | <source lang=bash>rgrep -i "friend page" * | ||
+ | # will find any instance of "friend page"</source> | ||
+ | |||
+ | If you want to search for strings with quotes, or things that contain periods, <> signs, the pipe (|), parentheses, the asterisk (not used as a wildcard), the slash, -- or any special symbol that means something different to the system -- you need to escape those characters with a backslash (\): | ||
+ | |||
+ | <source lang=bash>rgrep -i "Kirk\/Spock" * | ||
+ | # will find any instances of "Kirk/Spock"</source> | ||
+ | |||
+ | =Redirecting output= | ||
+ | |||
+ | By default, the results of a command will be displayed on your screen. You might not necessarily want that, though. For instance, if you do a search, you might get thousands of matches that will then scroll rapidly by on your terminal and leave you having to scroll back up. | ||
+ | |||
+ | You can redirect any output to another program, or to a file. The syntax is slightly different for each. If you want to redirect the output of one program to another program, use the pipe (|): | ||
+ | |||
+ | <code>ls -al | more</code> | ||
+ | |||
+ | That takes the output of the first command (ls -al) and sends it through the second command (more). | ||
+ | |||
+ | To redirect output to a file, use the greater-than symbol (>): | ||
+ | |||
+ | <code>ls -al > directorylist.txt</code> | ||
+ | |||
+ | That takes the output of the first command (ls -al) and sends it to the file named directorylist.txt. (Note that it <em>will</em> overwrite the contents of directorylist.txt if it already exists, so be careful!) | ||
+ | |||
+ | You can combine > with relative directory paths, too. For instance, even if you're 10 directories deep, if you want to output the directory listing to a file in your home directory, you could do: | ||
+ | |||
+ | <code>ls -al > ~/directorylist.txt</code> | ||
+ | |||
+ | =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: | 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: | ||
− | < | + | <code>man man</code> |
− | 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. (They're mostly useful for remembering what the heck the eighty billion flags a command can take happen to be.) | + | To exit the help viewer, hit '''q'''. (The help viewer actually uses the "less" utility discussed above, so anything that works in "less" will also work here.) Not all of the man pages for the basic utilities are necessarily helpful, but in a pinch they might give you a hand. (They're mostly useful for remembering what the heck the eighty billion flags a command can take happen to be.) |
− | + | =Outside Resources= | |
Good tutorials on Unix/Linux work: | Good tutorials on Unix/Linux work: | ||
* http://www.ee.surrey.ac.uk/Teaching/Unix/ | * http://www.ee.surrey.ac.uk/Teaching/Unix/ | ||
+ | |||
+ | [[Category: Getting Started]] | ||
+ | [[Category: Development]] | ||
+ | [[Category: Dreamhack]] | ||
+ | [[Category: Linux]] |
Latest revision as of 00:38, 30 January 2024
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.
To execute a command, you type the command and hit enter/return. The command won't execute until you hit enter.
Command not working? Check case as well as spelling.
Contents
- 1 Connecting
- 2 Directories
- 3 Command prompt
- 4 Command flags
- 5 Tab complete
- 6 Don't use spaces
- 7 Listing files and directories
- 8 Moving from directory to directory
- 9 Copying files
- 10 Editing files
- 11 Moving files
- 12 Deleting files
- 13 Making new directories
- 14 Deleting directories
- 15 Skimming through files
- 16 Wildcards
- 17 Searching files
- 18 Redirecting output
- 19 Getting help
- 20 Outside Resources
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.
Details on connecting to your Dreamhack can be found in Dreamhack getting started.
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
There are generally other options to a command -- 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.)
Tab complete
If you have a file called thisisareallylongandridiculousfilenametobetyping.txt, or a directory called thisisaridiculousdirectoryname, the good news is that you don't need to type it out every time you need to refer to it. The Dreamhack shell offers tab completion. Type a few characters and hit tab, and the machine will complete the name for you.
If you get a beep and nothing happens, it means that there are multiple matches that don't have any text in common past the point you typed. For instance, if you have the files "update.bml", "update.bml.txt", and "uploadpic.bml.text", and you type "up" and hit tab, it'll beep and do nothing: it doesn't know which one you mean.
If you get a beep and part of the filename fills in, but not all, it means there are multiple matches that have some text in common past the point you typed, but not all. It will fill in as much as it can and then wait for you to either hit return or type more of the filename. For instance, if you have the files "update.bml", "update.bml.txt", and "uploadpic.bml.text", and you type "upd" and hit tab, it'll beep and fill in "update.bml", then wait to see if you mean "update.bml" or "update.bml.txt".
If you hit tab twice, it'll show you all of the possible completions. This can be useful if you forgot what the heck you called the file and don't feel like deleting the command to go look.
Tab complete only works for files and directories, not for commands.
Don't use spaces
You can't use spaces in Unix directory or filenames. If you try to, the system will think you mean two separate files or directories.
(This is actually a lie, but the process of dealing with files and directories with spaces is complicated. None of the existing DW files or directories have spaces, though, so you shouldn't need to know how to handle them, and you shouldn't make files or directories with spaces yourself.)
Listing files and directories
To list files, type:
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.)
If you would like the information in a format that's accessible without color-coding, you can use the -F flag (note that the F is upper-case):
dh-daily@newhack:~$ ls -F README apache/ bin/ crontab-import dw/
In this mode, directories will have a slash ("/") added, executable files will have an asterisk ("*") added, and plain files won't have anything added.
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:~$ 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.
Editing files
Which text editor to use is the topic of multiple religious (we're not kidding) wars in the greater Unix community.
General tips:
- If one text editor doesn't work well with how you think, try another one.
- Dreamwidth files can be VERY picky about indentation.
- Look up how to save and exit the text editor before opening a file to edit.
Moving files
The command to move or rename files is 'mv'. It works exactly like 'cp' (Including overwriting the destination file if it already exists, and the -i flag to warn you first), only instead of copying the source file to the destination file, it renames it. It might seem strange to use a command whose name is obviously derived from the word "move" for renaming, but it made sense to the founding fathers of unix at the time with the way unix file systems work, it's exactly the same operation to both move and rename a file.
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
The first thing you need to remember is that on a Unix machine, there is no undo for deleting files. You're probably used to "delete" on your home machine meaning "move to Trash" or "move to the Recycle Bin", so if you screw up and delete the wrong thing, you can fish it back out of the Trash. You can't do that on your Dreamhack. Once you hit 'return' on the delete command, the file is gone and you can't get it back.
Now that I've scared you...
The command to delete a file is 'rm', for 'remove':
dh-rahaeli@hack:~$ ls README apache bin crontab-import dw newfilename.txt dh-rahaeli@hack:~$ rm newfilename.txt dh-rahaeli@hack:~$ ls README apache bin crontab-import dw dh-rahaeli@hack:~$
If you want the system to ask you whether or not you're really sure, use the -i flag:
dh-rahaeli@hack:~$ ls README apache bin crontab-import dw newfilename.txt dh-rahaeli@hack:~$ rm -i newfilename.txt rm: remove regular file `newfilename.txt'? Y dh-rahaeli@hack:~$ ls README apache bin crontab-import dw dh-rahaeli@hack:~$
Like everything else, you can use relative directory paths to delete files, so you don't need to be in the directory that the file you want to delete is in.
Making new directories
To make a new directory, the command is "mkdir":
dh-rahaeli@hack:~$ ls README apache bin crontab-import dw dh-rahaeli@hack:~$ mkdir new-directory dh-rahaeli@hack:~$ ls README apache bin crontab-import dw new-directory dh-rahaeli@hack:~$
Deleting directories
To delete a directory, the command is 'rmdir':
dh-rahaeli@hack:~$ ls README apache bin crontab-import dw new-directory dh-rahaeli@hack:~$ rmdir new-directory dh-rahaeli@hack:~$ ls README apache bin crontab-import dw dh-rahaeli@hack:~$
A directory has to be empty before you delete it, or else you get an error message:
dh-rahaeli@hack:~$ rmdir new-directory rmdir: failed to remove `new-directory': Directory not empty
If you get this error message, and you're absolutely positively sure that you want to delete the directory, you can use the 'rm' command with the -rf (recursive, force) commands. This is not a good idea, especially if you're also using wildcard matching (see below). Everyone, and I do mean everyone, has a horror story about being in the wrong directory when they do an rm -rf and losing years of data. (Mine involved all my saved email from 1992-1999. No, I didn't have backups. Sigh.)
If you're really really really sure you want to do this, it would look like this:
dh-rahaeli@hack:~$ rm -rf new-directory dh-rahaeli@hack:~$ ls README apache bin crontab-import dw dh-rahaeli@hack:~$
Be really really really sure.
Skimming through files
If you want to see whether a file you're looking at is really the file you think it is, you can call it up on your screen and page through it with the 'less' command:
less filename.txt
To get out of the pager, hit q.
There's an older version of 'less' called 'more'. It does slightly different things, and some of us can't retrain ourselves out of using it. For the most part, the two are functionally interchangeable, though you might prefer one or the other.
Wildcards
Most commands will accept wildcard matching. The wildcard operator is *. You can combine it with other bits and pieces to (for instance) only show you things that match certain patterns. For instance, if you only want to see a listing of files that end in .text:
ls *.text
If you only want to see files that end in .bml:
ls *.bml
If you only want to see files that have "pic" somewhere in the name:
ls *pic*
Be very, very, very careful if you are combining wildcard matching with the 'rm' command. It's a very good idea to use the -i flag if you're using a wildcard match with the 'rm' command, so that it prompts you to confirm each deletion, or do a "ls" first to make the system show you what it thinks the wildcard will match.
Searching files
If you're looking for files that match a certain pattern, the command is "find". It's a complex command that takes a bunch of different possible arguments, but here are a few useful bits of syntax:
find . -name "*.text" # finds all files ending in .text, starting in the current directory and working through all subdirs find . -name "*.bml" # finds all files ending in .bml, starting in the current directory and working through all subdirs find . -newer filename.txt # finds all files that have been modified more recently than filename.txt find -name *.orig -o -name *.rej | xargs rm # finds all .orig and .rej files, then deletes them
The second way to search files involves looking into the actual files. This is done with "grep" (don't ask what it stands for), or "rgrep", which works recursively (goes into each of the directories and subdirectories). It's a good idea to use the -i flag, which means "case insensitive" -- that way, it treats "friend" and "Friend" as the same thing.
The arguments it takes are: rgrep (flags) (what to search for) (where to search). In practice, you generally want to search everything, so you can use the wildcard (*). An example, where we search everything for the string "friend" to see if we've forgotten to change any of them in the code:
rgrep -i friend *
If you want to search for multiple words, you need to put them in quotes, like such:
rgrep -i "friend page" * # will find any instance of "friend page"
If you want to search for strings with quotes, or things that contain periods, <> signs, the pipe (|), parentheses, the asterisk (not used as a wildcard), the slash, -- or any special symbol that means something different to the system -- you need to escape those characters with a backslash (\):
rgrep -i "Kirk\/Spock" * # will find any instances of "Kirk/Spock"
Redirecting output
By default, the results of a command will be displayed on your screen. You might not necessarily want that, though. For instance, if you do a search, you might get thousands of matches that will then scroll rapidly by on your terminal and leave you having to scroll back up.
You can redirect any output to another program, or to a file. The syntax is slightly different for each. If you want to redirect the output of one program to another program, use the pipe (|):
ls -al | more
That takes the output of the first command (ls -al) and sends it through the second command (more).
To redirect output to a file, use the greater-than symbol (>):
ls -al > directorylist.txt
That takes the output of the first command (ls -al) and sends it to the file named directorylist.txt. (Note that it will overwrite the contents of directorylist.txt if it already exists, so be careful!)
You can combine > with relative directory paths, too. For instance, even if you're 10 directories deep, if you want to output the directory listing to a file in your home directory, you could do:
ls -al > ~/directorylist.txt
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. (The help viewer actually uses the "less" utility discussed above, so anything that works in "less" will also work here.) Not all of the man pages for the basic utilities are necessarily helpful, but in a pinch they might give you a hand. (They're mostly useful for remembering what the heck the eighty billion flags a command can take happen to be.)
Outside Resources
Good tutorials on Unix/Linux work: