|
Linux: The Beginners Guide
![]() |
The Linux Command Line
Before the times of Ubuntu’s elegant menus and intricate desktop backgrounds, Linux was accessed only by specific command line instructions. Similar to Microsoft Windows MS-DOS prompt, Linux offers its users the option to access and control the operating system without a user interface. We will be briefly discussing the most commonly used commands, and providing simple examples of there usage.
There are many useful shortcuts that Linux provides to its command-line users. One of the biggest being tab completion. Tab completion is a feature that allows a user to simply type part of a command or directory, and Linux will attempt to finish the command. For example, if I were to type ‘cd /ro’ and hit the tab key, Linux attempts to finish the command for me by showing ‘cd /root/’. Another shortcut any new users should learn to take advantage of is the ability to scroll up and down with the arrow keys. By using the arrow keys, the history of all commands that you have previously issued can be displayed and reused without having to be retyped.
It is very important to know that if you are genuinely serious about learning the Linux command line, you should go over each command referenced here over and over until it becomes second nature. The commands listed in this article are the very basic commands, and it is important to know that there are hundreds of commands and options out there that are beyond the scope of this article.
The terminology used in this write-up may be foreign to some readers, and thus it is important for us to define certain terms as we will be using them throughout this article.
Definitions:
Switch:
A switch can be defined as an option passed to a command. For example, the command ‘ls –l’ (what the command does is not important now, but notice the bolded switch). This is an option that tells the ‘ls’ command to perform differently than default.
System:
Any computer running a Linux distribution.
Commands:
man
First and foremost, the most important command to learn for any new Linux user is the ‘man’ command. man is short for user manual, and provides a detailed explanation and sample usage for almost all Linux commands.
Sample usage:
man mkdir (note: we will be reviewing the ‘mkdir’ command later)Sample output:
NAMEmkdir - make directoriesSYNOPSISmkdir [OPTION] DIRECTORY...DESCRIPTIONCreate the DIRECTORY(ies), if they do not already exist.
ls
The command that I use the most in my day-to-day usage of Linux is the ‘ls’ command. ‘ls’ is short for list, and will display the contents of the current folder. While the ‘ls’ command has many different options and ways to display data, this article will cover the most important and widely used options.
Note: In most Linux distributions, to tell the difference between a file and a directory, notice the colors that the ‘ls’ command uses. Blue is usually indicative of a folder, green indicates a file with executable permissions (usually a binary program or script)), and gray is a plain file (somewhat like a text file in Windows).
Sample usage:
lsSample output:
bin games kerberos libexec sbin src X11R6 etc include lib local share tmp‘ls –l’ will print out the contents of the current directory – in a more readable list format than just using the default command, ‘ls’. It also displays the folder permissions, who created the file/directory, and the last time it was modified.
Sample usage:
ls –lSample output:
total 180 drwxr-xr-x 2 root root 36864 Oct 27 04:02 bin drwxr-xr-x 2 root root 4096 Feb 21 2005 etc drwxr-xr-x 2 root root 4096 Feb 21 2005 games drwxr-xr-x 64 root root 12288 Oct 14 12:53 include drwxr-xr-x 6 root root 4096 Jun 26 18:51 kerberos drwxr-xr-x 74 root root 36864 Oct 23 04:04 lib drwxr-xr-x 8 root root 4096 Oct 23 04:04 libexec drwxr-xr-x 11 root root 4096 Sep 27 12:51 local drwxr-xr-x 2 root root 12288 Oct 23 04:04 sbin drwxr-xr-x 131 root root 4096 Oct 7 11:22 share drwxr-xr-x 4 root root 4096 Sep 27 13:04 src lrwxrwxrwx 1 root root 10 Sep 27 12:51 tmp -> ../var/tmp rwxr-xr-x 7 root root 4096 Sep 27 12:51 X11RNotice the differences in formatting? The furthermost left-hand column are the folder/file permissions. You may find the table below helpful if you are interested in knowing what all the columns represent:
drwxr-xr-x 2 root root 36864 Oct 27 04:02 bin
PERMISSIONS |
DIRECTORIES INSIDE |
CREATOR |
OWNER |
SIZE |
DATE |
NAME |
drwxr-xr-x |
2 |
root |
root |
36864 |
Oct 27 04:02 |
bin |
cd
There will come a time when you need to navigate through the directories of your machine. The command to change the current directory is ‘cd’. If you are at all familiar with MS-DOS, the command has the exact same usage. When I was first learning the Linux syntax, I found it good practice to just try moving around the directory tree of my system using the ‘ls’ and ‘cd’ commands.
Sample usage:
cd /etcSample output:
None.The syntax to move back a directory:
Sample usage:
cd ..Sample output:
None.pwd
To print the current directory, use the ‘pwd’ command.
Sample usage:
pwdSample output:
/homemkdir
To make a directory within the current working directory, you’ll want to use the ‘mkdir’ command.Sample usage:
mkdir dirnameSample output:
None.
rm
To permanently delete a file/folder, the command to do so is the ‘rm’ command (short for remove).
Sample usage:
rm filenameSample output:
rm: remove regular empty file `filename'?Confirm the deletion of the file by entering ‘y’ to the prompt, and hitting return.
Some switches you may be interested in are –r and –f.
-r stands for recursive, and is used when deleting entire directories.
-f is used to force a deletion of a file without confirmation. Since there will be no confirmation of file deletion, there will subsequently be no sample output.
Sample usage:
rm –rf directorynameSample output:
None.
cp
‘cp’ is short for copy and it does just that – copies a folder or directory to another location.
Sample usage:
cp test.txt /home/test.txtSample output:
None.The command above will copy test.txt (from the current directory) into the /home directory. To simply make a copy in the same working directory:
Sample usage:
cp test.txt test2.txtSample output:
None.
mv
To permanently move a file or directory instead of making a copy – use the ‘mv’ command. The syntax for ‘mv’ is the exact same for ‘cp’.
Sample usage:
mv test.txt /home/test.txtSample output:
None.Note: Renaming files in Linux is essentially just moving file1 to file2. The example below will rename test.txt to test2.txt.
Sample usage:
mv test.txt test2.txtSample output:
None.
cat
‘cat’ prints out the contents of a file.
Sample usage:
cat /var/log/messagesSample output:
Nov 27 13:16:18 kitty nfslock: rpc.statd shutdown succeeded Nov 27 13:16:18 kitty portmap: portmap shutdown succeeded Nov 27 13:16:19 kitty kernel: Kernel logging (proc) stopped. Nov 27 13:16:19 kitty kernel: Kernel log daemon terminating. Nov 27 13:16:20 kitty syslog: klogd shutdown succeeded Nov 27 13:16:20 kitty exiting on signal 15 Nov 27 13:18:53 kitty syslogd 1.4.1: restart.
passwd
‘passwd’ changes your password.
Sample usage:
passwdSample usage:
New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully.ps
‘ps’ displays the currently running processes. ‘ps’ by default doesn’t list all the running processes – so I heavily suggest becoming accustomed to using the switches that show all processes running. The switches –e and –f will display all the processes (-e), and format it appropriately with all the necessary information (-f).
Sample usage:
ps –efSample output:
UID PID PPID C STIME TTY TIME CMD root 1 0 0 Nov27 ? 00:00:00 init [3] root 2 1 0 Nov27 ? 00:00:00 [ksoftirqd/0] root 3 1 0 Nov27 ? 00:00:00 [events/0] root 4 3 0 Nov27 ? 00:00:00 [khelper] root 5 3 0 Nov27 ? 00:00:00 [kacpid] root 22 3 0 Nov27 ? 00:00:00 [kblockd/0] root 23 1 0 Nov27 ? 00:00:00 [khubd] root 40 3 0 Nov27 ? 00:00:00 [pdflush] root 41 3 0 Nov27 ? 00:00:00 [pdflush] root 42 1 0 Nov27 ? 00:00:00 [kswapd0] root 43 3 0 Nov27 ? 00:00:00 [aio/0] root 189 1 0 Nov27 ? 00:00:00 [kseriod] root 427 1 0 Nov27 ? 00:00:00 [kjournald] root 1599 1 0 Nov27 ? 00:00:00 httpdkill
The ‘kill’ command is primarily used to end processes that cannot be shutdown gracefully. If you tend to think in Microsoft terms, it’s the Linux version of CTRL+ALT+DEL.
The format for the command is ‘kill –PRIORITY PID’. The priority is ranked from 1 to 9, where 9 has the highest priority in the system. Killing a process at the highest level means that the system will do absolutely everything in its power to stop a running program. PID stands for “Process ID” – and is found by using the ‘ps –ef’ command.
Sample usage:
kill -9 1599Sample output:
None.
grep
‘grep’ is used to find a specified string of text within a file.
Sample usage:
grep root /etc/passwdSample output:
root:x:0:0:root:/root:/bin/bash
nano
‘nano’ is a text editor that ships with most Linux distributions. Depending on what flavor of Linux is being used, the command ‘pico’ may replace ‘nano’. Despite the different naming conventions, both commands will take you to essentially the same program. The text editor works much like the Windows Notepad. ‘nano’ lists a series of keyboard shortcuts at the bottom of the window.
Sample usage:
nano /home/newfileSample output:
(a text-based interface is displayed – if the file is new than the screen will be empty, otherwise the contents of the file will be shown)
CTRL + X will quit the editor.
CTRL + O will prompt to save the file.
CTRL + W will find a specified string of text




