Wednesday, March 7, 2012

Useful Unix Commands

Advanced use of the ls command

In SSH and basic commands, we showed you how to use ls to obtain a listing of all files in the current directory. By placing various letters after ls (known as switches, options or command line arguments, depending on the UNIX guru you talk to!), you can get it to give you a lot more information about the current directory. For example:

[username@mysite]$ ls -l
will produce a long listing format that includes the permissions, owner, group, size and modified date of each file:

drwxrwxr-x    3 matt     users        4096 Jun 27 17:17 images
-rw-rw-r--    1 matt     users         228 Jun 27 19:29 index.html
-rw-rw-r--    1 matt     users         272 Jun 27 19:30 index2.html 
 
The a switch will also include hidden files (hidden files in UNIX begin with a dot (.) in the listing), as well as the current directory and parent directory entries (. and .. respectively). Also, you can combine switches by placing them one after the other, for example:

[username@mysite]$ ls -al
drwxrwxr-x    3 matt     users        4096 Jun 27 19:32 .
drwxrwxr-x    5 matt     users        4096 Jun 27 17:09 ..
-rw-rw-r--    1 matt     users          23 Jun 27 19:31 .hidden_file
drwxrwxr-x    3 matt     users        4096 Jun 27 17:17 images
-rw-rw-r--    1 matt     users         228 Jun 27 19:29 index.html
-rw-rw-r--    1 matt     users         272 Jun 27 19:30 index2.html 
 

Creating folders with mkdir

mkdir (short for "make directory") lets you create new directories (folders) on your Web server, much the same as the "New Folder" options on Windows PCs and Macs.
To create a directory in the current directory, type mkdir followed by the directory name. For example, to create a new directory in your Web site called coolstuff you might type something like:

[username@mysite]$ cd mysite.com [username@mysite]$ cd htdocs [username@mysite]$ mkdir coolstuff
A quick listing of your site directory would now show something like:

[username@mysite]$ ls coolstuff images images index.html

Copying files and folders with cp

The cp (short for "copy") command allows you to copy files to new files, or copy files and directories to new directories. For example, to copy index.html to index2.html you'd use:

[username@mysite]$ cp index.html index2.html
To copy index.html into an existing directory called coolstuff, use:

[username@mysite]$ cp index.html coolstuff
To copy a whole directory, including its contents, to a new directory, use cp -r (the -r means "recursive"):
[username@mysite]$ ls coolstuff images index.html [username@mysite]$ cp -r coolstuff coolstuff2 [username@mysite]$ ls coolstuff coolstuff2 images index.html
 
To copy a whole directory, including its contents, into an existing directory:

[username@mysite]$ cp -r coolstuff2 coolstuff [username@mysite]$ cd coolstuff [username@mysite]$ ls index.html coolstuff2

Deleting stuff with rm

rm is the UNIX command to delete files and, sometimes, directories. It's short for "remove". Be very careful when deleting stuff with this command, as UNIX usually has no recycle bin or trash can - once you've deleted something, it's gone forever! :(

To delete a single file, use rm filename. For example, to delete index.html you'd do:
[username@mysite]$ rm index.html
To delete a directory and all its contents, use rm -r directory. For example:
[username@mysite]$ rm -r coolstuff
Note that if the directory is empty, you can also delete it using the command rmdir, as follows:
[username@mysite]$ rmdir coolstuff

Playing it safe

If you're deleting stuff with rm, particularly if you're using rm -r, it's a good idea to add the -i switch too, e.g.:
[username@mysite]$ rm -ir coolstuff
This will make sure the system prompts you before deleting each file or directory.

UNIX's online manual

Most UNIX servers come with a great online help system called man. You can use this to get help on most of the available commands by typing man followed by the command. For example, try typing:
[username@mysite]$ man ls
While reading a manual page on Linux, you can page up and down with the Page Up and Page Down keys, and scroll up and down with the Up Arrow and Down Arrow keys. To quit the manual viewer, press the q key. To search for some text, press the forward-slash (/) key and type the text you want to search for, e.g. /file, and press Return.

On non-Linux systems, you usually have to press Enter to go down a line, and the Space bar to go down a page, and you can't scroll up. :(

Running scripts and programs

Often you'll want to be able to run programs such as Perl scripts and executables on your Web server, in much the same way as you run a program from the Start menu in Windows.

In UNIX, running programs is easy - you usually just type the name of the program! In fact, all the commands we've shown you already are programs.

If you want to run a program that's in your current directory, you'll usually need to put a ./ in front of the program name, to tell UNIX that it should look in the current directory for the program, e.g.:
[username@mysite]$ ./myprog
If you're having trouble with a Perl CGI script, you can often find out the exact error message by running it from the UNIX prompt in SSH, rather than through a Web browser. Say you wanted to test a script called formmail.cgi. Run it at the prompt with the word perl before it, like this:

[username@mysite]$ cd cgi-bin [username@mysite]$ perl formmail.cgi
The CGI script will then run as if it were called from a Web browser, but you'll be able to see the exact output from the script appear in the SSH window (as opposed to the browser, where you'll probably just see something unhelpful, such as Internal Server Error!).
 

No comments:

Post a Comment