As a web developer you are assigned a task to administer Linux based web server or your company says we don’t like Windows you must have to choose Mac or Linux based system. Things get worse when you are afraid of command line and hesitate to write commands yourself. This series of articles will help you jump start with command line quickly, easily, and after reading the whole series, you will not fear to run commands yourself.
- Part 1 (current)
- Part 2
I assume you have successfully installed your favorite Linux distribution and you have some basic understanding of Unix/Linux based systems and at least must be comfortable working with the GUI environment of the OS.
Bash (Bourne Again SHell) is a shell program that interprets the command you enter and decides whether to execute itself, or execute using other binary, or execute a script. Bash is part of GNU project and it’s was developed as a replace for sh (Bourne shell) used in Unix. Bash not only comes with Linux but also the default shell for Mac OS X. So the commands which you will learn in this series will work in both Linux and Mac OS X.
Today almost every client OS comes with GUI so we need some terminal emulation program which allows us to emulate Bash shell in a terminal window. Default terminal in GNOME is gnome-terminal in GNOME, and konsole in KDE based environments. Similarly default terminal emulator in Mac OS X is Terminal.app.
I am a Linux user and will use gnome-terminal in Ubuntu for running commands. Don’t worry all the commands will also work in other Linux distributions and may work in Mac OS X as well but output may vary a bit. I will only show you the most commonly used options for each command. As it’s not a reference guide. My point is to give you basic understanding of each command so you can further explore it on your own.
User, Machine, Privilege Info
Open your terminal emulator and there you will see info related to logged in user, machine name, and a sign which represents super user or normal user. On my notebook it shows:
ifadey is my username separated by @ symbol and then Inspiron-1564 is the computer name. Then a $ (dollar sign) means I am a standard user and don’t have root/admin privileges. If your terminal is displaying # (hash) instead of $, it means your user have root privileges.
What exactly is a command? When you type a command in the terminal and hit enter. What’s the thing that makes it perform some operation? Actually the command can be an executable binary, or a Bash built-in command (a command integrated with Bash shell) or a script or an alias to some existing command. You can check the command type using type command. For example pwd, and cd are shell built-in commands and you can check that by typing the following commands in the terminal.
ifadey@Inspiron-1564:~$ type pwd pwd is a shell builtin ifadey@Inspiron-1564:~$ type cd cd is a shell builtin
Similarly cp and mkdir are executable binaries. Type following commands to check it.
ifadey@Inspiron-1564:~$ type cp cp is /bin/cp ifadey@Inspiron-1564:~$ type mkdir mkdir is /bin/mkdir
Don’t worry what these commands are. For now just remember there are four types of commands and you can check that using type command.
Navigating the File System
In GUI, you may navigate the file system using Nautilus (default file manager in GNOME) or Dolphin (default in KDE). You browser your file system using these graphical file managers or may be some other. But in command line you can navigate the file system using three commands.
- pwd – Print the name of the current working directory.
- ls – List directory contents
- cd – Change the shell working directory.
pwd command prints Present or Current Working Directory. It’s the currently targeted directory where you can run commands like ls, cp, rm, mkdir, etc without providing them the path as an argument. You will see later what I mean by command argument.
Open your terminal and type pwd.
ifadey@Inspiron-1564:~$ pwd /home/ifadey
This command is used to display contents of directory. Type ls at your terminal.
ifadey@Inspiron-1564:~$ ls Desktop Downloads examples.desktop Public Ubuntu One Documents Music Pictures Templates Videos
You will see the list of files and folders in current working directory. Now type “ls /” command to display contents of root directory.
ifadey@Inspiron-1564:~$ ls /
Even though our current directory is home but you can display contents of other directories using command arguments. In the above command / is the argument passed to ls command. Try another command.
ifadey@Inspiron-1564:~$ ls /usr
Above command display contents of usr directory under root. Note that the argument passed in above command is /usr. This is an absolute path or full path because it’s not relative to some directory. Also absolute paths start always start from root directory / (forward slash). Using absolute paths will always give same output no matter what your current working directory is.
Relative path define a location relative to current working directory. Let’s see some commands some examples of ls command with relative paths.
ifadey@Inspiron-1564:~$ ls .
Above command is not simple ls. It’s ls . (dot). In CLI, . (dot) symbol means current directory. So when you ran the above command, it actually printed the contents of current working directory as it did when ran without (dot) argument.
Similarly .. two dots represent the parent directory of current directory. Let’s try it.
ifadey@Inspiron-1564:~$ ls .. asalam ifadey
Above command print the contents of /home directory because it’s the parent directory of my current directory (/home/ifadey). Now above path is a relative path because it targets a directory relative to present directory. Here some more examples of relative paths.
ifadey@Inspiron-1564:~$ ls ../asalam/Desktop/ ifadey@Inspiron-1564:~$ ls ./Downloads/ ifadey@Inspiron-1564:~$ ls Downloads/
First path passed to ls command moves to its parent directory because of .. (two dots). And then it navigates to asalam and finally to Desktop in asalam.
As mentioned above . (dot) represents the current directory. So in second command, path targets Downloads directory in current directory.
Third command will produce the same output as second command did. If you write a directory name, or file name without any prefix, will try to find it in the current directory.
Every command has options with which you can change its behavior. Try ls command with -l option.
ifadey@Inspiron-1564:~$ ls -l drwxr-xr-x 2 ifadey ifadey 4096 Jun 23 00:33 Documents drwxr-xr-x 4 ifadey ifadey 4096 Jul 19 20:11 Downloads drwxr-xr-x 2 ifadey ifadey 4096 Jun 30 16:24 dwhelper -rw-r--r-- 1 ifadey ifadey 8445 Jun 23 00:27 examples.desktop
-l option display the contents of directory in long detailed format. This format displays information in following manner.
- Note the first alphabet d or – (dash) in each row. If it displays d, it means it’s a directory and – (dash) means it’s a file.
- Then first rwx means owner of this file/directory have read, write, and execute privileges. More info on file rights in later articles.
- Second rwx is for all the users in group to which this file/directory belongs.
- Third rwx is for everyone else. If you find – (dash) in rwx, it means that particular privilege is missing. For example r-x means read, no write access, and execute.
- Second column display numbers (2, 4, 2, 1). These numbers represent how many hard links exist for this file/directory. More info on links in later articles.
- Third column display usernames who own the file/directory.
- Fourth column display the group name to which this file/directory belongs.
- Then size of file/directory in bytes. Use -h option to display sizes in human readable format. For example ls -lh.
- Sixth column displays the Last modified date/time.
- Finally the name of file/directory.
In *nix based operating systems, hidden files are prefixed with . (dot). For example .filename is a hidden file. Use -a option to display all files including the hidden ones as well.
ifadey@Inspiron-1564:~$ ls -a
Use ls –help option to view all the options available for ls command.
cd command is used to change current working directory to some other directory according to the path passed as argument to it. When you open the terminal, default directory is your home directory (/home/username). You can always check your current directory using pwd command. Here’s an example using both cd and pwd commands.
ifadey@Inspiron-1564:~$ pwd /home/ifadey ifadey@Inspiron-1564:~$ cd Desktop/ ifadey@Inspiron-1564:~/Desktop$ pwd /home/ifadey/Desktop
Above example changes my current directory from ifadey to Desktop. This command can work with both absolute and relative paths like ls command. Here are some more examples.
ifadey@Inspiron-1564:~$ cd /usr/share/ ifadey@Inspiron-1564:/usr/share$ cd ../bin
cd command is easy to use but there are some interesting shortcuts which you can use with this command. First no matter what your current directory is, simply type cd without passing any argument. It will take you to your home directory.
ifadey@Inspiron-1564:/usr/share$ pwd /usr/share ifadey@Inspiron-1564:/usr/share$ cd ifadey@Inspiron-1564:~$ pwd /home/ifadey
Second shortcut is cd – (dash). This will take you to previous working directory. It’s similar to Back button in GUI based file manager. Here’s an example usage.
ifadey@Inspiron-1564:~$ cd Pictures/ ifadey@Inspiron-1564:~/Pictures$ pwd /home/ifadey/Pictures ifadey@Inspiron-1564:~/Pictures$ cd - /home/ifadey
Another useful shortcut is cd ~username. It will navigate or change your current directory to home directory of user whose username is entered after ~ (Tilde) sign.
ifadey@Inspiron-1564:~$ pwd /home/ifadey ifadey@Inspiron-1564:~$ cd ~asalam ifadey@Inspiron-1564:/home/asalam$ pwd /home/asalam
My home directory is /home/ifadey and I moved to other user’s home directory using command cd ~asalam.
Need more Help?
Most commands have –help option with which you can view additional details regarding command usage. But you can view more detailed reference for each command using man and help commands.
help command is used to display detailed information along command options for shell built-in commands. If you are not sure about command type, you can check it using type command as explained above. Here’s an example usage:
ifadey@Inspiron-1564:~$ type cd cd is a shell builtin ifadey@Inspiron-1564:~$ help cd
You will see a long description with command options after running the help command.
man (manual) command is used to display reference documents for executable commands. Actually it contains reference material for other things as well like System Calls, C library APIs, and much more. But for the time being remember this command can give you reference documents for executable commands. type command will print the path to binary/executable file used to run that particular command.
ifadey@Inspiron-1564:~$ type cp cp is /bin/cp ifadey@Inspiron-1564:~$ man cp
Pressing q will exist man pages.
In this first part you learned about command types and the type command. Then you learned how to navigate the file system using pwd, cd, and ls commands. In the end you learned commands to get help regarding the command usage. help command for shell built-in and man for binaries.