Learning to use (and love) the Command Line


Everyone has heard about it, and most new users dread it… the command line. However, it really doesn't need to be so frightening. After you get used to it, it really is a very useful tool. Below you will find a summary of many of the basic commands you will need in order to use the command line effectively.

First, some background:

The Linux command line is much like DOS, except there are some very important differences. First, it is case sensitive. In dos cd desktop and cd Desktop did the same thing. In Linux, this is not the case. Your desktop folder is located at ~/Desktop, and if you type ~/desktop it will tell you the folder does not exist. There are times when you might come into contact with capital letters, so be aware.

Second, there are permissions to consider. You, as a normal user, are only allowed access to your home folder (~, or /home/<username>). You can read, write and delete files to your hearts content here (not that this is a good idea, but you can). Outside of this folder you need to use the root user to change anything. Basically, use the command "su -" to switch to the root user. You will need to enter the root password you created at install. You now have the power to break your system, so use it carefully. Only run commands as root if you absolutely need to, to avoid the risk of breaking stuff.

To get to the command line, there is an icon on the dock with a command prompt. Also, if you go to the menu, under System there is an entry for the terminal. These are the "pretty" terminals with transparency. For a simpler version (equally functional, just more plain looking), use the terminal choice at the top level of the menu, or pick xterm under the System menu.

OK, time to learn some basic commands

su - Switch User & Super User

su is the switch user command. If you have multiple users you can switch between them on the terminal with this command (note, this does not affect your GUI session). If I have a user "bob" that I want to switch to, I can use this command to switch (assuming I have the password). The exit command can be used to stop being that user:

[df4@localhost ~]$ su bob
[root@localhost ~]# exit

And the video for this command:

There are many cases where you need root permissions to edit system files. In order to access these files, you need to be "root", or the admin user. To switch to root, the best way to use this command. It will send you to roots home folder (/root) and give you roots commands (many of which are unavailable to normal users for security reasons). Again, the exit command returns you to your normal user:

   [df4@localhost ~]$ su -
   [root@localhost ~]# exit

Another use of the "su" command is a more temporary "su -c". This runs only the single command you specify as the superuser, then returns you to your normal prompt:

[df4@localhost ~]$ su -c 'apt-get install ati'

ls - List

ls (1) is the basic command to list all the files in a directory. It is a rough equivelent of the DOS command dir, but much more powerful. By itself it lists the files in the directory you are currently in, however if you append a directory after it (2) it will list the files there. There are many extra options you can pass with the ls command. Some common ones are -a and -l, often combined as ls -al (3). ls -a shows all files (including hidden files) and -l shows more info about the files (owner, permissions, etc.). For a full listing of all options available with the ls command, type ls —help (4).


[df4@localhost ~]$ ls
   Desktop/               irc_minutes.odt   Screenshot.png          wallpaper/
   eosking_backpaper.gif  Screenshot-1.png  Swiftfox_wallpaper.png
   install-swiftfox.sh*   Screenshot-2.png  tmp/


[df4@localhost ~]$ ls /opt
   firefox/  swiftfox/  thunderbird/


[df4@localhost ~]$ ls -al
   total 3156
   drwxr-xr-x 38 df4  df4    4096 Apr 19 14:25 ./
   drwxr-xr-x  3 root root   4096 Apr 16 02:32 ../
   drwx------  2 df4  df4    4096 Apr 18 16:33 .AbiSuite/
   -rw-rw-r--  1 df4  df4       0 Apr 16 02:32 .adesklets
   -rw-------  1 df4  df4     547 Apr 19 14:18 .bash_history
   -rw-r--r--  1 df4  df4      24 Apr 16 02:31 .bash_logout
   -rw-r--r--  1 df4  df4     376 Apr 16 02:31 .bash_profile
   -rw-r--r--  1 df4  df4     124 Apr 16 02:31 .bashrc
   drwx------  2 df4  df4    4096 Apr 18 17:59 .bcast/
   drwxr-xr-x  2 df4  df4    4096 Apr 19 14:25 .beryl/
   -rw-rw-r--  1 df4  df4     187 Apr 19 14:25 .beryl-managerrc
   drwx------  5 df4  df4    4096 Apr 17 13:08 .cache/
   drwxrwxr-x  4 df4  df4    4096 Apr 18 17:34 .codered/
   drwx------  9 df4  df4    4096 Apr 17 15:50 .config/
   drwxrwxr-x  2 df4  df4    4096 Apr 18 17:37 .cube/
   drwxrwxr-x  2 df4  df4    4096 Apr 16 02:31 .dayplanner/
   drwxr-xr-x  2 df4  df4    4096 Apr 16 02:31 .desklets/
   drwxr-xr-x  2 df4  df4    4096 Apr 19 13:25 Desktop/
   -rw-------  1 df4  df4      26 Apr 16 02:31 .dmrc
   drwxr-xr-x  4 df4  df4    4096 Apr 17 19:29 .emerald/
   -rw-rw-r--  1 df4  df4    2020 Apr 18 17:48 eosking_backpaper.gif
   drwxrwxr-x  2 df4  df4    4096 Apr 17 17:03 .fontconfig/
   -rw-r--r--  1 df4  df4     516 Apr 16 02:31 .fonts.conf
   drwxrwxr-x  4 df4  df4    4096 Apr 19 13:28 .fullcircle/
   drwx------  4 df4  df4    4096 Apr 19 14:03 .gaim/
   drwx------  4 df4  df4    4096 Apr 19 14:25 .gconf/
   drwx------  2 df4  df4    4096 Apr 19 14:30 .gconfd/
   -rw-rw-r--  1 df4  df4     419 Apr 18 17:18 .glade2
   drwx------  3 df4  df4    4096 Apr 18 17:13 .gnome2/
   drwx------  2 df4  df4    4096 Apr 16 02:32 .gnome2_private/
   drwxrwxr-x  2 df4  df4    4096 Apr 17 22:04 .gstreamer-0.10/
   -rw-------  1 df4  df4       0 Apr 16 02:31 .gtk-bookmarks
   -rw-------  1 df4  df4    2310 Apr 19 14:25 .ICEauthority
   drwxr-xr-x  3 df4  df4    4096 Apr 18 17:54 .icons/
   -rwxrwxr-x  1 df4  df4    1279 Mar 21 16:20 install-swiftfox.sh
-rw-rw-r--  1 df4  df4    9668 Apr 18 16:34 irc_minutes.odt
drwx------  2 df4  df4    4096 Apr 18 17:37 .kde/
drwxrwxr-x  3 df4  df4    4096 Apr 16 02:32 .local/
drwx------  3 df4  df4    4096 Apr 17 15:56 .macromedia/
drwxr-xr-x  2 df4  df4    4096 Apr 18 17:37 .mcop/
-rw-rw-r--  1 df4  df4       0 Apr 16 02:32 .mdk-menu-migrated
drwx------  3 df4  df4    4096 Apr 19 13:08 .mozilla/
drwxrwxr-x  2 df4  df4    4096 Apr 17 15:44 .mplayer/
-rwxrwxr-x  1 df4  df4      18 Apr 16 02:34 .net_monitorrc
   drwxrwxr-x  3 df4  df4    4096 Apr 17 19:52 .nvu/
   -rw-rw-r--  1 df4  df4    1624 Apr 19 13:05 .recently-used.xbel
   -rw-rw-r--  1 df4  df4  807839 Apr 18 15:36 Screenshot-1.png
   -rw-rw-r--  1 df4  df4  807283 Apr 18 17:11 Screenshot-2.png
   -rw-rw-r--  1 df4  df4  389480 Apr 18 15:33 Screenshot.png
   drwxr-x---  2 df4  df4    4096 Apr 18 15:27 .sodipodi/
   -rw-rw-r--  1 df4  df4  957970 Apr 18 15:04 Swiftfox_wallpaper.png
   drwxrwxr-x  3 df4  df4    4096 Apr 18 17:58 .themes/
   drwxrwxr-x  3 df4  df4    4096 Apr 17 13:09 .thumbnails/
   drwx------  3 df4  df4    4096 Apr 19 13:08 .thunderbird/
   drwxr-xr-x  3 df4  df4    4096 Apr 16 02:31 .tilda/
   drwx------  6 df4  df4    4096 Apr 19 14:30 tmp/
   drwxrwxr-x  2 df4  df4    4096 Apr 18 15:04 wallpaper/
   -rw-r--r--  1 df4  df4    1185 Apr 19 13:07 .wbar
   drwxr-xr-x  2 df4  df4    4096 Apr 16 02:31 .Wbar/
   -rw-------  1 df4  df4       0 Apr 18 18:03 .Xauthority
   drwx------  4 df4  df4    4096 Apr 18 17:34 .xchat2/
   -rw-rw-r--  1 df4  df4    7062 Apr 16 02:31 .xscreensaver
   -rw-r--r--  1 df4  df4    5192 Apr 19 14:31 .xsession-errors


[df4@localhost ~]$ ls --help
   Usage: ls [ OPTION ]... [ FILE ]...
   List information about the FILEs (the current directory by default).
   Sort entries alphabetically if none of -cftuvSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. --author with -l, print the author of each file -b, --escape print octal escapes for nongraphic characters --block-size=SIZE use SIZE-byte blocks -B, --ignore-backups do not list implied entries ending with ~ -c with -lt: sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort by ctime -C list entries by columns --color[=WHEN] control whether color is used to distinguish file types. WHEN may be 'never', 'always', or 'auto' -d, --directory list directory entries instead of contents, and do not dereference symbolic links -D, --dired generate output designed for Emacs' dired mode -f do not sort, enable -aU, disable -lst -F, --classify append indicator (one of */=>@|) to entries --file-type likewise, except do not append '*' --format=WORD across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C --full-time like -l --time-style=full-iso -g like -l, but do not list owner -G, --no-group like -l, but do not list group -h, --human-readable with -l, print sizes in human readable format (e.g., 1K 234M 2G) --si likewise, but use powers of 1000 not 1024 -H, --dereference-command-line follow symbolic links listed on the command line --dereference-command-line-symlink-to-dir follow each command line symbolic link that points to a directory --hide=PATTERN do not list implied entries matching shell PATTERN (overridden by -a or -A) --indicator-style=WORD append indicator with style WORD to entry names: none (default), slash (-p), file-type (--file-type), classify (-F) -i, --inode with -l, print the index number of each file -I, --ignore=PATTERN do not list implied entries matching shell PATTERN -k like --block-size=1K -l use a long listing format -L, --dereference when showing file information for a symbolic link, show information for the file the link references rather than for the link itself -m fill width with a comma separated list of entries -n, --numeric-uid-gid like -l, but list numeric user and group IDs -N, --literal print raw entry names (don't treat e.g. control characters specially) -o like -l, but do not list group information -p, --indicator-style=slash append / indicator to directories -q, --hide-control-chars print ? instead of non graphic characters --show-control-chars show non graphic characters as-is (default unless program is 'ls' and output is a terminal) -Q, --quote-name enclose entry names in double quotes --quoting-style=WORD use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape -r, --reverse reverse order while sorting -R, --recursive list subdirectories recursively -s, --size with -l, print size of each file, in blocks -S sort by file size --sort=WORD extension -X, none -U, size -S, time -t, version -v, status -c, time -t, atime -u, access -u, use -u --time=WORD with -l, show time as WORD instead of modification time: atime, access, use, ctime or status; use specified time as sort key if --sort=time --time-style=STYLE with -l, show times using style STYLE: full-iso, long-iso, iso, locale, +FORMAT. FORMAT is interpreted like 'date'; if FORMAT is FORMAT1<newline>FORMAT2, FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is prefixed with `posix-', STYLE takes effect only outside the POSIX locale -t sort by modification time -T, --tabsize=COLS assume tab stops at each COLS instead of 8 -u with -lt: sort by, and show, access time with -l: show access time and sort by name otherwise: sort by access time -U do not sort; list entries in directory order -v sort by version -w, --width=COLS assume screen width instead of current value -x list entries by lines instead of by columns -X sort alphabetically by entry extension -1 list one file per line --help display this help and exit --version output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following: kB 1000, K 1024, MB 10001000, M 10241024, and so on for G, T, P, E, Z, Y.

By default, color is not used to distinguish types of files. That is equivalent to using --color=none. Using the --color option without the optional WHEN argument is equivalent to using --color=always. With --color=auto, color codes are output only if standard output is connected to a terminal (tty). The environment variable LS_COLORS can influence the colors, and can be set easily by the dircolors command.

Exit status is 0 if OK, 1 if minor problems, 2 if serious trouble.

Report bugs to <bug-coreutils@gnu.org>.

cd - Change Directory

cd is the command to change directory. It is very much similar to the DOS command by the same name. To use it, type the name of the directory you want to go to. For example, if I am at my home folder (~), I can type this to get there, because Desktop is a subfolder of the home folder (as I saw by typing ls above):

[df4@localhost ~]$ cd Desktop

Lets say I am in the home folder and I want to go to the /opt folder. In this case, I need the whole path, as /opt is not a subfolder of my home folder:

   [df4@localhost ~]$ cd /opt

Finally, if I want to go up a folder, there is a shortcut. It is similar to DOS, but in DOS there is no space:

   [df4@localhost ~]$ cd ..

cp - Copy

cp is used to copy a file. It is often used to create backups of config files before you edit them, or move a folder while leaving the original. Lets say I want to make a copy of the file /boot/grub/menu.lst. I have two options. First, I could cd to the directory then make the change:

   [df4@localhost ~]$ su -
   [root@localhost ~]# cd /boot/grub
   [root@localhost ~]# cp menu.lst menu.lst.backup

Note that I needed to become root to edit anything in /boot since it is not in my home folder. Now this is how I could do it in one command:

   [df4@localhost ~]$su -
   [root@localhost ~]# cp /boot/grub/menu.lst /boot/grub/menu.lst.backup
   [root@localhost ~]# exit

ln - Link

ln is the command used to create links. It is very useful when you want a file to be available in several places but with only one version. You can link to directories or single files. Typically you use what are called "symbolic links". These "symbolic links" are created by using "ln -s", followed by the file that you are linking, and the path that it is being linked to. Note that you cannot make a link to a link, you must link to the original file/directory. A common application of a link is to enable plugins in multiple browsers. Below is an example of linking the flash plugin in my firefox plugins folder to "swiftfox" which I have installed:

   [df4@localhost ~]$ su -
   [root@localhost ~]# ln -s /usr/lib/mozilla/plugins/libflashplayer.so /opt/swiftfox/plugins/
   [root@localhost ~]# exit

mv - Move

mv is the command used to move a file or a directory. To move a file, you simply use mv, followed by the file, followed by the place you want to move it to. In this example I am moving my recently downloaded Fedora 7 Test 4 disk image from the desktop to my home folder:

   [df4@localhost ~]$ mv ~/Desktop/F-6.93-i386-DVD.iso ~/

rm - Remove/Delete

rm is the command to delete from the command line. This is a very dangerous command and should be used sparingly, as once a file is deleted by rm there is no way to recover it. Here I will delete the iso file from above using rm:

   [df4@localhost ~]$ rm ~/F-6.93-i386-DVD.iso

To delete a folder, use the tag "-r" for recursive. Here I will delete a folder called Pictures on my desktop:

   [df4@localhost ~]$rm -r ~/Desktop/Pictures

pwd - Print Working Directory

pwd stands for "print working directory", and it does just that. It prints the location you are currently in. Here is what happens when I type pwd from my home folder:

   [df4@localhost ~]$ pwd

mkdir - Make a Directory

mkdir is the command you would use to create a new folder. You need to specify the path and name of the folder. If I wanted to create a new folder on my desktop called pictures, it would look like this:

   [df4@localhost ~]$ mkdir ~/Desktop/Pictures

mount - Mount a Drive

mount is a command that lets you access new hard drives or partitions on your system (including flash drives that are not automatically recognized). In this most common use, I am mounting a drive that has another linux OS on it:

First, I need to know what the partition is called, so I run this:

   [df4@localhost ~]$ su -
   [root@localhost ~]# fdisk -l

Disk /dev/sda: 81.9 GB, 81964302336 bytes 255 heads, 63 sectors/track, 9964 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System /dev/sda1 1 2490 20000893+ 83 Linux /dev/sda2 2491 4980 20000925 83 Linux /dev/sda3 * 4981 7412 19535040 83 Linux /dev/sda4 7413 9964 20498940 5 Extended /dev/sda5 7413 9819 19334196 83 Linux /dev/sda6 9820 9964 1164681 82 Linux swap / Solaris

Now say I want to mount the first partition, which is identified as /dev/sda1. First I need to create a mount point (just a folder I will access the drive through). The usual places for this are in /media and /mnt. Either is equally acceptable. Then use the mount command to mount the drive, the cd to the directory to browse its contents:

   [root@localhost ~]# mkdir /media/sda1
   [root@localhost ~]# mount /dev/sda1 /media/sda1
   [root@localhost ~]# cd /media/sda1

If I were mounting a windows partition, the steps would basically be the same, except the mount command would look different. Windows NTFS partitions can be mounted as ntfs, which only allows reading of the files, not writing, or as ntfs-3g, which alows you to change the files. To use ntfs-3g, use the -t flag in order to specify the file system type:

   [root@localhost ~]# mount -t ntfs-3g /dev/sda1 /media/sda1

Finally, if you want to mount all the file systems listed in your /etc/fstab file (INSERT LINK TO FSTAB PAGE HERE), use the -a flag:

   [root@localhost ~]# mount -a

locate - A File Search

locate is used to find a file anywhere on your system. If I want to find any flashplayer on my system, I could search with the locate command like this:

   [df4@localhost ~]$ locate libflashplayer.so

The system keeps a database of the files on the system. If you are looking for a file that you have recently added, it might not have been updated recently. Type this to fix this (might take some time):


chmod - Change Permissions

chmod is used to change the permissions of a file. This is a very complicated command. This link will explain it better than I can: http://catcode.com/teachmod/ . 90% of the time you will be using this command to make a file executable. In this example, I am making a script I made called EasySAMity executable:

# chmod +x ~/Desktop/EasySAMity

And the video presentation of the command:

chown - Change Owner

chown is used to change the ownership of a file or folder. If I want to change the folder /media/sda1 from above to my own user so I can edit that drive without root permissions, I would issue the chown command, with the -R flag (recursive) so it affects all subdirectories as well:

chown -R df4 /media/sda1

top - A System Monitor

top is used to launch a command line system processes monitor. It updates frequently and is quite powerful:

   [df4@localhost ~]$ top
   top - 06:08:28 up 1 day,  8:48,  2 users,  load average: 0.06, 0.08, 0.08
   Tasks: 102 total,   1 running, 101 sleeping,   0 stopped,   0 zombie
   Cpu(s):  0.8% us,  0.2% sy,  0.0% ni, 98.5% id,  0.0% wa,  0.3% hi,  0.2% si
   Mem:   1035740k total,   993028k used,    42712k free,    89712k buffers
   Swap:  1164672k total,       44k used,  1164628k free,   649108k cached

15081 df4 15 0 202m 91m 21m S 1 9.1 7:44.85 mozilla-firefox
14426 root 15 0 86024 46m 10m S 0 4.6 82:55.41 Xorg
14654 df4 16 0 6588 4692 2224 S 0 0.5 1:37.23 python
20558 df4 15 0 25412 13m 8160 S 0 1.4 0:01.10 terminal
20681 df4 15 0 2072 1080 832 R 0 0.1 0:00.03 top
1 root 15 0 1568 536 468 S 0 0.1 0:01.41 init 2 root RT 0 0 0 0 S 0 0.0 0:00.01 migration/0 3 root 34 19 0 0 0 S 0 0.0 0:00.00 ksoftirqd/0 4 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/0 5 root RT 0 0 0 0 S 0 0.0 0:00.11 migration/1 6 root 34 19 0 0 0 S 0 0.0 0:00.00 ksoftirqd/1 7 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/1 8 root 10 -5 0 0 0 S 0 0.0 0:02.18 events/0 9 root 10 -5 0 0 0 S 0 0.0 0:00.23 events/1 10 root 10 -5 0 0 0 S 0 0.0 0:00.00 khelper
11 root 12 -5 0 0 0 S 0 0.0 0:00.00 kthread
35 root 10 -5 0 0 0 S 0 0.0 0:00.01 kblockd/0

I can quit the screen by pressing 'q'. I can also enter a mode to kill processes by hitting 'k', then using the PID number next to each process to kill it.

Launching Programs

In order to launch a program from the terminal, you need to type its name. Thats it. Well, mostly. As long as it was installed from the repositories (through synaptic or apt-get), the command will be placed in /usr/bin. Any command in /usr/bin can be launched by just typing the name, and the computer just assumes the /usr/bin:

   [df4@localhost ~]$ firefox

If the app you want to launch is in another folder (say, /opt/swiftfox) then you need the full path to the executable:

   [df4@localhost ~]$ /opt/swiftfox/swiftfox

Finally, if you are in the directory of the program, use ./ to launch it. The ./ means the current directory. You can't just type the name, because the computer assumes that means /usr/bin. Example launching swiftfox as above:

   [df4@localhost ~]$ cd /opt/swiftfox
   [df4@localhost ~]$ ./swiftfox

Last Modified: 2010-07-22 21:37:40 by serjant - [ Snip Changes ] [ Wiki History ]

No users online

Google Search Engine