Getting started with Linux and the Command Line

Linux is great.  If you are in to developing websites, chances are, you’ll be working with Linux.  Problem is, a lot of people are afraid of the command line, or just don’t know enough commands to be comfortable working on a server via SSH.

Here are a few commands you’ll need to know to get started:

1. cd

cd stands for ‘change directory’ and is one you’ll probably use most frequently.  The syntax is very simple:

cd destination

A few tricks to know when working with this command:

cd ..

Changes to the directory UP one hierarchical level.

cd ../..

Changes to the director UP two hierarchical levels.. etc.

cd ../myFolder

Changes to a folder called ‘myFolder’ which is a subfolder of the current parent folder.  This is a relative path.

cd /

Changes to the root folder of the filesystem.

cd /var/www

Change to an absolute path.

cd ~

Change to your shell’s home directory.  By default, this is your system home directory, such as /home/yourUsername

cd ~/Desktop

Change to the Desktop folder under your home directory, equivalent to something such as cd /home/yourUsername/Desktop

2. cp

cp is the copy command, and is another frequently used command.  This is another command with very simple syntax.  Learning the syntax of cd is useful for using cp. The syntax is:

cp file-to-be-copied.php /destination/newfilename.php

Here are a few examples and tips for working with cp:

cp index.php home.php

Creates a copy of the index.php file, named home.php

cp index.php /someDirectory

Creates an index.php under the folder /someDirectory.  Not supplying the new name gives the copy the same name, index.php

cp someDirectory/ /destination/

Creates a folder ‘someDirectory’ under the ‘/destination’ folder.  This doesn’t copy any of the files, just the folder itself.

cp someDirectory/ newDirectoryName/

Creates a folder in the same directory with the name ‘newDirectoryName’ .. again, no files are copied just the folder (and its ownership/permissions). Apply a relative or absolute path before the directory name to create it somewhere else.

cp -r someDirectory/ newDirectoryName/

Creates a copy of the directory ‘someDirectory’ named ‘newDirectoryName’.  using the ‘-r’ flag will copy the folders contents “recursively.”

3. ls

ls is the command used to list all of the files in your present working directory.  It is as simple as typing: ls.  You can add flags for more views:

ls -a

will show you ALL files .. includes the hidden files (hidden files start with period, such as “.htaccess”)

ls -l

will show you the files in a list format. This method shows you more information about the file, such as the owner, the size, and the last modification date.

Combine the two flags, to get ls -al and you will see all files in a list format

4. pwd

pwd echoes out your “Present Working Directory” to the screen.  Cool.

5. rm

rm is the always useful remove command.  Type

rm filename.php

to remove the file ‘filename.php’. There are a lot of flags to be used with rm, but they can be dangerous. We’ll wait until you are more comfortable in the command line.

6. rmdir

rmdir is rm’s brother. rmdir clearly stands for “Remove Directory”.  You can only remove a directory that is empty.  If its not empty, you’ll first need to delete all of the files in the directory.  There are easier ways to remove things in bulk, or to remove non-empty directories, but as I said, they can be dangerous.  We’ll wait until you are more comfortable in the command line. :)

7. mv

mv stands for Move.  It is the command you’ll use to relocate files.  You can consider moving a file to be the same thing as renaming it.  Renaming a file from “fileA.php” to “fileB.php” is the same as moving its location to be “/directory/fileA.php to /directory/fileB.php.

Use mv as:

mv fileA.php fileB.php

to rename fileA to fileB.  Or you can use mv like:

mv fileA.php /some/other/dir/

to relocate fileA.php to /some/other/dir/fileA.php

This is all I have for now.  There will hopefully be more later.  Enjoy, n00bs :)

Leave a Reply