Basic Linux Commands

This article will explain some basic commands you can use on your 123 Reg Linux VPS server.

What is Unix?

UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops.

Listing files and directories

ls (list) The ls command (lowercase L and lowercase S) lists the contents of your current working directory. There may be no files visible in your home directory, in which case, the UNIX prompt will be returned. Alternatively, there may already be some files inserted by the System Administrator when your account was created. ls does not, in fact, cause all the files in your home directory to be listed, but only those ones whose name does not begin with a dot (.) Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information. ls -a lists files that are normally hidden.

Making Directories

mkdir (make directory) We will now make a subdirectory in your home directory. To make a subdirectory called unixstuff in your current working directory type

# mkdir unixstuff

To see the directory you have just created, type

# ls

Changing to a different directory

cd (change directory) The command cd directory means change the current working directory to ‘directory’. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree. To change to the directory you have just made, type

# cd unixstuff

Type ls to see the contents (which should be empty). . (The current directory) In UNIX, (.) means the current directory, so typing

# cd .

means stay where you are (the unixstuff directory). This may not seem very useful at first, but using (.) as the name of the current directory will save a lot of typing. .. (The current directory) (..) means the parent of the current directory, so typing

# cd ..

will take you one directory up the hierarchy.

Please note: Typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system.

Pathnames

pwd (print working directory) Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your home-directory, type cd to get back to your home-directory and then type

# pwd

Copying Files

cp (copy) The command cp will make a copy of a file in the current working directory, to make a copy of file1 and call it file2 type

# cp file1 file2

Moving Files

mv (move) To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two. It can also be used to rename a file, by moving the file to the same directory, but giving it a different name. To move (or renames) file1 to file2 type

# mv file1 file2

Removing files and directories

rm (remove), rmdir (remove directory) To delete (remove) a file, use the rm command. To delete file1 type

# rm file1
You can use the rmdir command to remove a directory (make sure it is empty first). You will not be able to since UNIX will not let you remove a non-empty directory.

Displaying the content of a file on the screen

clear (clear screen) If your screen is cluttered you may like to to use the clear command clear the terminal window of the previous commands so the output of the following commands can be clearly understood. At the prompt, type

# clear

This will clear all text and leave you with the % prompt at the top of the window. cat (concatenate) The command cat can be used to display the contents of a file on the screen. Type:

# cat filename.txt

As you may see, the file could be longer than than the size of the window, so it scrolls past making it unreadable.less The command less writes the contents of a file onto the screen a page at a time. Type

# less filename.txt

Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading. As you can see, less is used in preference to cat for long files. head The head command writes the first ten lines of a file to the screen. However if you type

# head -15 filename.txt

This will show the first 15 lines of the file. tail The tail command writes the last ten lines of a file to the screen. grepTo check your current quota and how much of it you have used, type

# grep word filename.txt

df The df command reports on the space left on the file system. For example, to find out how much space is left on the fileserver, type

# df .

Summary

CommandMeaning
lsList files and directories
ls -aList all files and directories
mkdirMake a directory
cd directoryChange to named directory
cdChange to root directory
cd ..Change to parent directory
pwdDisplay the path of the current directory.
cp file1 file2Copy file1 and call it file2
mv file1 file2Move or rename file1 to file2
rm fileRemove a file
rmdir directoryRemove directory
cat fileDisplay a file
less fileDisplay a file a page at a time
head fileDisplay the first few lines of a file
tail fileDisplay the last few lines of a tile
grep ‘keyword’ fileSearch a file for keywords
dfQuota and Diskspace