2008 March 3

Basic BASH commands - Or how I stopped worrying and learned to love the command line

By Andy

Now that we’ve enabled SSH access to our Business Webhosting package I thought it’d be nice if we tried to introduce new people to the Linux command line, so if they wanted to use this feature they wouldn’t be totally lost.

There are a few basic commands and tips listed here (by absolutely no means exhaustive), most of these I would’ve really appreciated as I struggled to learn this a couple of years ago! (btw don’t type the dollar sign, that’s just the prompt you’ll see when connected!)

Seeing Stuff

When you first login you will be sitting in your home directory. You can get a listing of the directory contents by typing:

$ ls

or for a long view of the listing with time stamps, file permissions and file ownerships type:

$ ls -l

You can move from directory to directory by typing;

$ cd directory-name

Just cd by itself will return you back to your home directory.

Directories are referenced with a slash ( / ). / being the root directory (like the c drive for all you windowers). So for example if I wanted to go to the etc directory (/etc) I would type;

$ cd /etc

Here’s a top tip - if you hit the TAB key at any point after typing a few characters bash will try and fill in the rest of the file, or directory name that matches what you’ve entered. If you tap the TAB key twice it will start to list all the options.

Actually Doing Things

There are a massive amount of different commands and variations but these 3 you simply can’t do without. cp, rm, and mv (Copy, remove and move).

$ cp file1 file2

e.g. $ cp /home/bill/new_httpd.conf /etc/httpd.conf

This will copy the file new_httpd.conf in bills home directory to the etc directory.

$ rm file

e.g. $ rm /home/bill/new_httpd.conf

This will delete the file new_http.conf that’s in bills home directory.

$ mv file1 file2

e.g. $ mv /home/bill/new_httpd.conf /etc/httpd.conf

This will move the file new_httpd.conf to the /etc directory.

Making Things Work

Another useful command for you will be to change the permissions on files within your home directory, you can do this by using chmod;

$ chmod 644 /home/bill/bigtrain/templates

The 3 numbers after the command are the access rights, these will change the access to ‘templates’ to read/write by the owner and read only by users in the specified group and all users (reading left to right). I can’t really go into any detail on the file permissions for lack of space but here’s an article from Linuxforums.org with a good explanation clicky . Be careful of changing these if you are not sure of what you are doing! Although it’s easy to change back if you mess something up ;) You’ll likely need to change these if you choose to install any scripts manually rather than using fantastico.

You can also edit files from the command line using an editor, the most powerful is called vi, but I really wouldn’t recommend this to newbies (myself included). I prefer to use nano as this is much easier to learn. To access and edit a file you simply need to type the editors name and then the file you wish to edit.

e.g. $ nano /home/bill/new_httpd.conf

This will open httpd.conf for editing, after which you can then save over the original.

If you try and open a file and it’s blank check your spelling! Editors will create that file if the instructions you specify are wrong.

e.g. $ nano /home/bill/new_httpds.conf

This would actually open a new blank file called new_httpds.conf if it didn’t exist rather than opening new_httpd.conf for editing.

And finally a couple of last tips for now, if you tap the up arrow key on your keyboard you can go through the list of commands you’ve already used (no need to type everything out again), and by typing ‘man’ before a command you can bring up the existing documentation on commands that is built into the operating system (a word of warning though if you’re new to Linux, this can confuse the hell out of you at the start!).

$ man term

e.g. $ man chmod

Thanks,
Andy

Leave a Reply

Please note that Spam, support requests and irrelevant comments will be deleted...