• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Running a Website / UNIX and Linux / Useful Unix Commands

Useful Unix Commands

29 June 2001 / 2 Comments

In this article we’re going to look at some useful things you can do with your Web server using UNIX commands and SSH. If you’ve never used SSH or UNIX commands, try reading my SSH and basic commands tutorial first, then come back here and carry on reading!

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!). You can find out more about troubleshooting CGI scripts in my handy tutorial.

Armed with these commands and techniques, you should be able to manage your websites and web server effectively. Good luck!

Filed Under: UNIX and Linux Tagged With: cp, execute programs, ls, man, mkdir, rm

Reader Interactions

Comments

  1. sankar27 says

    23 January 2011 at 1:31 pm

    Thank you,

    Could please send UNIX cmds in SAP environment

    Reply
  2. matt says

    23 January 2011 at 8:50 pm

    @sankar27: I don’t know anything about SAP I’m afraid!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits