Introduction To Linux Commands

Advertisement

At the heart of every modern Mac and Linux computer is the “terminal.” The terminal evolved from the text-based computer terminals1 of the 1960s and ’70s, which themselves replaced punch cards as the main way to interact with a computer. It’s also known as the command shell, or simply “shell.” Windows has one, too, but it’s called the “command prompt” and is descended from the MS-DOS of the 1980s.

Mac, Linux and Windows computers today are mainly controlled through user-friendly feature-rich graphical user interfaces (GUIs), with menus, scroll bars and drag-and-drop interfaces. But all of the basic stuff can still be accomplished by typing text commands into the terminal or command prompt.

Using Finder or Explorer to open a folder is akin to the cd command (for “change directory”). Viewing the contents of a folder is like ls (short for “list,” or dir in Microsoft’s command prompt). And there are hundreds more for moving files, editing files, launching applications, manipulating images, backing up and restoring stuff, and much more.

So, why would anyone want to bother with these text commands when you can use the mouse instead? The main reason is that they are very useful for controlling remote computers on which a GUI is not available, particularly Web servers, and especially Linux Web servers that have been stripped of all unnecessary graphical software.

Sometimes these lean Linux servers are managed through a Web browser interface, such as cPanel or Plesk, letting you create databases, email addresses and websites; but sometimes that is not enough. This article provides a broad introduction to text commands and the situations in which they are useful. We’ll cover the following:

  • Why knowing a few commands is useful;
  • Issuing commands on your own computer;
  • Using SSH to log into your Web server;
  • Getting your bearings: pwd, cd ls;
  • Viewing and moving files: cat, more, head, tail, mv, cp, rm;
  • Searching for files: find;
  • Looking through and editing files: grep, vi;
  • Backing up and restoring files and databases: tar, zip, unzip, mysqldump, mysql;
  • File permissions: chmod.

Why Knowing A Few Linux Commands Is Useful

As a website developer or server administrator, you would gain a big asset in becoming comfortable with these commands: for website emergencies, to configure a server and for your CV. It can also save you money. Many hosting companies offer fully managed servers but at a high monthly premium. Or else they charge by the hour for technical support.

Perhaps you need to archive some big files or make a change to the httpd.conf file or figure out why your website’s images have suddenly stopped loading. You might not want to pay $50 to your server’s administrator for a five-minute job. This article gives you the tools to make such changes yourself.

And why “Linux” commands? Two main types of servers are available today: Windows and UNIX. UNIX-based servers include Linux (which split off in 1991), Mac OS X (2002) and several more traditional UNIX systems, such as BSD, Solaris and HP-UX. Linux commands are basically UNIX commands and so will run on all of them. In fact, I use the term “Linux” here only because it is more common and less frightening than “UNIX.” Windows servers, on the other hand, have a much smaller market share and are more often controlled through GUIs, such as Remote Desktop2 and VNC3, rather than the command line.

In fact, a November 2011 survey showed that Apache accounted for about 65% of all Web servers4. Apache usually runs in the popular LAMP configuration: Linux, Apache, MySQL and PHP. Microsoft was a distant second, with 15%. Third place nginx runs on Linux, UNIX, Mac and Windows. So, the commands in this article will work on at least two thirds of all servers.

Issuing Commands To Your Own Computer

You can quickly experiment with text commands on your own computer. On Mac with OS X, go to Applications → Utilities, and run Terminal. On a PC with Windows, go to Start → All Programs → Accessories, and choose “Command Prompt.” On Ubuntu Linux, go to Applications → Accessories, and choose Terminal.

On Windows you should see this:


The Windows command prompt

This is the command line (i.e. shell, prompt or terminal) on your own computer. You can type dir on Windows or ls on Linux or Mac followed by “Enter” to see a list of the files in the current “directory” (i.e. folder, location or path).

All we will be doing for the rest of this article is opening up one of these terminals on a remote computer: your Web server.

You may have used VNC or Remote Desktop, which allow you to actually view the desktop on someone else’s computer: your screen shows their screen, your mouse controls their mouse, your keyboard mimics their keyboard.

The terminal is similar to this but without the fancy menus or scroll bars. If you were to plug a keyboard and screen into your Web server, sitting in a fireproof basement somewhere, you would probably see one of these terminals, waiting patiently for your user name and password.

Using SSH To Log Into Your Web Server

The application SSH, or Secure Shell, is used to log into Web servers. It often takes the same user name and password as FTP, but it has to be allowed by your host. If you have a dedicated Web server, it is probably already allowed. If you use cloud hosting, then you might need to request it first. If you are on shared hosting, you’ll definitely need to request it, and the administrator may refuse.

On Linux or Mac, open up Terminal as described above and type the following:

ssh -l username www.myserver.com

The -l stands for “log in as,” and your user name goes after it. If SSH is allowed, then it will ask for a password. If not, you’ll get an error message, like this one:

SSH Command and Connection Error
Running the ssh command and being denied access

On Windows, you will need to download some SSH software. Putty5 is a popular and easy choice. It downloads as a single EXE file, which you can save to your desktop and run right away. Type your website as the host name, check the SSH box under “Connection Type,” and click “Open.” It will ask for your user name and then your password.

Running Putty on Windows
Running Putty on Windows in order to SSH to your Web server

Once successfully logged in, you will usually see a welcome message. After that, you will be presented with a few letters and a $ sign (or a # sign if you have logged in as root). The letters often represent your user name and where you’ve come from, or the name of the server. A ~ indicates that you are in your home directory. The $ is the prompt; it indicates that you can start typing commands now, something like:

Successful SSH to a server
A successful SSH log-in to a Web server. The $ means we can start typing commands.

The next section introduces a few basic commands.

Getting Your Bearings

On Windows, when you go to “My Documents” from the Start menu, it opens your “My Documents” directory in Windows Explorer and shows the contents. If some nosy colleague walked by and asked “What directory are you in?” you could say “I’m in my documents.”

If you SSH’ed to a server as the user “admin,” you would land in admin’s home directory, probably /home/admin. You can verify this by typing the command pwd, which shows your current location (i.e. folder, directory or path).

The pwd and ls commands
The pwd command tells you where you are, cd changes the directory and ls shows the contents of a directory.

To change to another directory, use the cd command with the destination, like so:

cd /

This will change the directory to /, the top of the whole UNIX directory structure. The command ls lists the contents of the current directory, in this case /.

In the screenshot above, the terminal is color-coded. Dark-blue entries are subdirectories, and black entries are files. A lot of the interesting stuff on Web servers happens in the /etc, /home and /var directories. Using just cd and ls, you can explore your server and find out where stuff is.

When using cd, you can specify the new directory absolutely (beginning with a slash, like /var/www) or relative to your current location (without the initial slash). You can also go up a directory with two dots. Practice with the sequence below, pressing “Enter” after each command. Can you guess what the last command will tell you?

cd /var
ls
cd www
ls
cd ..
pwd

Viewing And Moving Files

On many Linux servers, websites are located in /var/www/vhosts. You can check on your server by doing the following:

cd /var/www/vhosts
ls

If you see a list of websites, you can move into one of them. Within the website’s main directory, you will probably see the same files that you see when you FTP to the website, things such as httpdocs (where your website’s files are), httpsdocs (if you have a separate secure website), conf (configuration files), statistics (logs and compiled statistics), error_docs, private and more.

You can then change into your website’s public-facing directory, which is myserver.com/httpdocs in this example:

cd myserver.com
ls
cd httpdocs
ls

Now you have arrived, and you can run a new command, cat, which displays the contents of a file. For instance, if you have an index.html file, run:

cat index.html

If your index.html file is more than a few lines long, it will rush past in a blur. You can use the more command to show it slowly, one page at time. After you type the command below, it will show you the first page. You can press the space bar to show the next page, “Enter” to show the next line, and Q to quit.

more index.html

You can also show just the first few or last few lines of a file with the head and tail commands. It shows 10 lines by default, but you can pass in any number:

head index.html
tail -20 index.html

If you would like to rename this file, use the mv command, short for “move”:

mv index.html indexold.html

Similarly, the cp is the copy command, and rm removes files.

cp index.html indexold.html
rm indexold.html

Below is a string of commands in action. In order, it confirms the current directory with pwd, looks at the contents with ls, views index.html with cat, then renames it with mv, and finally removes it with rm, with a lot of ls in between to show the changes.

The cat and mv commands
The cat, mv and rm commands in action, for displaying, moving and then removing a file.

More Advanced Tip: Changing the Prompt

Note that in our initial examples, the full prompt included the current directory. For instance, in [admin@myserver /]$, the / indicated that the user was in the / directory. In the example directly above, it was removed, or else it would have crowded the screenshot by constantly saying [admin@myserver /var/www/vhosts/myserver.com/httpdocs]$.

You can change the prompt to whatever you want by setting the PS1 environment variable. Here are a couple of examples, the latter including the user, host and current directory:

PS1="[woohoooo ]$ "
PS1='[${USER}@${HOSTNAME} ${PWD}]$ '

Searching For Files

On big websites, files can get lost. Perhaps you vaguely remember uploading a new version of your client’s logo about four months ago, but it has since fallen out of favor and been replaced. Now, out of the blue, the client wants it back. You could download everything from the server using FTP and search the files using Finder or Explorer. Or you could log in and search using the command line.

The find command can search through files by name, size and modified time. If you just give it a directory, it will list everything that the directory contains. Try this:

find /var/www

You will probably see lots and lots of file names whizzing past. If you have many websites, it could continue for a couple of minutes. You can stop it by hitting Control + C (i.e. holding down the Control key on your keyboard and pressing the letter C). That’s the way to interrupt a Linux command. A more useful command would be:

find /var/www | more

The pipe symbol (|) takes the output of one command (in this case, the long list of files produced by find) and passes it to another command (in this case, more, which shows you one page of files at a time). As above, press the space bar to show the next page, and Q to quit.

To search for a specific file name, add -name and the file name. You can use * as a wild card (the backslash is not always necessary but is good practice with the find command). You can combine searches using -o (for “or”). If you leave out the -o, it becomes an “and.”

find /var/www -name logo.gif
find /var/www -name *.gif
find /var/www -name *.gif -o -name *.jpg

You can also search by size by adding -size. So, you could look for all GIFs between 5 and 10 KB:

find /var/www -name *.gif -size +5k -size -10k

Similarly, to find a file that was last changed between 90 and 180 days ago, you can use -ctime:

find /var/www -name *.gif -ctime +90 -ctime -180

In both of these cases, you will probably also want to know the actual file size and date last changed. For this, you can add -printf, which is similar to the C function printf in that you use the % sign to output various information. This command outputs the file size (up to 15 characters wide), the date and time changed (down to the nanosecond) and the file name:

find /var/www -name *.gif -size +5k -size -10k -ctime +90 -ctime -180 -printf "%10s  %c  %pn"

With that whopper, you have hopefully found the missing file. Here is an example:

Variations on the find command
Searching for all GIFs within a single website, and displaying the file sizes, changed times and file names.

Another useful parameter is -cmin, which lets you see files that have changed in the last few minutes. So, if something goes wrong on a website, you can run this to see everything that has changed in the last 10 minutes:

find /var/www -cmin -10 -printf "%c %pn"

This will show files and directories that have changed. Thus, it won’t show files that have been removed (because they are no longer there), but it will show the directories that they were removed from. To show only files, add -type f:

find /var/www -cmin -10 -type f -printf "%c %pn"

More Advanced Tip: Reading the Manual

I didn’t have to remember all of the variations above. I consulted the manual several times, like so:

man find

While reading a manual page, the controls are the same as more: space bar for paging, “Enter” to go forward one line and Q to quit. The up and down arrows also work. You can search within a page of the manual by typing / and a keyword, such as /printf. This will jump you to the next occurrence of that term. You can search backwards with ?printf, and you can repeat the search by pressing N.

Looking Through And Editing Files

Most visual code editors allow you to search through many files when you’re looking for a particular variable or bit of HTML. You can also do this directly on the server using the command grep. This is useful when something goes wrong on a complex website with hundreds of files and you have to find the error and fix it fast.

Let’s say you view the HTML source and see that the error happens right after <div id="left">. You can let grep do the searching for you. Give it the thing to be searched for and the files to search in. These commands change to the website directory and grep through all files ending in php. You need to put quotes around the HTML because it contains spaces, and the inner quotes have to be escaped with backslashes:

cd /var/www/vhosts/myserver.com/httpdocs/
grep "<div id="left">" *.php

This will tell you which files in the current directory contain that bit of HTML. If you want to search in subdirectories, you can use the -r option with a directory at the end, instead of a list of files. The single dot tells it to start in the current directory.

grep -r "<div id="left">" .

Alternatively, you could use the find command from above to tell it which files to look in. To put a command within a command, enclose it in back apostrophes. The following searches only for the HTML in PHP files modified in the last 14 days:

grep "<div id="left">" `find . -name *.php -ctime -14`

You can also add -n to show the line numbers, as in this example:

Using grep to look for things inside files
Searching for a bit of HTML within the PHP files in the current directory

And how do you quickly fix an error when you find it? To do that, you will need to start up a Linux text editor. Different editors are available, such as pico and emacs, but the one that is guaranteed to be there is vi. To edit a file, type vi and the file name:

vi index.php

vi is a complex editor. It can do most of the amazing things that a fully featured visual editor can do, but without the mouse. In brief, you can use the arrow keys to get around the file (or H, J, K and L on very basic terminals where even the arrow keys don’t work). To delete a character, press X. To delete a whole line, press DD. To insert a new character, press I. This takes you into “insert mode,” and you can start typing. Press the Escape key when finished to go back to “command mode.” Within command mode, type :w to save (i.e. write) the file and :q to quit, or :wq to do both at the same time.

The vi editor also supports copying and pasting, undoing and redoing, searching and replacing, opening multiple files and copying between them, etc. To find out how, look for a good vi tutorial (such as “Mastering the VI Editor96”). Note also that on many computers, vi is just a shortcut to vim, which stands for “vi improved,” so you can follow vim tutorials, too.

The Linux editor vi
Editing files with the vi text editor

More Advanced Tip: Tab Completion

When changing directories and editing files, you might get tired of having to type the file names in full over and over again. The Terminal loses some of its shine this way. This can be avoided with command-line completion, performed using tabs.

It works like this: start typing the name of a file or a command, and then press Tab. If there is only one possibility, Linux will fill in as much as it can. If nothing happens, it means there is more than one possibility. Press Tab again to show all of the possibilities.

For example, if above I had typed…

vi i

… And then pressed Tab, it would have filled in the rest for me…

vi index.php

… Unless several files started with I. In that case, I would have had to press Tab again to see the options.

Backing Up And Restoring Files And Databases

Some Linux servers do support the zip command, but all of them support tar, whose original purpose was to archive data to magnetic tapes. To back up a directory, specify the backup file name and the directory to back up, such as:

cd /var/www/vhosts/myserver.com/httpdocs/
tar czf /tmp/backup.tgz .

The czf means “create zipped file.” The single dot stands for the current directory. You can also back up individual files. To back up just things changed in the last day, add the find command:

tar cfz /tmp/backup.tgz `find . -type f -ctime -1`

Both of these commands put the actual backup file in the temporary /tmp directory — if the backup file is in the same directory that you are backing up, it will cause an error. You can move the file to where you need it afterwards. To see what is in an archive, use the tzf options instead:

tar tfz /tmp/backup.tgz

Linux tar command
Creating and showing the contents of a backup file using tar

To restore things, use xzf, for “extract from zipped file.” First, run a listing as above to check what’s in there, and then restore one or more of the files. The second command restores all of the files from the archive into the current directory:

tar xfz /tmp/backup.tgz ./index.php ./test.php
tar xfz /tmp/backup.tgz

If your server has the zip command, then run these commands to do the same thing:

cd /var/www/vhosts/myserver.com/httpdocs/
zip -r /tmp/backup.zip .
zip -r /tmp/backup.zip `find . -type f -ctime -1`
unzip -l /tmp/backup.zip
unzip /tmp/backup.zip test.php
unzip /tmp/backup.zip

If your Web server uses MySQL, then you might want to regularly back up your data. For this, there is the mysqldump command. The format of the command is:

mysqldump --user="username" --password="password" --add-drop-table database

Replace the user name, password and database with your values. Instead of specifying a database, you can use -A to dump all databases. If you get errors about table locking, you can add --single-transaction. Once you submit the user name and password, this will output a load of SQL in a long blur. To save the output to a file, you will need to use the > symbol. This sends the output of a command to a file.

mysqldump --user="username" --password="password" --add-drop-table database > /tmp/db.sql

To restore a database backup, you can use the mysql command. This command lets you run SQL statements from the command line. For example, the following command gets you into the database:

mysql --user="username" --password="password" dbname

At the mysql> prompt, you can type an SQL statement such as:

mysql> SHOW TABLES;
mysql> SELECT * FROM customers;

For restoring, you’ll need to use the pipe (|), which will send the output from one command into another. In this case, cat will output the database backup file and send it into the mysql command:

cat /tmp/db.sql | mysql --user="username" --password="password" dbname

If people are looking over your shoulder while you’re doing this, you might not want to type the password directly into the command. In this case, just leave it out, and mysql or mysqldump will ask for it instead.

cat /tmp/db.sql | mysql --user="username" --password dbname

Once you’ve created the database backup file, you can include it in the backups we did above:

tar czf /tmp/backup.tgz . /tmp/db.sql

More Advanced Tip: Hidden Files and Wildcards

Many websites use a file called .htaccess to implement URL rewriting and password protection. In UNIX, all files starting with a single dot are hidden. They won’t show up when you do ls, and they won’t get backed up if you do this:

tar czf /tmp/backup.tgz *

The * is a wildcard. Before the command executes, the * is replaced with all non-hidden files in the current directory. To include hidden files as well, it’s better to back up the whole directory as above using a single dot:

tar czf /tmp/backup.tgz .

To show hidden files when doing a directory listing, add -a to the command:

ls -a
ls -la

File Permissions

If you use FTP regularly to upload files to websites, then you might be familiar with permissions. All files and directories on Linux (and Mac, Windows and other UNIX systems) have an owner, a group and a set of flags specifying who can read, write and execute them.

The list of user names (and, thus, potential file owners) on a UNIX system is stored in the file /etc/passwd. You can try:

more /etc/passwd

The Apache Web server is started by a command when the Web server boots up. But the user who starts Apache is often a restricted and unprivileged user, such as nobody or apache or www-data. This is for security reasons, to prevent someone from hacking into the website and then gaining control of the whole server. You can find out who that user is by running the command below and looking in the first column. The ps aux command shows all of the processes running on the server, and grep shows only processes that contain the word “apache.”

ps aux | grep apache

This can cause conflicts, though. If you upload a file to a website via FTP and log in as admin, then the file will be owned by admin. If Apache was started by the user named nobody, then Apache might not be able to read that file and won’t be able to send it to any users who request it when viewing the website. Instead, users will see a broken image or a message such as “403 Forbidden. You don’t have permission to access that file.”

A subtler and more common problem is when an image can be viewed but not overwritten or removed via the website’s content management system (CMS). In this case, the user nobody can read the file but can’t write to it.

You can view permissions using the ls command with an extra -l, like so:

ls -l

ls command with long list format
The command ls -l shows information about permissions, owners, size and date.

This directory contains three files, with three subdirectories shown in green. The first letter on each line indicates the type: d for directory and - for normal file. The next nine letters are the permissions; they indicate the read, write and execute permissions for the owner, group and everyone else. After the number (which represents the size) is the owner and group for the file. These files are all owned by admin. This is followed by the file size (less useful for directories) and the date and time of the last modification.

Below is another example of three files in an images subdirectory. Two of the files were uploaded by admin via FTP, and Apache was started by the user www-data. One of the files will be unviewable through a Web browser. Which do you think it is?

Bad permissions

The answer is bg.jpg. Both bg.jpg and logo2.gif have the same permissions: only the owner can read and write them. The logo2.gif file is OK because the owner is www-data, so that file can be accessed, read and returned by Apache. The logo.gif file is also OK because it has r in all three permissions (i.e. owner, group and everyone else). But bg.jpg will fail because only the user admin can read it, not the user who started Apache. If you were to access that file in a Web browser, you would see something like this:


What happens when you try to access a file without the correct permissions in a browser.

These sorts of errors can be resolved with the chmod command, which changes file permissions. The three sets of permissions are represented in commands with u (“user” or owner), g (“group”), o (“other” or everyone else) or a (“all”). So, to enable all users to read bg.jpg, either of these would work:

chmod go+r images/bg.jpg
chmod a+r images/bg.jpg

If this file were also part of a CMS, then you’d have to also add write permissions before the CMS could overwrite it:

chmod a+rw images/bg.jpg

You can also make these changes to all files in all of the subdirectories by adding -R. This recursive operation is not supported by some FTP programs and so is a useful command-line tool:

chmod -R a+rw images/

Directories also need the x (“execute” permission), but files generally don’t (unless they are in a cgi-bin). So, you can give everything rwx (read, write and execute) permissions and then take away the x from the files:

chmod -R a+rwx images/
chmod -R a-x `find images/ -type f`

However, this does leave everything rather open, making it easier for hackers to gain a foothold. Ideally, your set of permissions should be as restrictive as possible. Files should be writable by the Apache user only when needed by the CMS.

More Advanced Tip: Chown and the Superuser

Another useful permissions command is chown. It changes the owner of a file. However, you have to be logged in as a user with sufficient privileges (such as root) in order to run it. To make www-data the owner of bg.jpg, run this:

chown www-data images/bg.jpg

This will probably return “Permission denied.” You have to run the command as the superuser. For this, you will need to find the root password for your server, and then run the following:

sudo chown www-data images/bg.jpg

You will definitely need to be the superuser if you want to edit configuration files, such as Apache’s:

sudo vi /etc/httpd/conf/httpd.conf

If you want to become the superuser for every command, run this:

su

This is dangerous, though, because you could easily accidentally remove things — especially if you are using the rm command, and particularly if you’re using it in recursive mode (rm -r), and most especially if you also force the changes and ignore any warnings (rm -r -f).

Conclusion

This article has introduced some very useful Linux commands, a potential asset for any aspiring Web worker and a surefire way to impress a dinner date.

For a few more commands related specifically to website crashes, check out the Smashing Magazine article “What to Do When Your Website Goes Down7.” For a broader view, try this list of Linux commands8. And the “Mastering the VI Editor96” tutorial mentioned above explains vi well.

Hopefully, you now have the tools and confidence to pitch in the next time one of your websites has a problem.

(al)

Footnotes

  1. 1 http://en.wikipedia.org/wiki/VT52
  2. 2 http://en.wikipedia.org/wiki/Remote_Desktop_Protocol
  3. 3 http://en.wikipedia.org/wiki/Virtual_Network_Computing
  4. 4 http://news.netcraft.com/archives/2011/11/07/november-2011-web-server-survey.html
  5. 5 http://www.putty.org/
  6. 6 http://www.eng.hawaii.edu/Tutor/vi.html
  7. 7 http://coding.smashingmagazine.com/2010/12/13/what-to-do-when-your-website-goes-down/
  8. 8 http://ss64.com/bash/
  9. 9 http://www.eng.hawaii.edu/Tutor/vi.html

↑ Back to topShare on Twitter

Paul Tero is an experienced PHP programmer and server administrator. He developed the Stockashop ecommerce system in 2005 for Sensable Media. He now works part-time maintaining and developing Stockashop, and the rest of the time freelancing from a corner of his living room, and sleeping, eating, having fun, etc. He has also written numerous other open sourcish scripts and programs.

Advertising

Note: Our rating-system has caused errors, so it's disabled at the moment. It will be back the moment the problem has been resolved. We're very sorry. Happy Holidays!

  1. 1

    definitely missing in this list (and a must-have for every linux server) & most popular linux application:
    Midnight Commander => “mc”

    • 2

      MC is indeed great. Yet it is better to learn the shell/bash at first, to know what exactly are you doing and how exactly this all works. There are types of environments where you’ll be left alone without MC ;-)

    • 3

      Cisco, Unix, linux and even old oracle guys lover your “command line”?

      Why do you have so much jealousy of the mouse? The people have spoke and they want GUI, stop it.

  2. 4

    Great article, Paul! Congrats ! It is great for a beginner, and it shows the basic&most useful commands. I noticed a mistake in the article, and I think you should correct it before it leads to confusion:
    : Getting your bearings: pwd, cs ls; – I guess you wanted to write CD not CS.

    • 5

      Thanks for pointing that out – I will get that mistake, and the one with the su command, corrected soon.

  3. 6

    I am a graphical designer/illustrator/GUI designer. Needless to say, front-end development requires my kind to learn HTML/CSS/jQuery and so on. Sometimes there’s also the need to dabble with PHP (or any other server-side scripting language if you work with a CMS). Yet, doing PHP for someone ‘artistic’ is as challenging as mountain-climbing is to a fish, yet at one point in my career it becomes necessary. The same applies to working in the command-line/terminal. Getting through the lingo of ‘chmods’ and ‘grep’ is daunting. Are there any good books out there to (or tutorials) to allow designers to learn programming and it’s environment (meaning, basic knowledge on servers, handling permissions, the principals of programming etc?).

    The reason why I ask this question, is that when someone decides to become a webdesigner, he/she never knows the required skillset, unless they start to work in the industry for x-amount of years.

    • 7

      Sorry I don’t really know of any books enough to solidly recommend. I learned these commands along the way, or by reading snippets of various online tutorials. I think the most suitable book or tutorial also depends on how you learn best. So browsing the computing section of a book shop is a good place to start. O’Reilly usually have good quality guides, or maybe even an Idiot’s Guide title – I’ve read a couple of those about various subjects and they’re usually pretty good. Good luck!

    • 8

      I’m in the same situation as you, but actually I already read what I need to read, watch all the tutorials that can out there, but if you want like some compact tutorials in a 1 hours length. Try peepcode, they focus mostly on ruby but there is unix, jquery and other programming language that can suit your need. I will emphasize again try PEEPCODE. worth your money and time. There is just so much information pact inside one video, what I do is take notes and pause.
      And beside it, go read the documentation. It was the last thing I’ve done, and the thing I regret the most. It saves you so much time, if you already grasp the basic knowledge of how programming work, the documentation it’s actually quite fast and easy to read.

    • 9

      Perhaps you could try treehouse.
      Also, personally I’ve benefitted the most from learning Chris Coyier’s WordPress tutorial on lynda.com where i picked up quite a bit of html/css/wordpress’ php . Great stuff for beginners like me I must say. Good luck!

    • 10

      I have a similar situation but mine has to do with learning disabilities. How I get around this that I keep a cheat sheet of all the commands I regularly use. Each time I need to learn a new one I added it to the cheat sheet along with examples and notes. That way you can type out the commands you want on the cheat sheet and paste them into the command line (it is pretty easy to bork a Linux box with a mis-typed command). Right clicking will paste the text into the command prompt with Putty and other command shells I have used. After a while the cheat sheet got really long so I put it into a Wiki with each command as a header so it is easy to navigate. This helped me out a *lot*.

  4. 11

    I would say most Linux and Unix machines I’ve come across these days use Vim as opposed to Vi. If you get a choice, I would use Vim. I actually use Vim as my IDE for Rails programming. Vi is a little screwy to work with sometimes, Vim is much… “Vimproved”… Both have a bit of a learning curve, but once you get used to it it’s probably the most comfortable development environment I’ve ever worked with, but that’s me.

  5. 12

    There are few inconsistencies in the article with the commands, for example you mention the command “czf” (create zip file) and then you use it as “cfz”. Same for xzf (extract zip file) –> you used it as “xfz”.

    Is this a typo or …?

    • 13

      Not a typo; ‘tar’ processes options in a more flexible way than most other cmds, leading to confusion. The following all do the same thing:
      tar tzvf /path/to/archive.tar.gz
      tar vztf /path/to/archive.tar.gz
      tar -vztf /path/to/archive.tar.gz
      tar -v -z -t -f /path/to/archive.tar.gz

      Only caveat is that -f needs to be followed by the filename of the archive. The first form is most prevalent.

      • 14

        It’s worth mentioning inline, in that case. If you were to insert a line there to explain that the options can be scrambled and mean the same thing, that would have made reading the rest of the paragraph easier. This is a reference for people who aren’t familiar with the commands, so it’s not intuitive that cfz and czf are the same.

  6. 15

    Hmm… I am sorry, but I think it is very, very dangerous to encourage people to set up their own web server saying it is as easy as learning a few shell commands.

    There are a lot of pitfalls at various levels that can leave your server vulnerable to attacks. But even just misconfiguring Apache can easily expose your server’s contents to the public unless you really know what you are doing.

    I think there is a reason that system administrators charge $ 50.

    The future belongs to hosted application code à la Engine Yard, Heroku, etc.

    Even as a developer with system administration background, I prefer hosted applications because I can focus on my job as a developer and not as a system administrator.

    • 16

      Yeah I agree…you should not be casually handing even “basic” commands to people who don’t know what they are doing… You’re just begging for them to mess something up. If a graphic artist or [insert any other “beginner”] really wants to learn this stuff…STEP 1 should be to walk them through setting up their own environment on a virtual host or something so that if (read: WHEN) they mess something up, no loss. This article is like handing out loaded guns to kids.

      • 17

        I don’t think I’m encouraging people to set up their own web servers – just to be familiar with the tool set in case they ever need to dive in. The same way that I might try to fix a leaky tap myself rather than calling out a plumber for £50. Yes – sometimes the leak gets even worse and I have to call the plumber anyway. But sometimes I save myself £50. Actually, I think that’s only happened once or twice. The point is – if you’re dead set on fixing a leaky tap, perhaps it’s better to have some basic plumbing skills in advance. But it’s a good comment and thank you Michael for raising it.

        @some dude: the information is out there anyway. More like handing out condoms than loaded guns.

        • 18

          I fully support the idea of knowing your way around in a shell because it can greatly enhance your productivity on your local dev environment. As such, your article is welcome and well done.

          But I still think the general bottom line “If you know some Terminal commands, you can fix those leaky taps on your production server yourself“ is a bad idea because administrating a server is so much more than just a few shell commands.

          When typical web developers start fixing leaky taps, they do not wonder where they come from. They just pick up duct tape and wrap it around everywhere and swing the big hammer around. Because that is all they know and it is fast.

          I have seen it so many times: Permission errors? Just run “chmod -R 777 *“! Fixed. Apache is not working? Paste solutions from google into the Apache config or remove strange lines until it works! Fixed.
          Knowing the right keywords, you can google a lot of unsecured phpmyadmin installations. I could go on and on.

          If you *really* know what you are doing, alright. Fix it yourself. But how many people are that?

          Maybe my argument is a little off-topic as my point is: If you are a web developer and manage your production servers, you are asking for trouble. Trust me.

          • 19

            Unless you’re one of those supertalented people that can develop sites and set up Apache correctly, you shouldn’t.

            As you say, I’ve seen lots of cases where people “fix” permission problems by running chmod 777 * on their web root. For many web applications, especially open source ones like WordPress, this is asking for trouble. People look for exploits in these systems because they’re so popular, so if you’re the kind of person that opens all your permissions and doesn’t apply security updates, your site will be compromised in no time. Remember: just because it doesn’t look compromised, doesn’t mean it isn’t. Clever people compromise servers and use them for warez, spam links etc. without the server owners finding out for months.

            I’ve also seen people installing Apache options just for fun, which is a terrible idea. If you have mod_proxy enabled and don’t set it up right, everyone in the world can use your server as an anonymous proxy server. At best, this will use all your bandwidth and impact your sites. At worst, the police will come to your door to find out why YOUR server has been looking at illegal material online. People run scanning tools to find misconfigured servers like this: they will find your mistake. Whoever had my server’s IP address before me had a misconfigured server: searching Google for my IP shows it listed on many proxy lists from 2 years ago. My apache logs are still getting hits for requests like GET 404 http://www.yahoo.com.

            Bottom line, if you care about your servers, get someone who really knows their stuff.

    • 20

      Not sure how comfortable I would be with a design staff who truly saw the server as a magic black box. And setting up a webserver really is as simple as learning a few shell commands. And if you make some mistakes, you learn how to fix them.

  7. 23

    Here is my PS1 cause im nice :D

    PS1=”[e[1;35m]u[e[0m]@h W[e[0;35m]>[e[0m] ”

    SJ
    shahmirj.com

  8. 24

    Thank you! This article is informative and SO much better written than any website I’ve tried to get this sort of information from.

  9. 25

    Reading the manual is considered an “advanced tip” ? really? That should be the FIRST thing you should be telling people to do….

  10. 26

    Very nice round up . You forgot to warn everyone of the utter destructive power that is the gzip command though :P

  11. 27

    Linux is the Kernel!!!!

    You are referring to bash commands maybe?!?

    Why do you call it GNU/Linux and not Linux? (#why)

    When people see that we use and recommend the name GNU/Linux for a system that many others call just “Linux”, they ask many questions. Here are common questions, and our answers.

    Most operating system distributions based on Linux as kernel are basically modified versions of the GNU operating system.

    We began developing GNU in 1984, years before Linus Torvalds started to write his kernel. Our goal was to develop a complete free operating system. Of course, we did not develop all the parts ourselves—but we led the way. We developed most of the central components, forming the largest single contribution to the whole system. The basic vision was ours too.
    In fairness, we ought to get at least equal mention.

    • 28

      I was trying to imagine what would have the widest appeal and make the most sense to Smashing Magazine readers. In any case, these are commands which run on a computer with a GNU/Linux kernel – I think Linux Commands is an appropriate shortening of that. Just as Windows software is software running on a Windows operating system.

  12. 29

    I wanted to delete the directory containing the old version of my website.

    Did : rm – rf MyBeautifullSite Old
    (notice the space before “Old”)

    Cry !

    Hopefully, I did this on my test server…
    … about ten years ago !
    Learned a lot since.

    I think beginners need a few more tips not to play with a loaded gun !

    • 30

      haha – “You only learn from your mistakes…”

    • 31

      Quote you parameter value ;)
      “MyBeautifullSite Old”

      Or include the ‘i’, just to make sure you got the correct one.
      When you know its correct type a (all), and enter.

      Still allot less worse then typing rm -rf /usr/local
      Which is something I did (almost) once, IN PRODUCTION! Meant to remove some file from that dir but accidentally almost completely removed /usr/local!

  13. 32

    Good article seems to cover most of the basics, pity the discussion of permissions is left to last.
    I think the last command has been left blank sudo – root should work fine.. spaces both sides of the dash are good if you want the root profile as well… most devs I have worked with glaze over when you explain the risks.

  14. 33

    Great guide!

    I’d have loved if you slipped in “sudo !!” when explaining that you have to sudo to do the chown.

    “sudo !!” means “Do that last command again, but as as a superuser this time”. Incredible useful every time you forget to sudo and get that pesky permission denied message.

  15. 34

    Suggesting vim for a newish Linux user is cruel. Nano is far simpler to use and accomplishes most common use cases.

  16. 35

    It’s very nice to see the appearing of such articles on Smashing Magazine. Keep going!

  17. 36

    Thanks! MORE! maybe a nice entry level article about GIT ?

  18. 38

    Julien Desrosiers

    January 23, 2012 2:55 pm

    The command after “If you want to become the superuser for every command, run this:” is blank.
    I think you forgot to write “sudo su” in there.

  19. 39

    I want to thank the people commenting for some very useful information. I’m relearning bash & found the suggested caveats very instructive.

  20. 40

    Take it from a senior level developer who develops for a LAMP environment using a WAMP environment. I could have saved myself a lot of time in previous years had I understood some of the basic *nix commands listed in this article from the get-go. This knowledge is invaluable as you progress in skill, especially if you develop in Windows but distribute in a Linux environment. Don’t get the idea what XAMPP will solve all of your development problems, because it won’t. Learn to use the shell. Sudo, groups, owners, permissions, etc. are all things you simply need to know if you’re going to make any real headway as a developer. My skill level of the shell is a bit above this article’s target-audience’s skill level, but I’m by no means a shell guru. But I can say from experience that this is a valuable article. If you aren’t familiar with the shell, you should bookmark this. Smashing Magazine needs more of this stuff. Paul, you’re getting a two-thumbs up from me. Good read.

    @Michael, I agree with what you’re saying, but think you might be taking this article a bit out of context. My company has a half-dozen professionally managed, dedicated, servers. But the developers still need to know their way around the shell if they’re going to be productive. You have to start somewhere. I would advise entry-level developers buy a personal VPS hosting solution with shell access, for a playground and experiment area. It’s a good way to learn, without all of the potentially fatal consequences.

    • 41

      I know that is quite common to have developers work on production servers. And it may be ok if you just need to access some files or logs on a managed server, but the article talks about Apache configuration and critical file permissions.

      Yes, having knowledge about servers does not hurt but it is a completely different field and one where most developers will never ever get proficient enough to run their own server. They *will* mess things up in some way eventually.

      So I would advise developers to host their app code somewhere because it is the fastest, cleanest, safest, easiest and most productive solution (plus you can scale your web app on demand, something you cannot do with basic server hosting).

  21. 42

    u read my mind i was waiting for this for long time.
    Keep posting GR8.

  22. 43

    if you use single quotes while grepping, you can spare yourself the hideous escaping of doublequotes and the like ;)

    like: grep ‘this is a “example”, right?’ *

  23. 44

    Excellent article ! I second the previous suggestion to do one similar for Git.

  24. 45

    Innovative Web Provider

    January 24, 2012 12:59 am

    I just recently installed Ubuntu , which is a linux os. Your post really helps me a lot because I am still newbie in linux. Thanks for this!

  25. 46

    Nice information.Thank you.

  26. 47

    Using those everyday and can tell that this is really big help for the beginners. Will share this list among some colleagues and friends :)

  27. 48

    Thanks, Great Article, Great Compilation!

    PS: The first ‘More Advanced Tip’ is quite irrelevant and naughty!

  28. 49

    Nice article for beginners. Commands related to adding users to groups or editing profiles to provide shared access to folders instead of changing ownership could have been added to help.

  29. 50

    Nice article. But you might point out, that Linux is case sensitive unlike default behavior in Windows.
    “find / -name *.gif” will never find *.GIF but “find / -iname *.gif” will ignore case and find both *.gif and *.GIF

  30. 51

    This is a good article. Thanks for the information!

  31. 52

    Even after using Linux for the past year I found this article very useful, thank you for the time and effort you put into writing it! An advanced tutorial would be great =D

  32. 53

    Just wondering why you didn’t use “mysqlimport” for restoring (mysql) database?

    • 54

      Thanks for pointing that out. I forgot about that command. It does the same thing but is easier to use than piping SQL through the mysql command.

      @ericG – Yes it would be more secure to just use -p so the password doesn’t appear in clear text as part of the command. There are uses for a full command including the password (like backup scripts and aliases), but in those cases, the user should have as minimal access rights as possible (eg SELECT only).

  33. 55

    When using SSH, you don’t need to use the -l option, you can use @ instead.

    ssh -l admin livewebserver

    ssh admin@livewebserver

  34. 56

    Here’s my contribution to useful shell scripts.

    To find out how much free disk space you have, type:

    df -h

    To find out which folders on your system are using the most space, use:

    du -h –max-depth=1

    For that second script, change to the folder you want to scan before you run it. If you want to scan every folder on the entire server, do “cd /” first. It will take a few minutes to calculate disk usage.

  35. 58

    Thanks a lot, very useful, practical article.

  36. 59

    You are so right. Absolutely a must for any developer.
    Another nice trick is the use of {} when defining paths.
    tail -f /srv/www/{web1,web2}/logs/access.log will tail both /srv/www/web1/logs/access.log and /srv/www/web2/logs/access.log

  37. 60

    “Mac, Linux and Windows computers today are mainly controlled through user-friendly feature-rich graphical user interfaces (GUIs), with menus, scroll bars and drag-and-drop interfaces. But all of the basic stuff can still be accomplished by typing text commands into the terminal or command prompt.”

    I would say that it is more appropriate to say:

    “Mac, Linux and Windows computers today are best controlled by typing text commands into the terminal or command prompt. But all of the basic stuff can still be accomplished through user-friendly feature-rich graphical user interfaces (GUIs), with menus, scroll bars and drag-and-drop interfaces.”

    I’ll admit I’m a CLI snob, but you can accomplish stuff easily using the terminal that is difficult (often impossible) using the GUI, and do so much faster.

  38. 61

    Great article ! Good beginner stuff. Judging from the amount of comments this is a topic that could be explored further.
    It also seems to have drawn quite a few douche bags to comment on this post too.
    $ sudo rm -r
    Spread the knowledge ! maybe an article about bash aliases ?

  39. 62

    Sure, you could learn all of those cryptic commands and hassle with unix/Linux OR you could just get a Windows server and handle your stuff via a comfy RemoteDesktop connection with the GUI you are already used to work with. Except maybe the price, there is absolutely no reason NOT to host your rails, php, .NET or whatever site and database on a Windows server.

  40. 63

    A nice beginners guide, thanks for putting it together.

    I’d like to recommend a change where you explain mysql commands.

    Following your example
    mysql –user=”username” –password=”password” dbname
    would, in most cases, store the database password in the logged in user’s command history which in my paranoid, please don’t make me clean up another compromised website mindset could be serious security issue

    if you use
    mysql –user=”username” -p dbname
    or even
    mysql -u username -p
    which is easier to type, and in my mind one of the reasons to use command line over GUI interfaces for these tasks is because they are faster — not to mention less strain on overworked hands and wrists.

    it will then prompt you for your password and not store more than the above command in your history

  41. 64

    THANK YOU! I use SSH on my iPad and iPhone and computers all the time and everybody thinks I’m some programmer or something (graphic designer here) and I keep trying to tell them it’s not hard to learn a handful of commands – that’s what used to be EXPECTED of all computer users in their parents generation and it’s STILL the easiest way to do many things.

    Thanks for the intro course, I will use this as a guide to teach my girlfriend (and she can use it as a reference later)

  42. 65

    Thank you for giving us a great starting point! I think many programmers out there want others to stay in the dark. Some are saying, you’ve handed people a loaded gun! But, I think they are aggrandizing their positions… That attitude runs very deep in some programmers… they try to protect their turf under the guise of protecting others, but mainly they’re trying to shroud their work in mystery and keep the demand. You can even see the look on their faces! I think it’s obvious that they had a loaded gun in their hand at one point. I’m sure they still don’t have a masters degree in programming or know every command their is to know, so they’re still holding a loaded gun. Technically, if you don’t know the totality of your language, you shouldn’t be doing using of it in the real world, because you allow a hacker who knows more than you access… you may compromise your system. But they still code.

    Anyway, I think you did an awesome job getting people’s feet wet. Some of these people here are acting like heroes for insisting you take down this sensitive information… First of all, I installed a CMS on my server a while back, and the instructions told me how to change the permissions using my FTP program… oh no!!!!! To make a long story short, I changed the permissions of every file in the website and then I lost my job and now I’m homeless. Anyway, thank you for getting us off to a great start.

    Thanks!

    • 66

      You’re way off the mark. After more than ten years in the business I have yet to meet any programmer who deliberately tried to keep co-workers in the dark.

      If anything, the problem I’ve faced has been a complete lack of interest in how code and computers work — most people just want magic. There’s no turf to protect because nobody is trespassing.

  43. 67

    A really helpful article!.

  44. 68

    Instead of `ssh -l username http://www.myserver.com`, i always use `ssh username@myserver.com`.

    Is there any difference in security or whatsover ?
    Or just another way of doing the same?

  45. 69

    There really is a lot of talk about the command line thing nowadays. I have always regarded them things of the past or products of lazy (ass) developers. Those black screens are abuzz on twitter and talks like fronteers12 and smashingconf (great conferences by the way). The commanline should be easy because we know al the buttons in Photoshop (That’s a lot off “tosh” ofcourse).

    But.. I am persuaded and will experiment with “Yeoman” from Addy Osmani

  46. 70

    I am not a server administrator, or a graphic designer, but I know quite a bit about programming and security. I am rather dismayed at some of the comments here from self-appointed ‘experts’ who would rather keep the graphic designers in the dark (and perhaps hope to charge them $$$ for even the smallest fixes). Hoping to keep people uninformed ‘just in case they do something stupid’ is just stupidity of the next order.

    It’s fair enough to warn that some of these ‘tricks’ (especially chmod) can and will cause dangerous security holes, but that’s not a reason not to share knowledge. We were all novices once. Nobody knows everything, and you have to start somewhere.

    You can do a lot of damage with a sharp kitchen knife, but if my child wants to learn to cook, I will sooner or later get the knife out (and guide him carefully). It’s an essential tool. I still cut myself occasionally. (More often I smash up my fingernails on the damn grater!)

    For safest results, learn shell basics on your own local machine first. Windows users can install something like cygwin to get a terminal whose commands and syntax closely resemble those of the unix world. Mac users can just start up Terminal. There are several books and online tutorials introducing UNIX to Mac OS X users, most of which focus on shell commands. That’s how I learned these commands, and it’s surprising how useful they have been.

    I agree with others here that have called for more articles on this subject, perhaps with more focus on security, or even some basic shell scripting.

    Keep learning, stay curious, and keep backups.

  47. 71

    If you want to get a more solid grounding in use of the command line then the tutorial http://linuxtutorial.todolistme.net covers a lot of what was covered in this article but with quite generous descriptions of what is going on and why.

  48. 72

    You can find other linux commands in here too, and you can also learn some scripting as well

    http://www.linuxcommand.net

  49. 73

    You can find other linux commands in here

    learn it us well general commands

    linuxcmd7.blogspot.com

  50. 74

    how to copy a shell program in one account to another account ?.my lab is a lan.We are connected to a linux server using putty.everyone has a user name but have no password.so how i copy shell program to one account to another account.give me the commands.

↑ Back to top